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

sql-optimizer

Diagnose a slow SQL query and produce a concrete optimization plan. Use when asked to optimize SQL, speed up a slow query, reduce a query's cost/sca…

不碰外部(只输出文字)无严重或高危命中mohitagw15856/pm-claude-skills

它会碰到什么

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

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

技能内容

SQL Optimizer Skill

A slow query almost always has a specific, findable cause — a missing index, a non-sargable predicate, a

join that explodes rows, a scan that should be a seek. This skill diagnoses it: read what the query (and

plan, if given) is actually doing, name the bottleneck, and produce a concrete rewrite plus the index /

partition / structural changes — with the expected impact, not vague "add indexes" advice.

Required Inputs

Ask for these only if they aren't already provided:

  • The query (and the engine — Postgres, BigQuery, Snowflake, MySQL… optimizations differ).
  • The symptom — slow, expensive (bytes scanned), timing out, or just under review.
  • Context if availableEXPLAIN/query plan, table sizes/row counts, existing indexes, partitioning/clustering.

Output Format

SQL Optimization: [query purpose]

1. What it's doing now — read the query (and plan): the scans, joins, sorts, and where the time/cost goes. Name the primary bottleneck (don't list ten micro-tweaks — find the one that matters).

2. The problems — ranked, each with why it's slow:

  • Non-sargable predicates (functions on indexed columns, leading wildcards) → can't use an index.
  • Missing/`wrong index or partition pruning; full scans where a seek is possible.
  • Join issues — fan-out, wrong join order, missing join keys, SELECT * pulling everything.
  • Sorts/spills, DISTINCT/GROUP BY on high-cardinality, correlated subqueries that should be joins.
  • Engine-specific: BigQuery/Snowflake → bytes scanned (partition/cluster pruning), not row counts.

3. The fix — the rewritten query, plus the index / partition / clustering / materialization changes. Be specific (CREATE INDEX … ON … (cols), partition on event_date).

4. Expected impact — roughly what each change buys (seek vs. scan, pruning N% of partitions, removing a sort) and how to verify (re-run EXPLAIN, compare bytes/rows).

Quality Checks

  • [ ] Names the single primary bottleneck, not a scattershot list
  • [ ] Predicates are checked for sargability (no functions on indexed columns, no leading %)
  • [ ] Index/partition recommendations are specific (exact columns), not "add an index"
  • [ ] For columnar/cloud engines, addresses bytes scanned & pruning, not just row counts
  • [ ] Provides the rewritten query and a way to verify the improvement

Anti-Patterns

  • [ ] Do not say "add indexes" generically — name the columns and explain which predicate/join they serve
  • [ ] Do not ignore the engine — Postgres index tuning and BigQuery partition pruning are different games
  • [ ] Do not optimize a query that should be a model — repeated heavy logic belongs in a materialized/dbt model
  • [ ] Do not wrap indexed columns in functions in the WHERE clause — it kills index usage (non-sargable)
  • [ ] Do not recommend changes without an expected impact or a way to measure it

Based On

Query-optimization practice — sargability, index/partition pruning, join-order and fan-out, plan reading, columnar bytes-scanned tuning.

想直接用这个技能?

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

同名技能的其他版本

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