N+1 Query Detector

N+1 Query Detection

by oliver-kriska · MIT license · ★ 560 on GitHub

N+1 Query Detection cover

Detect N+1 query anti-patterns specifically — Repo calls inside Enum/for loops, missing preloads on associations. Use when N+1 is explicitly suspected, NOT for unrelated Ecto questions or wider database performance.

---
name: n1-check
description: "Detect N+1 query anti-patterns specifically — Repo calls inside Enum/for loops, missing preloads on associations. Use when N+1 is explicitly suspected, NOT for unrelated Ecto questions or wider database performance."
effort: medium
---

# N+1 Query Detection

Identify and fix N+1 query anti-patterns in Ecto/Phoenix applications.

## Iron Laws - Never Violate These

1. **Never access associations without preload** - Always preload before `Enum.map`
2. **No Repo calls inside loops** - Restructure to batch queries
3. **Preload at context boundary** - Load associations in context, not controllers/views
4. **Use joins for filtering** - Use `join` + `preload` when filtering by association

## Detection Patterns

Overview

Identify and fix N+1 query anti-patterns in Ecto/Phoenix applications.

Manual install (npx, ZIP)
Claude Codenpx skills add oliver-kriska/claude-elixir-phoenix --skill n1-check -g -a claude-code
Claude Code (degit)npx degit oliver-kriska/claude-elixir-phoenix/plugins/elixir-phoenix/skills/n1-check#main ~/.claude/skills/n1-check
Claude.ai / desktopZip the folder and upload it under Settings → Capabilities → Skills.
Codex, Cursor, other agentsPoint them at SKILL.md. The other files are plain text.

Connects with Agent Alley MCP · agentalley.io/mcp

Files included

  • SKILL.md2.1 KB
Read SKILL.md here
oliver-kriska/main1 file shown
SKILL.md
Show the full text73 lines

N+1 Query Detection

Identify and fix N+1 query anti-patterns in Ecto/Phoenix applications.

Iron Laws - Never Violate These

  1. Never access associations without preload - Always preload before Enum.map
  2. No Repo calls inside loops - Restructure to batch queries
  3. Preload at context boundary - Load associations in context, not controllers/views
  4. Use joins for filtering - Use join + preload when filtering by association

Detection Patterns

Pattern 1: Enum.map with Repo
# BAD: N+1 queries
users
|> Enum.map(fn user -> Repo.get(Order, user.order_id) end)

# GOOD: Single query with preload
users
|> Repo.preload(:orders)
Pattern 2: Association Access Without Preload
# BAD: Lazy loading triggers N queries
for user <- users do
  user.posts  # Triggers query for each user!
end

# GOOD: Eager load first
users = Repo.all(User) |> Repo.preload(:posts)
for user <- users do
  user.posts  # Already loaded
end
Pattern 3: Nested Association Access
# BAD: N+1 for nested associations
user.posts |> Enum.map(fn post -> post.comments end)

# GOOD: Nested preload
Repo.preload(user, posts: :comments)

Quick Detection Commands

Use Grep with context lines (-B 5 -A 5) to find Enum.map near Repo. calls in lib/**/*.ex. Use Grep to find association access patterns (.posts, .comments, .orders) in lib/**/*.ex. Use Grep with context (-B 3) to find Repo.get or Repo.one near loop patterns (for, Enum) in lib/**/*.ex.

Analysis Command

Use Grep to find all Repo. calls in a context module, then verify each query has appropriate preloads.

References

For detailed patterns, see:

  • ${CLAUDE_SKILL_DIR}/references/preload-patterns.md - Efficient preloading strategies
  • ${CLAUDE_SKILL_DIR}/references/query-optimization.md - Query batching techniques
