From altimateai-data-engineering-skills
Fetches Snowflake query profile from a query ID, identifies bottlenecks, and returns an optimized SQL query with expected improvements. Activates when user provides a query_id or mentions slow query/optimization.
How this skill is triggered — by the user, by Claude, or both
Slash command
/altimateai-data-engineering-skills:optimizing-query-by-idThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
**Fetch query → Get profile → Apply best practices → Verify improvement → Return optimized query**
Fetch query → Get profile → Apply best practices → Verify improvement → Return optimized query
SELECT
query_id,
query_text,
total_elapsed_time/1000 as seconds,
bytes_scanned/1e9 as gb_scanned,
bytes_spilled_to_local_storage/1e9 as gb_spilled_local,
bytes_spilled_to_remote_storage/1e9 as gb_spilled_remote,
partitions_scanned,
partitions_total,
rows_produced
FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY())
WHERE query_id = '<query_id>';
Note the key metrics:
seconds: Total execution timegb_scanned: Data read (lower is better)gb_spilled: Spillage indicates memory pressurepartitions_scanned/total: Partition pruning effectiveness-- Get operator-level statistics
SELECT *
FROM TABLE(GET_QUERY_OPERATOR_STATS('<query_id>'));
Look for:
output_rows vs input_rows (explosions)Based on profile, look for:
| Metric | Issue | Fix |
|---|---|---|
| partitions_scanned = partitions_total | No pruning | Add filter on cluster key |
| gb_spilled > 0 | Memory pressure | Simplify query, increase warehouse |
| High bytes_scanned | Full scan | Add selective filters, reduce columns |
| Join explosion | Cartesian or bad key | Fix join condition, filter before join |
Rewrite the query:
EXPLAIN USING JSON
<optimized_query>;
Compare original vs optimized:
Provide:
Original Query Metrics:
Issues Found:
Optimized Query:
WITH filtered_events AS (
SELECT event_id, user_id, event_type, created_at
FROM events
WHERE created_at >= '2024-01-01'
AND created_at < '2024-02-01'
AND event_type = 'purchase'
)
SELECT fe.event_id, fe.created_at, u.name
FROM filtered_events fe
JOIN users u ON fe.user_id = u.id;
Changes:
Expected Improvement:
npx claudepluginhub joshuarweaver/cascade-featured --plugin altimateai-data-engineering-skillsOptimizes Snowflake queries using clustering keys, materialized views, caching, query profiling, and warehouse tuning. Identifies slow queries and bottlenecks via QUERY_HISTORY.
Optimizes Snowflake SQL query performance by eliminating common anti-patterns like functions on filter and join columns. Returns only the optimized SQL for direct execution.
Rewrites slow SQL queries for maximum performance using execution plan analysis, anti-pattern detection, and database-specific optimizations for PostgreSQL and MySQL.