Ash Query Optimizer
Ash query optimizer — detects N+1 loads, suggests aggregates over load+Enum, identifies calculation vs load tradeoffs. Use when reviewing Ash queries, LiveView data loading, or domain action efficiency.
- Type
- Subagent
- Repository
- oliver-kriska/claude-elixir-phoenix
- GitHub stars
- 556
- License
- MIT
- Repo last updated
- Sep 25, 2026
- Model
- sonnet
What Ash Query Optimizer is
Ash Query Optimizer is a subagent published in the oliver-kriska/claude-elixir-phoenix repository on GitHub, which has about 556 stars. The repository describes itself as: “Claude Code plugin for Elixir/Phoenix/LiveView — 26 specialist agents, Iron Laws enforcement, and Tidewave MCP integration. Plan features with parallel research agents, execute with automatic verification, review with 4-agent parallel audits, and capture learnings as reusable knowledge.”
A subagent is a specialist assistant that Claude can hand part of a task to. It is a markdown file whose frontmatter sets a name, a description that tells Claude when to delegate, and optionally the tools and model it may use; the body becomes the subagent's own system prompt.
Because a subagent works in its own context, it keeps the main conversation focused: Claude can send a narrow job, such as a review or a specialised analysis, to Ash Query Optimizer and get back a compact result.
It is set up to use these tools: Read, Grep, Glob, Write. Limiting tools is a good sign: the subagent can only do what those tools allow.
How to install Ash Query Optimizer
Claude Code
- Download ash-query-optimizer.md from the repository.
- Save it to ~/.claude/agents/ to use it in every project, or to .claude/agents/ inside one project to share it through version control.
- Claude Code watches these folders, so the subagent is usually available right away. Ask Claude to use it by name, or @-mention it to make sure it runs.
Claude Cowork
- Cowork loads subagents through plugins. If the repository is packaged as a plugin marketplace, add it under Customize → Plugins → Add marketplace and install the plugin that contains this subagent.
- Otherwise, bundle the file into your own plugin's agents/ folder and upload it from Customize → Plugins.
New to extending Cowork? Our plugins guide and Customize guide explain how skills, plugins, and connectors fit together.
Inside the source file
An excerpt from plugins/elixir-phoenix/agents/ash-query-optimizer.md, shared under the repository's MIT license. Read the full file on GitHub.
Detect N+1 patterns, load/aggregate/calculation mismatches, and inefficient data fetching in Ash Framework projects. Output is a findings file; you do not modify source code.
CRITICAL: Save Findings File First
Turn budget:
- First ~8 turns: Grep for load patterns in LiveViews and domain modules
- By turn ~10: Write initial findings — partial file beats no file
- Remaining turns: Deepen analysis with read/aggregate/combination alternatives
Default output path if none given: .claude/reviews/ash-query-opt.md
Iron Laws — Flag All Violations
- NO LOAD FOR COUNT/SUM — Ash.load(records, [:children]) followed by Enum.count/length/Enum.sum is an N+1; declare or inline an aggregate (count, sum, avg, min, max, list, first)
- EXISTS, NOT count > 0 — Presence checks must use the exists aggregate or exists/2 in expressions; count > 0 scans every matching row instead of short-circuiting
- NO LOADING IN LOOPS — Ash.load!/2, Ash.read!, or a domain action inside Enum.map/each/reduce is an N+1; batch with a single load or a bulk action
- DERIVED VALUES → CALCULATIONS — Values computed from attributes or relationships belong in a calculation on the resource, not post-load Map.put/Enum.map in callers. Calculations stay filterable and sortable in the query layer
- CUSTOMIZE LOADS WITH QUERIES, NOT POST-FILTERS — Filtering a loaded relationship with Enum.filter after Ash.load wastes a DB round trip; pass a query to load: Ash.load(users, posts: Ash.Query.filter(Post, published == true))
- SELECT FOR LARGE RESOURCES — Reading a resource with 20+ attributes when only a few are needed should use Ash.Query.select/2
- NO DIRECT REPO CALLS — MyApp.Repo.all/aggregate/one in resource-backed code skips policies, calculations, and aggregates; use Ash actions or Ash.aggregate
- PIN USER INPUT WITH ^ — Same rule as Ecto; user input in filter expr(...) must be pinned, not interpolated
Load vs Aggregate vs Calculation vs Combination
N+1 and Anti-Pattern Detection
Pattern 1: Load Inside Enum
# BAD — one load per user
users |> Enum.map(fn user ->
user = Ash.load!(user, :posts)
{user, length(user.posts)}
end)
# GOOD — single batched load (Ash batches nested loads too)
users = Ash.load!(users, :posts)
Enum.map(users, fn user -> {user, length(user.posts)} end)
# BETTER — declared aggregate on User, loaded at read time
# aggregates do: count :post_count, :posts end
users = Ash.read!(User |> Ash.Query.load(:post_count))Pattern 2: Load Just to Count
# BAD — fetches every comment to call length/1
post = Ash.load!(post, :comments)
length(post.comments)
# GOOD — declared aggregate
# aggregates do: count :comment_count, :comments end
post = Ash.load!(post, :comment_count)Pattern 3: count > 0 Presence Check
# BAD — runs COUNT(*) over the whole set
post = Ash.load!(post, :comment_count)
if post.comment_count > 0, do: ...
# GOOD — exists aggregate (declared or inline)
# aggregates do: exists :has_comments, :comments end
post = Ash.load!(post, :has_comments)
# OR inline in an expression
Ash.Query.filter(Post, exists(comments, author_id == ^current_user.id))Pattern 4: Domain Action in a Loop
# BAD — N round trips
Enum.each(ids, fn id -> MyApp.Accounts.deactivate_user!(id) end)
# GOOD — bulk action via code interface
MyApp.Accounts.bulk_deactivate_users!(ids)
# OR Ash.bulk_update with a query
User |> Ash.Query.filter(id in ^ids) |> Ash.bulk_update!(:deactivate, %{})Pattern 5: Post-Load Computation Belongs in a Calculation
# BAD — derived field computed in caller, not filterable/sortable in queries
users |> Enum.map(&Map.put(&1, :full_name, "#{&1.first_name} #{&1.last_name}"))
# GOOD — expression calculation on the resource
# calculations do: calculate :full_name, :string, expr(first_name <> " " <> last_name) end
Ash.load!(users, :full_name) Before you install
- Read the whole file first. Skills, commands, and subagents are instructions Claude will follow, so make sure they match what you want.
- Check which tools, scripts, or MCP servers it uses. Local servers and scripts run with your permissions.
- Try it in a test project or a copy of your files before pointing it at real work.
- Pin the version you tested, and review changes before updating.
- Watch for instructions that fetch web content or run shell commands; those are where prompt injection risks start. See our prompt injection guide.
FAQ
What is Ash Query Optimizer?
Ash Query Optimizer is a subagent for Claude Code and Claude Cowork from the oliver-kriska/claude-elixir-phoenix repository on GitHub. Ash query optimizer — detects N+1 loads, suggests aggregates over load+Enum, identifies calculation vs load tradeoffs. Use when reviewing Ash queries, LiveView data loading, or domain action efficiency.
How do I install Ash Query Optimizer in Claude Code?
Download ash-query-optimizer.md from the repository. Save it to ~/.claude/agents/ to use it in every project, or to .claude/agents/ inside one project to share it through version control. Claude Code watches these folders, so the subagent is usually available right away. Ask Claude to use it by name, or @-mention it to make sure it runs.
Can I use Ash Query Optimizer in Claude Cowork?
Cowork loads subagents through plugins. If the repository is packaged as a plugin marketplace, add it under Customize → Plugins → Add marketplace and install the plugin that contains this subagent. Otherwise, bundle the file into your own plugin's agents/ folder and upload it from Customize → Plugins.
Is Ash Query Optimizer safe to install?
It is a third-party community resource, not reviewed by Anthropic or this site. Read the source file first, check which tools and connectors it uses, and install only from sources you trust.
Similar resources
- Hex Library Researcher Researches Elixir libraries on hex.pm. Use when evaluating libraries for a feature, checking alternatives, or verifying library quality and compatibility. Subagent · oliver-kriska/claude-elixir-phoenix
- Oban Specialist Oban worker specialist - reviews idempotency, error handling, and production safety. Use proactively when implementing or reviewing background jobs. Subagent · oliver-kriska/claude-elixir-phoenix
- Liveview Architect LiveView architecture specialist - component structure, real-time patterns, streams vs assigns, async patterns. Use proactively when planning interactive features. Subagent · oliver-kriska/claude-elixir-phoenix
- Iron Law Judge Checks code for Iron Law violations using pattern analysis. Use proactively after code changes or as part of review. Subagent · oliver-kriska/claude-elixir-phoenix
- Ash Resource Designer Ash resource architect — designs resources the "Ash Way" with built-in changes, validations, types, and policy checks before hand-rolling. Use proactively when planning new resources or extending existing ones. Subagent · oliver-kriska/claude-elixir-phoenix
- Ash Policy Reviewer Ash policy security reviewer — audits policies, checks, and authorization rules for gaps, bypass patterns, and ordering hazards. Use proactively on Ash resources with policies do blocks or checks/ modules. Subagent · oliver-kriska/claude-elixir-phoenix
- Call Tracer Orchestrates parallel call tree tracing using subagents for each entry point category (Controllers, LiveViews, Workers, GenServers). Use proactively when debugging unexpected values, tracing request flow, or planning signature changes. Subagent · oliver-kriska/claude-elixir-phoenix
- Workflow Verifier Goal-backward verification across three layers (existence, substance, connection) Subagent · catlog22/maestro-flow