Make queries prune — clustering keys and scan diagnosis
Choose a clustering key from the predicates of your most expensive queries, prove the prune with partitions_scanned versus partitions_total, and fix the disease you actually have — a lost predicate or a low-selectivity key on the scan side, or queue time on the compute side.
At a glance
| When | A dashboard or model scans far more partitions than it needs; Choosing the first clustering key for a large fact table |
|---|---|
| Where | dbt |
| Why | Pruning is the single biggest lever on warehouse cost and latency, and it is decided by one key and the predicates that use it. |
Learning objectives
- Choose a clustering key from predicate selectivity
- Prove pruning with partitions_scanned versus partitions_total
- Fix the scan or compute problem you actually measured
What you need
- A shared warehouse with a query history you can read
- Install: npx de-agent-platform add warehouse-clustering-advisor
- Your top-cost queries and their WHERE clauses
Steps
- Pick the clustering column from the workload — the one that appears in the WHERE clause of the most expensive queries, with reasonable cardinality; order matters, so put the most selective first. (from skill-warehouse-clustering-advisor )
- Know the failure mode — clustering on a low-selectivity key stops pruning entirely. (from runbook-warehouse-query-slow )
- Diagnose before changing anything — separate scan cost from compute time (bytes/partitions scanned vs queue time) and compare the plan against the clustering/partition key. (from runbook-warehouse-query-slow )
- Measure the prune directly — query_id, partitions_scanned, partitions_total, and bytes_scanned from the session's query history. (from runbook-warehouse-query-slow )
- Match the fix to the finding — over-scan means fixing the predicate or adding/adjusting the clustering key; rebuilding clustering on a low-selectivity key. (from runbook-warehouse-query-slow )
- Keep it fixed — alert on the partitions_scanned/partitions_total ratio and review query history weekly for the top cost queries. (from runbook-warehouse-query-slow )
Check yourself
- Your clustering key's first column is the most selective predicate of your costliest queries.
- You can quote partitions_scanned/partitions_total for a query before and after the change.
- Scan-bound and queue-bound queries get different fixes, and you can name which yours is.
Worked example
Prove the prune from query history — from runbook-warehouse-query-slow
-- Snowflake
SELECT query_id, partitions_scanned, partitions_total, bytes_scanned
FROM table(information_schema.query_history_by_session())
ORDER BY start_time DESC LIMIT 10;
Clustering key choice, from the advisor — from skill-warehouse-clustering-advisor
Pick the column that appears in the WHERE clause of the *most expensive*
queries, with reasonable cardinality. Order matters: put the most
selective first.
Sourced from
Security scan
Last sweep 2026-10-24: 0 finding(s), 0 marked as examples.
Common gotchas
- Clustering on a low-selectivity key stops pruning entirely.
- Scan is optimal but the query waits → warehouse sizing or concurrency problem.
- Bytes scanned and queue time are different diseases — fix the predicate vs add clusters; sizing up a scan-bound query buys nothing.
Production considerations
- Alert on the partitions_scanned/partitions_total ratio.
- Review weekly query history for the top-cost queries.
Diagram
clustering and pruning
|
|- most expensive queries --> WHERE columns --> cardinality --> key order
|
|- query history --> partitions_scanned / partitions_total --> prune proof
|
|- over-scan --> fix predicate / adjust key
|
|- waits with optimal scan --> sizing or concurrency, not the key
Registry stats
| Verification | community · Oct 23, 2026 |
|---|---|
| Completeness | 81 |
| Trust multiplier | ×1.25 |
| Composite | 34.15 · adequate |
| Freshness | 1 days since last source check |
| Difficulty | intermediate |
| Version | 0.1.0 |
| License | CC-BY-4.0 |
| Security flags | none |
Did this solve your problem?
Make queries prune — clustering keys and scan diagnosis