ln-651-query-efficiency-auditor by levnikolaevich/claude-code-skills
npx skills add https://github.com/levnikolaevich/claude-code-skills --skill ln-651-query-efficiency-auditorPaths: File paths (
shared/,references/,../ln-*) are relative to skills repo root. If not found at CWD, locate this SKILL.md directory and go up one level for repo root. Ifshared/is missing, fetch files via WebFetch fromhttps://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/{path}.
专门用于审计数据库查询模式中冗余、低效和误用问题的 Worker。
必读: 加载 shared/references/audit_worker_core_contract.md。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
接收包含以下内容的 contextStore:tech_stack、best_practices、db_config (数据库类型、ORM 设置)、codebase_root、output_dir。
领域感知: 支持 domain_mode + current_domain。
必读: 加载 shared/references/two_layer_detection.md 了解检测方法。
* 提取 tech_stack、best_practices、db_config、output_dir
* 确定 scan_path (逻辑与 ln-624 相同)
2. 扫描代码库中的违规行为
* 所有 Grep/Glob 模式都使用 `scan_path`
* 跟踪调用链以查找冗余获取 (需要读取调用者和被调用者)
3. 收集发现项,包含严重性、位置、工作量、建议
使用惩罚算法计算分数
编写报告: 根据 shared/templates/audit_worker_report_template.md 在内存中构建完整的 Markdown 报告,通过单次 Write 调用写入 {output_dir}/651-query-efficiency.md
返回摘要: 向协调器返回最小摘要 (见输出格式)
内容: 在同一调用链中两次从数据库获取同一实体
检测:
repo.get(id) 或 session.get(Model, id),然后将 id (而非对象) 传递给函数 Brepo.get(id) 或 session.get(Model, id)acquire_next_pending() 返回作业,但 _process_job(job_id) 重新获取它检测模式 (Python/SQLAlchemy):
repo.*get_by_id|session\.get\(|session\.query.*filter.*identity_id: int/UUID 并且在内部执行 repo.get(entity_id),检查调用者是否已拥有实体对象expire_on_commit 设置:如果为 False,对象在提交后保持有效严重性:
建议: 传递实体对象而非 ID,或在 expire_on_commit=False 时移除第二次获取
工作量: 小 (更改签名以接受对象而非 ID)
内容: 使用单独的 UPDATE/DELETE 操作循环,而非单个批量查询
检测:
for item in items: await repo.update(item.id, ...) 或 for item in items: await repo.delete(item.id)for item in items: session.execute(update(Model).where(...))检测模式:
for .* in .*: 后 1-3 行内跟有 repo\.(update|delete|reset|save|mark_)for .* in .*: 后 1-3 行内跟有 session\.execute\(.*update\(严重性:
建议: 替换为单个 UPDATE ... WHERE id IN (...) 或 session.execute(update(Model).where(Model.id.in_(ids)))
工作量: 中 (重写查询 + 测试)
内容: 当值已在调用者作用域内可用时,从数据库重新解析该值
检测:
profile_id 并从中解析引擎,但调用者已确定 enginelang_code 并查找 dialect_id,但调用者已同时拥有 lang 和 dialectX_id,执行 get(X_id),提取 .field,而调用者已拥有 field严重性:
建议: 将方法拆分为两个变体:with_known_value(value, ...) 和 resolving_value(id, ...);或直接传递已解析的值
工作量: 小-中 (重构签名,更新调用者)
内容: 加载完整的 ORM 模型,而实际上只需要少数几个字段
检测:
session.query(Model) 或 select(Model) 没有为列数超过 10 的模型使用 .options(load_only(...))严重性:
建议: 对于列表查询,使用 load_only()、defer() 或原始的 select(Model.col1, Model.col2)
工作量: 小 (在查询中添加 load_only)
内容: 使用顺序 INSERT/DELETE/UPDATE 而非批量操作
检测:
for item in items: session.add(item) 而不是 session.add_all(items)for item in items: session.delete(item) 而不是批量删除INSERT 的循环严重性:
建议: 使用 session.add_all()、session.execute(insert(Model).values(list_of_dicts))、bulk_save_objects()
工作量: 小 (用批量调用替换循环)
内容: 为很少变化的数据使用请求作用域缓存 (应使用应用作用域)
检测:
Depends()),并带有内部缓存 (_cache 字典、_loaded 标志)检测模式:
_cache、_loaded、_initialized 属性的类严重性:
建议: 将缓存移至应用作用域服务 (单例),添加基于 TTL 的失效,或使用具有可配置 TTL 的 CacheService
工作量: 中 (更改 DI 作用域,添加 TTL 逻辑)
必读: 加载 shared/references/audit_worker_core_contract.md 和 shared/references/audit_scoring.md。
必读: 加载 shared/references/audit_worker_core_contract.md 和 shared/templates/audit_worker_report_template.md。
将报告写入 {output_dir}/651-query-efficiency.md,设置 category: "Query Efficiency" 和检查项:redundant_fetch, n_update_delete_loop, unnecessary_resolve, over_fetching, missing_bulk_ops, wrong_caching_scope。
向协调器返回摘要:
Report written: docs/project/.audit/ln-650/{YYYY-MM-DD}/651-query-efficiency.md
Score: X.X/10 | Issues: N (C:N H:N M:N L:N)
必读: 加载 shared/references/audit_worker_core_contract.md。
expire_on_commit、autoflush、会话作用域必读: 加载 shared/references/audit_worker_core_contract.md。
{output_dir}/651-query-efficiency.md (原子性的单次 Write 调用)shared/references/audit_output_schema.md版本: 1.0.0 最后更新: 2026-02-04
每周安装数
145
仓库
GitHub 星标数
245
首次出现
2026年2月7日
安全审计
安装于
cursor132
opencode132
gemini-cli131
claude-code131
codex131
github-copilot130
Paths: File paths (
shared/,references/,../ln-*) are relative to skills repo root. If not found at CWD, locate this SKILL.md directory and go up one level for repo root. Ifshared/is missing, fetch files via WebFetch fromhttps://raw.githubusercontent.com/levnikolaevich/claude-code-skills/master/{path}.
Specialized worker auditing database query patterns for redundancy, inefficiency, and misuse.
MANDATORY READ: Load shared/references/audit_worker_core_contract.md.
Receives contextStore with: tech_stack, best_practices, db_config (database type, ORM settings), codebase_root, output_dir.
Domain-aware: Supports domain_mode + current_domain.
MANDATORY READ: Load shared/references/two_layer_detection.md for detection methodology.
Parse context from contextStore
Scan codebase for violations
scan_pathCollect findings with severity, location, effort, recommendation
Calculate score using penalty algorithm
Write Report: Build full markdown report in memory per shared/templates/audit_worker_report_template.md, write to {output_dir}/651-query-efficiency.md in single Write call
Return Summary: Return minimal summary to coordinator (see Output Format)
What: Same entity fetched from DB twice in a call chain
Detection:
repo.get(id) or session.get(Model, id), then passes id (not object) to function Brepo.get(id) or session.get(Model, id) for the same entityacquire_next_pending() returns job, but _process_job(job_id) re-fetches itDetection patterns (Python/SQLAlchemy):
repo.*get_by_id|session\.get\(|session\.query.*filter.*id in service/handler filesentity_id: int/UUID AND internally does repo.get(entity_id), check if caller already has entity objectexpire_on_commit setting: if False, objects remain valid after commitSeverity:
Recommendation: Pass entity object instead of ID, or remove second fetch when expire_on_commit=False
Effort: S (change signature to accept object instead of ID)
What: Loop of individual UPDATE/DELETE operations instead of single batch query
Detection:
for item in items: await repo.update(item.id, ...) or for item in items: await repo.delete(item.id)for item in items: session.execute(update(Model).where(...))Detection patterns:
for .* in .*: followed by repo\.(update|delete|reset|save|mark_) within 1-3 linesfor .* in .*: followed by session\.execute\(.*update\( within 1-3 linesSeverity:
Recommendation: Replace with single UPDATE ... WHERE id IN (...) or session.execute(update(Model).where(Model.id.in_(ids)))
Effort: M (rewrite query + test)
What: Re-resolving a value from DB when it is already available in the caller's scope
Detection:
profile_id and resolves engine from it, but caller already determined enginelang_code and looks up dialect_id, but caller already has both lang and dialectX_id, does get(X_id), extracts .field, when caller already has fieldSeverity:
Recommendation: Split method into two variants: with_known_value(value, ...) and resolving_value(id, ...); or pass resolved value directly
Effort: S-M (refactor signature, update callers)
What: Loading full ORM model when only few fields are needed
Detection:
session.query(Model) or select(Model) without .options(load_only(...)) for models with >10 columnsSeverity:
Recommendation: Use load_only(), defer(), or raw select(Model.col1, Model.col2) for list queries
Effort: S (add load_only to query)
What: Sequential INSERT/DELETE/UPDATE instead of bulk operations
Detection:
for item in items: session.add(item) instead of session.add_all(items)for item in items: session.delete(item) instead of bulk deleteINSERT per iterationSeverity:
Recommendation: Use session.add_all(), session.execute(insert(Model).values(list_of_dicts)), bulk_save_objects()
Effort: S (replace loop with bulk call)
What: Request-scoped cache for data that rarely changes (should be app-scoped)
Detection:
Depends()) with internal cache (_cache dict, _loaded flag)Detection patterns:
_cache, _loaded, _initialized attributesSeverity:
Recommendation: Move cache to app-scoped service (singleton), add TTL-based invalidation, or use CacheService with configurable TTL
Effort: M (change DI scope, add TTL logic)
MANDATORY READ: Load shared/references/audit_worker_core_contract.md and shared/references/audit_scoring.md.
MANDATORY READ: Load shared/references/audit_worker_core_contract.md and shared/templates/audit_worker_report_template.md.
Write report to {output_dir}/651-query-efficiency.md with category: "Query Efficiency" and checks: redundant_fetch, n_update_delete_loop, unnecessary_resolve, over_fetching, missing_bulk_ops, wrong_caching_scope.
Return summary to coordinator:
Report written: docs/project/.audit/ln-650/{YYYY-MM-DD}/651-query-efficiency.md
Score: X.X/10 | Issues: N (C:N H:N M:N L:N)
MANDATORY READ: Load shared/references/audit_worker_core_contract.md.
expire_on_commit, autoflush, session scope before flagging redundant fetchesMANDATORY READ: Load shared/references/audit_worker_core_contract.md.
{output_dir}/651-query-efficiency.md (atomic single Write call)shared/references/audit_output_schema.mdVersion: 1.0.0 Last Updated: 2026-02-04
Weekly Installs
145
Repository
GitHub Stars
245
First Seen
Feb 7, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
cursor132
opencode132
gemini-cli131
claude-code131
codex131
github-copilot130
代码库搜索技能指南:精准查找函数、追踪依赖、理解架构与定位错误
10,900 周安装