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: ecto: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
allowed-tools: Read, Grep, Glob, Bash
---
# 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
```elixir
# BAD: N+1 queries
users… install to load the full skill