Data & analytics · Review & audit
sql-review
/sql-review
SQL 性能评审:新增或修改 SQL、加索引、改分页、改批量写入时使用;提交前必须给出执行计划与数据量评估。
For whom
- Author
- fengyale ↗
- Repository
- harness-bootstrap ↗
- Licence
- MIT
- Last repository change
- 13.11.2026
Assessment and security
91/ 100Class: good
Format20 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability21 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 13
- URLs in SKILL.md
- 0
Deprecation
No signals detected. Repository changed on 13.11.2026, without references to retired models or APIs.
Licence
MIT, open: use and modify while retaining author attribution.
What to check before installing
- SKILL.md zawiera komentarze HTML (13). Nie widać ich w podglądzie Markdown, a model je czyta.
What it does and when to use it
SQL 性能评审:新增或修改 SQL、加索引、改分页、改批量写入时使用;提交前必须给出执行计划与数据量评估。
Original author description
SQL 性能评审:新增或修改 SQL、加索引、改分页、改批量写入时使用;提交前必须给出执行计划与数据量评估。
How it works
- 何时用
- 项目绑定(编译时替换)
- 模式(跨技术栈不变)
- 证据锚点(编译后填写)
Original SKILL.md headings · 105 lines, 452 words
SKILL.md preview
---
name: sql-review
description: SQL 性能评审:新增或修改 SQL、加索引、改分页、改批量写入时使用;提交前必须给出执行计划与数据量评估。
---
# SQL 评审(事前)
> **本文件是骨架**:`slot:` 注释必须在安装时用项目事实替换,替换不到就整条删除。
> `harness:package` 区块是包内通用内容(升级整体替换),`harness:project` 区块是项目内容(升级永不触碰)。
## 何时用
- 新增 / 修改查询、关联、子查询、排序
- 改分页、改批量写入、循环里调数据库
- 给表加字段、加索引、改唯一约束
- 接口新增按时间 / 状态 / 组织等维度过滤
> 改代码时用本技能;线上已经变慢的排查用 `trouble-shooting/01-slow-sql`。
<!-- harness:project:begin bindings -->
## 项目绑定(编译时替换)
- 数据源与方言(Oracle / MySQL / MSSQL / PG):<!-- slot:db.datasources -->
- SQL 写在哪里(ORM / 手写 / 视图 / XML):<!-- slot:db.sql.location -->
- 表行数与索引的查询方式(客户端 / MCP / 平台):<!-- slot:db.catalog -->
- 只读执行 / EXPLAIN 的 MCP 或客户端:<!-- slot:db.mcp -->
- 只读沙箱或测试库(可跑 fixture):<!-- slot:db.sandbox -->
- 事务与批量写入的约定:<!-- slot:db.batch -->
<!-- harness:project:end -->
<!-- harness:package:begin mode -->
## 模式(跨技术栈不变)
### 步骤
1. **定位 SQL 与数据源**:哪个 module、哪次连接、命中哪张表
2. **估数据量**:查 count 或表统计;大表(> 10 万行)必须确认有可用索引
3. **看执行计划**:`EXPLAIN` / `explain plan` / `showplan`
4. **核对索引**:where / join / order by 列是否有索引;注意隐式转换与函数包列
5. **改写法**:前导通配改前缀、`not in` 改 `not exists`、深分页改游标、批量改分批
6. **写结论**:`summary.md` 加 `sql_review:` 一行(正确性四层 + 表、预估行数、访问路径)
### 正确性校验(先证明对,再评估快)
> 性能评审之前先回答:这条 SQL 会不会错。**没有正确性结论,不允许写"通过"。**
| 层 | 问题 | 判据 | 手段(见「项目绑定」) |
| --- | --- | --- | --- |
| 1 语法 / 方言 | 目标数据库能不能解析 | parse 或 `EXPLAIN` / `PREPARE` 不报语法错;方言一致 | `db.mcp` 的只读执行方式 |
| 2 结构 | 表、列、类型、约束是否存在 | 对照 `.harness/wiki/数据模型.md` 或查询 catalog | `db.catalog` 的查询方式 |
| 3 绑定 | 参数是否安全、数量与类型是否匹配 | 只用 `#{}`,无 `${}`;动态列走白名单;NULL / IN / 排序已处理 | 代码检索 |
| 4 结果 | 语义是否符合预期 | 只读沙箱用代表性参数或 fixture 跑一次,断言列、行数与关键值;写操作事务回滚或 dry-run | `db.sandbox` 的只读沙箱 |
**步骤:**
1. **还原可执行 SQL**:按本次真实分支渲染动态 SQL,记录参数集;还原不了就列出分支与参数,不能跳过。
2. **先解析**:用只读账号跑 `EXPLAIN` 或 `PREPARE`;语法错、对象不存在会在这里暴露。
3. **核对结构**:表名、列名、类型、唯一键;以项目 wiki / 迁移脚本为准,不拿测试库结构当生产。
4. **核对绑定**:参数个数、类型、空值、`IN` 列表、`ORDER BY` 白名单;禁止 `${}` 拼接。
5. **跑结果(只读)**:用 fixture 或代表性参数执行,断言返回列、行数与关键值;写操作在事务里执行后回滚,或只做 dry-run。
6. **跑计划**:`EXPLAIN` 看 `type` / `key` / `rows` / `Extra`;大表、深分页、批量写单独评估。
7. **写结论**:`sql_review:` 必须含四层结论;**无法验证的层写“未验证 + 原因”,不能写“通过”**。
**停止条件:**
- 没有只读库 / 没有 fixture / 生产数据不可用 → 只能写“未验证 + 原因”;DDL、大表、批量写等高风险 SQL 必须人工确认后才能合并
- 动态 SQL 无法还原 → 至少覆盖本次实际路径,并列出未覆盖分支
- 结果与预期不一致 → 不进阶段 5,先改 SQL 或改预期
**结论模板:**
```text
sql_review: <表名> · 语法 <OK/失败> · 结构 <OK/缺 X> · 绑定 <#{} / 无 ${}> · 结果 <已验(fixture/参数) / 未验证(原因)> · 计划 <type=ref key=idx_x rows≈N / 未跑> · 结论 <通过 / 已改写 / 待 DBA>
```
**证据:** 优先落盘到 `.harness/changes/<特性>/sql-review/`(EXPLAIN 输出、结果摘要、fixture 路径),在 `sql_review:` 里引用路径。
### 检查项
- [ ] SQL 只用一种数据源方言
- [ ] where 列无函数、无隐式类型转换
- [ ] 没有 `select *`
- [ ] 没有前导通配 `like '%x'`
- [ ] `not in` 已改 `not exists`,或确认子查询无空值
- [ ] 分页不是大 offset(> 1 万行考虑游标 / 延迟关联)
- [ ] 批量写入已分批,没有循环单条 insert
- [ ] SQL 能在目标方言解析(`EXPLAIN` / `PREPARE` 不报语法错)
- [ ] 表、列、类型、约束存在,且与项目 wiki / 迁移脚本一致
- [ ] 参数只用 `#{}`,无 `${}`,个数与类型匹配
- [ ] 只读查询用代表性参数或 fixture 跑过,结果符合预期;写操作已事务回滚或 dry-run
- [ ] 已给出执行计划或预估行数
- [ ] 无法验证的层已写“未验证 + 原因”,没有写成“通过”
### 常见错误
- 凭"应该走索引"下结论,没跑执行计划
- 只写“通过”,没跑过任何一次解析或结果验证
- 拿测试库结构当生产结构
- 一种数据库的写法直接搬到另一种
- 加索引不看写入成本与选择性
- 在索引列上套函数导致失效
<!-- harness:package:end -->
<!-- harness:project:begin evidence -->
## 证据锚点(编译后填写)
<!-- slot:evidence - 至少一条 - [E2] 真实文件路径 :: 证明了什么 -->
<!-- harness:project:end -->
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · fengyale/harness-bootstrap | 91 | MIT |
| sumankrsh/enterprise-data-skills | 100 | MIT |
| claude-dev-suite/claude-dev-suite | 95 | MIT |
Database snapshot: 13.11.2026 · package version 7fb55effca5d (SHA-256 digest of the entire package)
Downloads in the last 30 days: 3 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100fai-sql-optimization-skill
Optimize SQL queries with execution plan analysis, index tuning, query rewriting, and statistics management. Use when diagnosing slow queries across PostgreSQL, SQL Server, or MySQL. - 100nl2sql
自然语言转 SQL 的取数能力,只承接**纯取数**请求(用户只要数据或 SQL,不要分析结论)。 输入一句问话,在当前工作空间绑定的业务库上走三段:备料(召回表 + 定表 + 采维值)→ 写 SQL → 跑 SQL,交回 SQL 文本与结果(导出 CSV 文件 + 头部若干行)。所有元数据与 数据访问都经 dw-db-connector 走 web API,脚本不直连数据库。维值在备料阶段只对 选定表做轻量采样;执行结果为空时再按 SQL 里的过滤列补采核对。 **排他边界**:问句含任何分析意图(分析/对比/统计/分布/占比/排名/趋势/报告/… - 100optimizing-sql-queries
Execute use when you need to work with query optimization. This skill provides query performance analysis with comprehensive guidance and automation. Trigger with phrases like "optimize queries", "analyze performance", or "improve query speed". - 100sql
Expert SQL assistance covering ANSI SQL, CTEs, Window Functions, aggregation, indexing, and query optimization. Use when writing complex analytical queries, tuning query execution plans, or designing databases. - 100sqlserver
Expert Microsoft SQL Server assistance covering T-SQL, execution plans, index tuning, Always On availability groups, and DMV profiling. Use when developing enterprise .NET applications, administering SQL Server, or optimizing T-SQL. - 90sql-expert
Expert system for generating, validating, and optimizing ClickHouse SQL. Use this when the user needs data, queries, or analysis.