sql-optimizer
Skill2 starsChanged 9 days agoMITView on GitHub
SQL 性能优化审查技能。对已有 SQL(草稿/线上版/ETL 脚本)做性能扫描, 按第〇层判断 + 六层框架逐项检查并给出输出等价的改写建议。 DO trigger when 用户说"优化这个SQL"、"SQL 跑得慢"、"能不能提速"、 "帮我优化"、"提前过滤"、"性能审查"、"减少扫描量"、 要求对已有 SQL 做性能层面的改写。 Do NOT trigger for 从需求文档开发新 SQL(使用 /data-developer)、 对比线上/变更版本的口径审查(使用 /aqueduct-review)、 交付后的需求变更管理(使用 /change-management)、 排查数据异常/落表不对(性能正常,属于数据排查场景)、 仅校验 SQL 规范(关键字小写/竖排等,属于 data-developer 内置校验)。
11 other projects use a version of this.
Adopted by
Most recently taken up by
- divik-09/claude-skills-collectiontheir own version, first seen Nov 29, 2026
- imtiazrayhan/agentscamp-librarytheir own version, first seen Nov 16, 2026
- jaskirat1616/grok-skillstheir own version, first seen Nov 1, 2026
- Mathews-Tom/armorytheir own version, first seen Nov 1, 2026
- milad-ahmd/agent-skillstheir own version, first seen Nov 1, 2026
- JayRHa/agent-skillstheir own version, first seen Oct 15, 2026
Used with
Projects with it also use changelog-keeper, 0-template and 10 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
JayRHaSkillchangelog-keeper
Maintains a CHANGELOG.md in the Keep a Changelog format with Semantic Versioning, grouping user-facing entries under Added/Changed/Deprecated/Removed/Fixed/Security and cutting dated releases from an Unreleased section. Use this skill when the user asks to "update the changelog", "add a changelog entry", "cut a release", "bump the version", "what changed since the last release", or to create/clean up a CHANGELOG.md or release notes.
Also in 4 projects that use this
JayRHaSkill0-template
Template for creating a new Agent Skill. Copy this folder, rename it to your skill's kebab-case name, and fill in every section. Use this as the starting point whenever you author a new skill for the AgentSkills library.
Also in 3 projects that use this
MehdibargachSkillokr-writer
Turn business goals into structured OKRs with measurable Key Results, linked initiatives, alignment check, and anti-goals. Takes a strategic priority and outputs ready-to-use OKRs.
Also in 4 projects that use this
peopleforresterSkillrefactoring-guide
Safe refactoring patterns and techniques. Use when the user wants to improve code structure, reduce duplication, or make code more maintainable without changing behavior. Emphasizes incremental changes with test coverage.
Also in 3 projects that use this
xmqywxSkillmeeting-summarizer
Transform meeting transcripts into structured summaries with action items, decisions, follow-ups, and efficiency analysis. Works with any meeting format.
Also in 3 projects that use this
- +7 more from JayRHa
Other versions11 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 11
- Mathews-Tom/armory★ 340same name
- jaskirat1616/grok-skills★ 22same name
- JayRHa/agent-skills★ 12same name
- JayRHa/AgentSkills★ 12same name
- milad-ahmd/agent-skills★ 7same name
- juanlurg/data-science-claude-skills★ 3same name
- BleckWolf25/AIToolkit★ 1same name
- divik-09/claude-skills-collection★ 1same name
- imtiazrayhan/agentscamp-library★ 1same name
- minagayid/Agents★ 1same name
1 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository6 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- aqueduct-devSkill
- aqueduct-reviewSkill
- aqueductSkill
- change-managementSkill
- data-developerSkill
The file
Files
SQL 性能优化技能 (SQL Performance Optimizer Skill)
对已有 SQL(草稿 / 线上版 / ETL 脚本)做性能扫描,按"第〇层判断 + 六层框架"逐项检查, 给出输出严格等价的改写建议,必要时直接改写。
排除场景 (Skip)
- 从需求文档开发新 SQL(使用 /data-developer 技能)
- 对比线上/变更版本的口径审查(使用 /aqueduct-review)
- 交付后的需求变更管理(使用 /change-management 技能)
- 排查数据异常 / 落表不对(性能正常,属于数据排查场景)
- 仅校验 SQL 规范(关键字小写 / 竖排等,属于 data-developer 内置校验)
核心心法 (不可违背)
优化不是"凭感觉提前过滤",而是"证明被裁的行/被省的计算在下游必然不被消费"。
每个优化动作必须能写出等价性论证:被裁的行在下游 join/where 中必然被丢弃或取不到, 故输出逐行相等。写不出论证的优化不做,宁可保持原样。
口径零变更原则:指标公式、人群定义、窗口定义、兜底逻辑等业务口径一个字符不动。 只动"读什么、算多少、算几次",不动"算什么"。
执行流程
Step 1: 通读 SQL,建立清单
读入目标 SQL,先扫描并记录(不改):
- 每个子查询/CTE 的消费方:被谁 join、join 键是什么格式(lpad/split/大小写)
- 每张事实表的分区条件:上下界是否闭合、是否扫了不会被用的历史
- 重复逻辑:相同/近似的子查询块出现几次
- 人群来源:最终输出的人群是谁(哪个 inner join / where 决定的)
Step 2: 第〇层判断——冗余归属哪一层(gate)
SQL 优化只解"一个脚本内"的重复;很多冗余根源在数仓设计层。先分类,再决定动不动:
| 冗余类型 | 判断信号 | 处理 |
| ① 任务内冗余 | 同一脚本内相同/近似逻辑出现 ≥2 次 | SQL 层可解 → 走六层框架(抽 TMP/合并) |
| ② 跨任务冗余 | 逻辑在本任务之外也有别家实现(如同一维表标记在多个任务各算一遍) | 设计层才能根治 → 输出上浮建议(建 dim/DWS 共享表),SQL 层优化只作缓解 |
| ③ 结构性必要冗余 | 多个 join 各 serve 不同列组、键格式互不相同(宽表装配固有形态) | 不消除——强行合并制造耦合,标注"结构性必要" |
上浮建表的判据(②类何时值得建层表):
- 消费方 ≥2 个任务、逻辑变更需多处同步、单次计算成本高
- 反面约束:依赖链变长(上游无实例可能空放行)、中间表须落正式表(临时表有生命周期治理会被删)、各任务自行推导易口径漂移
正面样板:若发现某个"重复聚合"其实有层表可读(如已物化的指标汇总表),说明设计层已做过这件事,SQL 只是消费方——不要重算,直接用。
Step 3: 按六层框架逐层扫描(仅 ① 类)
一、扫描层(读什么 — 成本大头,优先级最高)
| # | 方法 | 判断信号 | 做法 |
| 1 | 分区上下界闭合 | 事实表只有 <= 没有下界 | 业务窗口能推出下界就补上 |
| 2 | 动态下界 | 多人各有窗口,逐人裁需 join 后才能做 | 全人群窗口下界取 min() 做全局下界,先裁再 join |
| 3 | 列裁剪 | 子查询 select 了下游不用的宽表冷列 | 只投影下游用到的列(parquet 列存收益直接) |
| 4 | 谓词下推 | JOIN 后 WHERE 过滤右表字段 | 过滤推进右表子查询;LEFT JOIN + 右表过滤 → 改 INNER JOIN |
二、数据量层(算多少)
| # | 方法 | 判断信号 | 做法 |
| 5 | 按消费端收窄 | TMP/CTE 输出只被某个人群 left join 消费 | 人群收窄到消费方(先证明 join 键 + 消费方人群 ⊆ 收窄后人群) |
| 6 | 半连接提前收窄 | 大事实表先全量算再 join 人群 | exists (人群TMP) 先裁再 join,shuffle 降到人群规模 |
| 7 | 分支消费收窄 | 某标记只在某 CASE 分支被读 | 只算会被分支消费的行 |
| 8 | 先过滤再 JOIN | 直接拿原表 join | 派生表模式:子查询内先 WHERE 再 join,防数据膨胀 |
三、重复层(算几次)
| # | 方法 | 判断信号 | 做法 |
| 9 | 公共逻辑抽 TMP | 相同/近似逻辑出现 ≥2 次 | 抽成独立 TMP 算一次;同源靠结构保证,不靠复制粘贴 |
| 10 | 同表多用途合并 | 同一张表读多次、不同条件聚合 | 合并为一个子查询,CASE WHEN 区分用途 |
| 11 | 死条件删除 | CASE/join 里条件恒真(上游已保证) | 删,避免误导后来读者以为有分支逻辑 |
四、算子层(怎么算)
| # | 方法 | 判断信号 | 做法 |
| 12 | join 键格式对齐 | lpad/split 一侧加一侧不加 | 键变换提前做进 TMP,避免每处 join 重算 |
| 13 | 防膨胀 | 1人N行展开 + 聚合 | count(distinct) 天然防膨胀;collect_list 必须先 select distinct |
| 14 | 窗口函数 | 取每组 top-N 用自 join | row_number 窗口一次搞定 |
| 15 | 动态分区落点 | union 多分支 insert | 逐分支核对分区键取值与落点分区 |
五、防错层(优化时不引入 bug — 优化改动特有陷阱)
| # | 陷阱 | 检查方法 |
| P1 | inner join 改变人群 | 确认裁掉的行真的不被消费 |
| P2 | 键格式单边 | 半连接条件的 lpad 必须与原 join 键逐一对应 |
| P3 | 空集边界 | min() 返回 NULL 时下界条件失效 → 确认空人群走"全滤空"与原逻辑等价 |
| P4 | 字段来源张冠李戴 | 改读 TMP 后别名变了,每个字段迁移后核对来源(如 hire_dt 误写成 cncl_dt) |
| P5 | group by 遗漏 | 改写聚合查询时核对 select 的非聚合列是否都在 group by 里 |
六、验证层(怎么证明等价)
| # | 验证手段 | 适用 |
| V1 | 纸面论证 | 每个裁剪点写"被裁的行在下游必然被丢弃或取不到" |
| V2 | 机械检查 | 括号配平、残留旧引用 grep、字段来源核对 |
| V3 | 数字对比 | 改前改后各跑一次 count(*) / sum(关键指标) / 分状态计数 三数全等 |
| V4 | 平台实测 | dry-run 卡点 + 实测编译(本地静态校验逮不住的语义问题靠引擎编译兜底) |
Step 4: 输出优化报告
按固定格式给用户报告,每条建议包含:
[层级] 方法名
判断信号:(原 SQL 里看到的症状)
改法:(具体怎么改)
等价性论证:(为什么输出不变)
预估收益:(高/中/低)报告末尾给出优先级排序:分区下界 > 事实表收窄 > 抽TMP > 人群收窄 > 谓词下推 > 算子微调。 (扫描裁剪永远是第一杠杆——少读一个分区胜过所有算子优化。)
②类跨任务冗余在报告中单列一节"上浮建议",描述建议物化的层表、判据满足情况、反面约束风险,不直接动手建表。
Step 5: 改写执行(用户确认后)
用户确认后执行改写,纪律:
- 一次只改一类:每类优化单独一个编辑操作,失败可单独回滚
- 注释标注:改写处加
-- [opt]标记 + 简述,便于 review diff - 改前备份口径清单:列出所有口径相关条件(CASE 分支、窗口定义、兜底 coalesce),改后逐一核对未动
- 字段迁移核对:每改一处读 TMP 的子查询,逐字段核对来源列
- 机械验证:括号配平 + 残留旧引用 grep(旧表名/旧别名是否清干净)
- 全文复核:用 cat 而非 Read 工具(Read 可能返回过期内容)
- 实测编译:新写的 SQL 段(尤其标量子查询/CTE 引用)先平台编译验证再交付
边界(明确不做)
- 不改业务口径:指标公式、人群定义、窗口定义、兜底逻辑都不动
- 不做激进修改:不合并 STEP 改变执行顺序语义、不改表落点/分区键
- 不确定不裁:写不出等价性论证的行不裁,报告里标注"疑似可裁但需业务确认"
- 不代替审查:口径正确性审查属于 /aqueduct-review,本技能只管性能
与其他技能的关系
data-developer (首次开发产出 SQL)
↓ 需要提速时
sql-optimizer (本技能, 性能审查改写)
↓ 改写完成
data-developer Phase 4.5 (代码审查模式, 口径复核)Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

