跳到主要内容
知仓学习社ZHICANG

ecto-n1-check

Find Ecto N+1 queries and missing preloads. Use only when N+1 is suspected;

不碰外部(只输出文字)无严重或高危命中oliver-kriska/claude-elixir-phoenix

它会碰到什么

扫了多少3 个文本文件,9 KB
它会碰到什么不碰外部(只输出文字)
命中总数0 处
命中统计严重 0 · 高 0 · 中 0 · 低 0

这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。

技能内容

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

For a context module, run:

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

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

想直接用这个技能?

本站把开放许可(MIT / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。

它属于哪个仓库

星标★ 553
本站分层T2
该仓技能数322
原文件路径targets/codex/skills/ecto-n1-check/SKILL.md

同一个仓库里的其他技能

看这个仓库的全部 322 个技能

同名技能的其他版本

有 5 个不同仓库或目录里都有叫 ecto-n1-check 的技能。它们内容并不相同,别混用: