ワンクリックで
query-optimize
Analyze and optimize SQL queries for better performance
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Analyze and optimize SQL queries for better performance
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
Cloudflare-style AI code review for dbt/SQL pull requests. Produces a signed APPROVE/COMMENT/REQUEST_CHANGES verdict where every blocking finding is backed by a deterministic engine call — column-lineage blast radius, query equivalence, PII classification, and A–F grade. Use to review a dbt PR or the working-tree changes before merge.
REQUIRED before writing or modifying ANY dbt model. Invoke this skill FIRST whenever a task says "create", "build", "add", "modify", "update", "fix", or "refactor" a dbt model, staging file, mart, incremental, or snapshot. Skipping this skill is the leading cause of silent-correctness bugs — models that compile and `dbt build` cleanly but produce wrong values. It contains the patterns that prevent the most common such bugs encountered in real dbt projects: • Incremental high-water marks (`>=` vs `>` ties → silent row dropout) • Snapshot strategy selection (timestamp vs check, `unique_key` choice) • `LEFT JOIN + COUNT(*)` phantom rows from unmatched parents • Type harmonization in `COALESCE` / `CASE` / `UNION` legs • Date-spine completeness (every period present, even empty ones) • Off-by-one window boundaries (`BETWEEN d - (N-1) AND d` for N-wide) • Uniqueness enforcement when schema implies a key • Window-function `LIMIT` with deterministic tiebreaker • Verifying transformation correctness with dbt unit te
REQUIRED after building or modifying ANY dbt model that has columns declared in `schema.yml` / `_models.yml`. Run `altimate-dbt schema-verify --model <name>` to diff actual columns against the spec, and treat any `mismatch` verdict as "not done." The most common reason "the build is green but the tests still fail" is that the model produces the right *data values* in the wrong *column shape* — extra columns, missing columns, wrong order, wrong types. Many dbt equality tests grade the column tuple `(name, type, position)` exactly, and the agent's prior bias is to add "helpful" extras (`p1`/`p2`/`p3` rank breakdowns, name-resolved variants, lineage metadata) or reorder columns "more logically." Both break the contract. This skill enforces the mechanical check that catches those bugs before declaring done. Use it before declaring any model task complete.
Generate dbt unit tests automatically for any model. Analyzes SQL logic (CASE/WHEN, JOINs, window functions, NULLs), creates type-correct mock inputs from manifest schema, and assembles complete YAML. Use when a user says "generate tests", "add unit tests", "test this model", or "test coverage" for dbt models.
Validate that two tables or query results are identical — or diagnose exactly how they differ. Discover schema, identify keys, profile cheaply, then diff. Use for migration validation, ETL regression, and query refactor verification.
Add schema tests, unit tests, and data quality checks to dbt models. Use when validating data integrity, adding test definitions to schema.yml, writing unit tests, or practicing test-driven development in dbt. Powered by altimate-dbt.
| name | query-optimize |
| description | Analyze and optimize SQL queries for better performance |
Agent: any (read-only analysis)
Tools used: altimate_core_rewrite (with verify_equivalence: true), sql_analyze, sql_explain, read, glob, schema_inspect, warehouse_list
Analyze SQL queries for performance issues and suggest concrete optimizations including rewritten SQL.
Get the SQL query -- Either:
Determine the dialect -- Default to snowflake. If the user specifies a dialect (postgres, bigquery, duckdb, etc.), use that instead. Check the project for warehouse connections using warehouse_list if unsure.
Run the verified optimizer:
schema_inspect on the relevant tables to build schema context (needed both for better rewrites — e.g. SELECT * expansion — and to verify equivalence)altimate_core_rewrite with the SQL, schema context, and verify_equivalence: true. This proposes rewrites AND proves each one returns the same results as the original in a single step. The result is partitioned into verified-equivalent rewrites (safe to apply) and unverified rewrites (review before applying), so you never recommend a rewrite that silently changes semantics.Run detailed analysis:
sql_analyze with the same SQL and dialect to get the full anti-pattern breakdown with recommendationsGet execution plan (if warehouse connected):
sql_explain to run EXPLAIN on the query and get the execution planEquivalence verification is built into step 3 (verify_equivalence: true):
Present findings in a structured format:
Query Optimization Report
=========================
Summary: X suggestions found, Y anti-patterns detected
High Impact:
1. [REWRITE] Replace SELECT * with explicit columns
Before: SELECT *
After: SELECT id, name, email
2. [REWRITE] Use UNION ALL instead of UNION
Before: ... UNION ...
After: ... UNION ALL ...
Medium Impact:
3. [PERFORMANCE] Add LIMIT to ORDER BY
...
Optimized SQL:
--------------
SELECT id, name, email
FROM users
WHERE status = 'active'
ORDER BY name
LIMIT 100
Anti-Pattern Details:
---------------------
[WARNING] SELECT_STAR: Query uses SELECT * ...
-> Consider selecting only the columns you need.
If schema context is available, mention that the optimization used real table schemas for more accurate suggestions (e.g., expanding SELECT * to actual columns).
If no issues are found, confirm the query looks well-optimized and briefly explain why (no anti-patterns, proper use of limits, explicit columns, etc.).
The user invokes this skill with SQL or a file path:
/query-optimize SELECT * FROM users ORDER BY name -- Optimize inline SQL/query-optimize models/staging/stg_orders.sql -- Optimize SQL from a file/query-optimize -- Optimize the most recently discussed SQL in the conversationUse the tools: altimate_core_rewrite with verify_equivalence: true (proposes rewrites AND proves they preserve results in one step), sql_analyze, sql_explain (execution plans), read (for file-based SQL), glob (to find SQL files), schema_inspect (for schema context), warehouse_list (to check connections).