AGENT SKILL

SQL Query Review

Deep review of SQL queries — execution plans, index utilization, join efficiency, subquery patterns, locking behavior, and performance anti-patterns.

License 许可
MIT
最近更新
2026-10-09
Stars
★ 1
Forks
0

01 前言 Front Matter

name
SQL Query Review
license
MIT
version
2026.3.1
triggers
{ "lenses": [ "performance", "logic" ], "keywords": [ "sql", "query", "select", "join", "index", "explain" ] }
description
Deep review of SQL queries — execution plans, index utilization, join efficiency, subquery patterns, locking behavior, and performance anti-patterns.

03 SKILL.md 正文

When reviewing SQL queries, apply the following checks.

Full Table Scans

Flag SELECT * on large tables — specify only needed columns. Flag queries without a WHERE clause on tables with more than a few hundred rows. Check that WHERE clause columns have supporting indexes.

Flag LIKE '%pattern%' (leading wildcard) — this cannot use an index. Flag OR conditions on different columns — these often prevent index usage; consider rewriting as UNION ALL. Flag function calls on indexed columns in WHERE clauses (WHERE YEAR(created_at) = 2026) — this prevents index usage; rewrite as range conditions.

Join Efficiency

Verify join columns have indexes on both sides. Flag CROSS JOIN without explicit intent documentation — it produces a Cartesian product. Flag implicit joins (FROM a, b WHERE a.id = b.a_id) — use explicit JOIN ... ON syntax.

Check join order for large tables — the optimizer usually handles this, but flag forced join orders (STRAIGHT_JOIN) without justification. Flag joins on non-indexed columns between large tables — this causes nested loop scans.

Subquery Anti-Patterns

Flag correlated subqueries in SELECT or WHERE clauses — these execute once per row in the outer query. Rewrite as JOIN or use a CTE. Flag IN (SELECT ...) on large result sets — rewrite as EXISTS or JOIN.

Flag SELECT inside SELECT that could be a window function. Check that CTEs (WITH clauses) are not used for premature materialization — on some databases, CTEs are optimization fences.

N+1 in Application Code

When reviewing queries generated by an ORM, flag patterns where a query inside a loop produces one SQL statement per iteration. Check for:

  • Loop over a collection with a .load() or .find() call per item
  • Template rendering that triggers lazy-loaded relationship queries
  • API serialization that accesses related objects without eager loading

Index Design

Flag redundant indexes: if (a, b) composite exists, a standalone (a) index is redundant. Flag indexes on low-cardinality columns (boolean, status with < 5 values) unless used in combination with selective columns.

Check that unique constraints exist for natural keys. Flag tables where uniqueness is enforced only at the application level — the database should be the source of truth.

Verify covering indexes are considered for frequently-run read queries — all columns in SELECT, WHERE, and ORDER BY in a single index avoids table lookups.

Locking and Concurrency

Flag SELECT ... FOR UPDATE on ranges that could be narrowed. Flag long-running transactions that hold row locks — keep transaction scope minimal. Check for potential deadlocks from transactions that lock tables in different orders.

Flag LOCK TABLE in application code — use row-level locking instead. Verify read-heavy queries use READ COMMITTED or appropriate isolation level — SERIALIZABLE has significant overhead.

Data Type Efficiency

Flag VARCHAR(255) as a default when shorter lengths are known. Flag TEXT or BLOB for columns that should have length constraints. Check that numeric types match the expected range — BIGINT for IDs that will never exceed INT range wastes storage.

Flag storing dates as strings — use native DATE, TIMESTAMP, or TIMESTAMPTZ types. Check timezone handling: verify TIMESTAMP WITH TIME ZONE is used for user-facing timestamps.

For each finding, report: the query or table, the specific performance or correctness issue, and the recommended fix.

04 同仓库更多 Skills