Optimize and rewrite Hive + Spark 3.2 SQL with correctness checks, logic refinement, performance tuning, and readability improvements. Use when asked to review or optimize SQL (for example, "optimize this SQL", "优化这个 SQL"), especially for ETL pipelines, analytics queries, slow jobs, correctness risks, or when rewritten SQL with risk/benefit analysis is required.
Follow a staged workflow to diagnose and rewrite SQL while balancing quality and performance. Prioritize decisions in this order: correctness, explainability, performance gain, rewrite scope.
Read only what is needed:
references/rules.md for baseline rules and gating.references/patterns.md for anti-pattern rewrites.references/output-template.md for the default concise report format.references/output-template-full.md only when the user explicitly requests a detailed report.Default mode:
references/output-template.md (Concise v2 four-section format).TL;DR section before the core sections.Detailed mode triggers:
references/output-template-full.md.TL;DR section before the detailed sections.Conflict handling:
Default rules:
M01... for major rewrites, S01... for minor rewrites.改了什么) or SQL inline comments.Major rewrite triggers (any one is enough):
>= 2.>= 20%.Major rewrite rendering:
-- [Mxx] <中文短句:改动点 + 目的>Mxx summary comment and explain scope under 改了什么.Minor rewrite rendering:
-- [Sxx] <中文短句:改动点 + 目的>改了什么No-major handling:
改了什么 or 有什么影响.Mark SQL as high when any condition is true:
UNION or UNION ALL.When SQL is high complexity, return:
Use medium-constraint semantics protection:
Use this decision priority:
## Risk and Benefit Gate; keep conservative or suggestion-only output when the gate is not met.references/output-template.md.references/output-template-full.md only when the user explicitly requests detailed output.TL;DR and include three non-empty bullets: 主要改动, 核心收益, and 风险结论.change_id mapping for all material edits, even when no standalone major map section is used.references/rules.md as the single source of truth.references/patterns.md.SKILL.md owns workflow, complexity routing, and hard-constraint summaries only.references/rules.md owns detailed rewrite conventions and style/performance rules.references/patterns.md owns anti-pattern rewrite strategies.SKILL.md summary-level to avoid duplication drift.