ecto:n1-check

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

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "ecto:n1-check" with this command: npx skills add oliver-kriska/claude-elixir-phoenix/oliver-kriska-claude-elixir-phoenix-ecto-n1-check

N+1 Query Detection

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

Iron Laws - Never Violate These

  • Never access associations without preload - Always preload before Enum.map

  • No Repo calls inside loops - Restructure to batch queries

  • Preload at context boundary - Load associations in context, not controllers/views

  • 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

Find Enum.map with Repo calls nearby

grep -B5 -A5 "Enum.map" lib/ -r --include="*.ex" | grep -A5 -B5 "Repo."

Find association access patterns

grep -r ".posts|.comments|.orders" lib/ --include="*.ex"

Find Repo calls in loops

grep -B3 "Repo.get|Repo.one" lib/ -r --include="*.ex" | grep -B3 "for|Enum"

Analysis Command

For a context module, run:

grep -n "Repo." lib/my_app/[context].ex

Then verify each query has appropriate preloads.

References

For detailed patterns, see:

  • references/preload-patterns.md

  • Efficient preloading strategies

  • references/query-optimization.md

  • Query batching techniques

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

oban

No summary provided by upstream source.

Repository SourceNeeds Review
General

phx:full

No summary provided by upstream source.

Repository SourceNeeds Review
General

ecto-patterns

No summary provided by upstream source.

Repository SourceNeeds Review
General

tidewave-integration

No summary provided by upstream source.

Repository SourceNeeds Review