warehouse-query-optimization — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited warehouse-query-optimization (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
Use when a Snowflake query is too slow, too expensive, or both. Triggers:
Default to Snowflake. For BigQuery / Postgres / Redshift, the principles transfer but specific syntax/internals differ — see reference.md.
| Input | Why it matters |
|---|---|
| Query text | What to optimize |
| Query history info | Run time, MB scanned, warehouse size — use query_history |
| Table sizes / partitioning | Whether clustering helps |
| Frequency of use | Is it run once (don't over-optimize) or hourly (very worth it) |
| Acceptable runtime / cost | The bar to clear |
select * from snowflake.account_usage.query_history
where query_id = '<id>';Is the query scanning more partitions than needed?
WHERE date_col >= '...' on a clustering key.SELECT * from a 10TB table is almost always wrong.
Largest table on the left, smallest hash table on the right.
Is the WHERE clause applied early or late?
Snowflake doesn't always materialize CTEs. A CTE used twice may be computed twice.
OVER (PARTITION BY user_id) on 1B rows may spill.
Is the warehouse undersized for the query?
# Query Optimization: <description>
## Original query stats
- Run time: 14.2s
- Partitions scanned: 1,840 / 2,100 (88%)
- Bytes scanned: 4.2 GB
- Bytes spilled (remote): 612 MB
- Warehouse: M
## Diagnosis
- **Primary issue:** Window function over 800M rows spills to remote storage
- **Secondary:** WHERE filter on event_date applied after window — should be pushed down
- **Minor:** SELECT * pulls 47 columns; only 6 are used downstream
## Optimized query
\`\`\`sql
-- [optimized SQL here]
\`\`\`
## Key changes
1. Pre-filtered to last 30 days before the window function (10× row reduction)
2. Selected only 6 needed columns instead of *
3. Replaced `qualify row_number() over (...) = 1` with `argmax`-equivalent pattern
## New query stats
- Run time: 1.4s (10× faster)
- Partitions scanned: 64 / 2,100 (3%)
- Bytes scanned: 124 MB (34× less)
- Bytes spilled: 0
- Warehouse: M (no upsize needed)
## Cost impact
- Original: ~0.04 credits/run × 24 runs/day = 1.0 credits/day
- Optimized: ~0.004 credits/run × 24 runs/day = 0.1 credits/day
- Savings: ~0.9 credits/day = ~$650/year at $2/credit
## Caveats
- Optimization assumes event_date filter is acceptable. If full history is needed, fix doesn't apply.
- 0 spillage assumes M warehouse with current data volume. If table grows 5×, may need to revisit.bytes_scanned_from_cache. First run cold can be much slower than subsequent warm runs.TEMP TABLE for expensive CTEs referenced multiple times.ROWS BETWEEN .... RANGE requires sort and is more expensive.1:N join without aggregation first inflates the right side. Look for "Cartesian product" or unexpectedly large intermediate result sets.scripts/profile_query.sql — Pull stats for a query_id from query_history.-- Get profile stats
select query_id, query_text, total_elapsed_time, bytes_scanned, bytes_spilled_to_remote_storage,
partitions_scanned, partitions_total, warehouse_size, credits_used_cloud_services
from snowflake.account_usage.query_history
where query_id = '<id>';sql-query-review — static review when no runtime profile is availablesql-correctness-review — verify the query is right before making it fastmodular-sql-ctes — well-structured SQL is also faster SQLdata-quality-audit — sometimes "slow" is "scanning too much because the table has dupes"metric-definition — pre-aggregating into a metric layer often beats optimizing ad-hoc queries~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.