Data-engineering-skills optimizing-query-text
git clone https://github.com/AltimateAI/data-engineering-skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/AltimateAI/data-engineering-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/snowflake/optimizing-query-text" ~/.claude/skills/altimateai-data-engineering-skills-optimizing-query-text && rm -rf "$T"
skills/snowflake/optimizing-query-text/SKILL.mdOptimize Query from SQL Text
OUTPUT FORMAT
Return ONLY the optimized SQL query. No markdown formatting, no explanations, no bullet points - just pure SQL that can be executed directly in Snowflake.
CRITICAL: Semantic Preservation Rules
The optimized query MUST return IDENTICAL results to the original.
Before returning ANY optimization, verify:
- Same columns: Exact same columns in exact same order with exact same aliases
- Same rows: Filter conditions must be semantically equivalent
- Same ordering: Preserve
exactly as writtenORDER BY - Same limits: If original has
, keepLIMIT N
. If no LIMIT, do NOT add one.LIMIT N
If you cannot guarantee identical results, return the original query unchanged.
Pattern 1: Function on Filter Column
Problem: Functions on columns in WHERE clause prevent partition pruning and index usage.
CAN Fix
| Original | Optimized | Why Safe |
|---|---|---|
| | Equivalent range |
| | Equivalent range |
| | Equivalent range |
| | Same boundaries |
| | Equivalent range |
CANNOT Fix
| Pattern | Why Not |
|---|---|
| Dynamic values, cannot precompute range |
| Comparing two columns, both need function |
| Day-of-week has no contiguous range |
in GROUP BY | Needed for grouping logic |
| Function in SELECT/GROUP BY is fine, only filter matters |
Pattern 2: Function on JOIN Column
Problem: Functions on JOIN columns prevent hash joins, forcing slower nested loop joins.
CAN Fix
| Original | Optimized | Why Safe |
|---|---|---|
| | If both are same type (e.g., INTEGER) |
| | If data is already consistently cased |
| | If data has no leading/trailing spaces |
CANNOT Fix
| Pattern | Why Not |
|---|---|
| Types genuinely differ, CAST required |
| Different granularity, DATE() required |
| If b.code might have different case |
| Arithmetic transformation, cannot remove |
Pattern 3: NOT IN Subquery
Problem:
NOT IN has poor performance and unexpected NULL behavior.
CAN Fix
| Original | Optimized | Why Safe |
|---|---|---|
| | Equivalent when subquery column is NOT NULL |
where id has NOT NULL constraint | | NOT NULL guarantees equivalence |
CANNOT Fix
| Pattern | Why Not |
|---|---|
| If subquery returns NULL, NOT IN returns no rows; NOT EXISTS doesn't |
| Multi-column NOT IN has complex NULL semantics |
Key Rule: Only convert NOT IN to NOT EXISTS if you can verify the subquery column cannot be NULL.
Pattern 4: Repeated Subquery
Problem: Same subquery executed multiple times causes redundant scans.
CAN Fix
| Original | Optimized |
|---|---|
| Subquery appears 2+ times identically | Extract to CTE, reference CTE multiple times |
| Same aggregation used in multiple places | Compute once in CTE |
CANNOT Fix
| Pattern | Why Not |
|---|---|
| Correlated subquery (references outer table) | Each execution is different, cannot cache |
| Subqueries with different filters | Not actually the same subquery |
| Subquery in SELECT that depends on current row | Correlation prevents extraction |
Pattern 5: Implicit Comma Joins
Problem: Comma-separated tables in FROM clause are harder to read and optimize.
CAN Fix - Always
Convert
FROM a, b, c WHERE a.id = b.id AND b.id = c.id to explicit JOIN syntax.
This is always safe - just restructuring, no semantic change.
UNSAFE Optimizations (NEVER apply)
- UNION to UNION ALL: UNION deduplicates rows, UNION ALL does not - different results
- Changing window functions: Do not modify
or similar nested aggregatesSUM(SUM(x)) OVER(...) - Adding redundant filters: Do not add filters in JOIN ON if same filter exists in WHERE
- Changing column names: Copy column names EXACTLY from original - do not "simplify" or rename
- Changing column aliases: Keep all aliases exactly as original
- Adding early filtering in JOINs: If a filter is in WHERE, do not duplicate it in JOIN ON clause
Principles
- Minimal changes: Make the fewest changes necessary. Simpler optimizations are more reliable.
- Preserve structure: Keep subqueries, CTEs, and overall query structure unless there's a clear benefit.
- When in doubt, don't: If unsure whether a change preserves semantics, skip it.
- Copy exactly: Column names, table aliases, and expressions should be copied character-for-character.
Priority Order
- Date/time functions on filter columns - Highest impact
- Implicit joins to explicit JOIN - Always safe, improves readability
- NOT IN to NOT EXISTS - Only if NULL-safe
Requirements
- Results must be identical: Same rows, same columns, same order
- Valid Snowflake SQL: Output must execute without errors in Snowflake