Claude Skill

n1-check

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.

LLM Mart · 0 points · 1 views 0 listing impressions 0 install-command copies
Download oliver-kriska-claude-elixir-phoenix-plugins_elixir-phoenix_skills_n1-check-9767a82.zip · 3 KB

Install

skills CLI npx skills add https://github.com/oliver-kriska/claude-elixir-phoenix/tree/main/plugins/elixir-phoenix/skills/n1-check
Claude Code claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install oliver-kriska-claude-elixir-phoenix@llmmart
Git git clone https://github.com/oliver-kriska/claude-elixir-phoenix.git

The skills CLI installs just this skill, for any of its supported agents. Claude Code installs the whole oliver-kriska/claude-elixir-phoenix collection as a plugin from our marketplace. Git is the plain clone.

Skill manifest

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
Files (claude-elixir-phoenix)
  • references
    • preload-patterns.md 2.9 KB
      Loading…
    • query-optimization.md 3.8 KB
      Loading…
  • SKILL.md 2.1 KB
    ---
    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
    
    ### Pattern 1: Enum.map with Repo
    
    ```elixir
    # 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
    
    ```elixir
    # 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
    
    ```elixir
    # 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
    

Comments (0)

Sign in to join the conversation.

No comments yet.

Reviews (0)

No reviews yet.