1---
2name: n1-check
3description: "Detect N+1 query anti-patterns specifically — Repo calls inside Enum/for loops, missing preloads on associations. Use when N+1 is explicitly suspected, NOT for unrelated Ecto questions or wider database performance."
4effort: medium
5---
6 
7# N+1 Query Detection
8 
9Identify and fix N+1 query anti-patterns in Ecto/Phoenix applications.
10 
11## Iron Laws - Never Violate These
12 
131. **Never access associations without preload** - Always preload before `Enum.map`
142. **No Repo calls inside loops** - Restructure to batch queries
153. **Preload at context boundary** - Load associations in context, not controllers/views
164. **Use joins for filtering** - Use `join` + `preload` when filtering by association
17 
18## Detection Patterns
19 
20### Pattern 1: Enum.map with Repo
21 
22```elixir
23# BAD: N+1 queries
24users
25|> Enum.map(fn user -> Repo.get(Order, user.order_id) end)
26 
27# GOOD: Single query with preload
28users
29|> Repo.preload(:orders)
30```
31 
32### Pattern 2: Association Access Without Preload
33 
34```elixir
35# BAD: Lazy loading triggers N queries
36for user <- users do
37 user.posts # Triggers query for each user!
38end
39 
40# GOOD: Eager load first
41users = Repo.all(User) |> Repo.preload(:posts)
42for user <- users do
43 user.posts # Already loaded
44end
45```
46 
47### Pattern 3: Nested Association Access
48 
49```elixir
50# BAD: N+1 for nested associations
51user.posts |> Enum.map(fn post -> post.comments end)
52 
53# GOOD: Nested preload
54Repo.preload(user, posts: :comments)
55```
56 
57## Quick Detection Commands
58 
59Use Grep with context lines (`-B 5 -A 5`) to find `Enum.map` near `Repo.` calls in `lib/**/*.ex`.
60Use Grep to find association access patterns (`.posts`, `.comments`, `.orders`) in `lib/**/*.ex`.
61Use Grep with context (`-B 3`) to find `Repo.get` or `Repo.one` near loop patterns (`for`, `Enum`) in `lib/**/*.ex`.
62 
63## Analysis Command
64 
65Use Grep to find all `Repo.` calls in a context module, then verify each query has appropriate preloads.
66 
67## References
68 
69For detailed patterns, see:
70 
71- `${CLAUDE_SKILL_DIR}/references/preload-patterns.md` - Efficient preloading strategies
72- `${CLAUDE_SKILL_DIR}/references/query-optimization.md` - Query batching techniques
73 
View all files on GitHub →

Details

License
MIT
Stars on the repo
★ 560
Checked
Source
oliver-kriska/claude-elixir-phoenix ↗
Creator
oliver-kriska · 7 listings
View creator

Similar skills

See all →

More from oliver-kriska

SkillAsync Return Briefingoliver-kriska/claude-elixir-phoenix · by oliver-kriskaSummarize and review what changed while you were away. Use after a weekend, vacation, or flight to check missed PRs, git commits, Linear tickets, and meetings — one prioritized brief, not a firehose.★ 560 GitHub starsSkillHex Dependency Security Auditoliver-kriska/claude-elixir-phoenix · by oliver-kriskaAudit Hex deps for supply-chain security risk — bidi chars, compile-time exec, maintainer changes, typosquats, CVEs. Use after mix deps.update, when checking if a package upgrade is safe, or reviewing mix.lock PR diffs.★ 560 GitHub starsSkillCatchup Briefingoliver-kriska/claude-elixir-phoenix · by oliver-kriskaEaster-egg alias for /catchup. Same return-from-absence briefing, squeezier name. Use exactly like /catchup — all flags pass through unchanged.★ 560 GitHub starsSkillLiveView Memory Auditoroliver-kriska/claude-elixir-phoenix · by oliver-kriskaInspect LiveView socket assigns for memory bloat — missing temporary_assigns, unused assigns, unbounded lists needing streams, memory estimates. Use when LiveView memory grows or you need to add temporary_assigns.★ 560 GitHub starsSkillPhoenix Context Validatoroliver-kriska/claude-elixir-phoenix · by oliver-kriskaAnalyze Phoenix context boundaries and module coupling via mix xref. Use when checking cross-context calls, validating dependencies, before splitting modules, or reviewing architecture.★ 560 GitHub starsSkillPlugin Development Workflowoliver-kriska/claude-elixir-phoenix · by oliver-kriskaGuide plugin development workflow — editing skills, agents, hooks, or eval framework in this repo. Use when modifying files in plugins/elixir-phoenix/, lab/eval/, or lab/autoresearch/. Ensures changes pass eval, lint, and tests before committing.★ 560 GitHub stars