Data Warehouse Optimizer
Optimize query performance and resource utilization in Snowflake, BigQuery, and Redshift through clustering, partitioning, materialized views, and query profiling.
Activation Triggers
Activate on: "Snowflake optimization", "BigQuery performance", "Redshift tuning", "query optimization", "clustering key", "partitioning", "materialized view", "warehouse sizing", "query profile", "slow query"
NOT for: dbt project structure → dbt-analytics-engineer | Dimensional modeling → dimensional-modeler | Cost optimization beyond warehouse → data-cost-optimizer
Quick Start
- Profile slow queries — use QUERY_PROFILE (Snowflake), INFORMATION_SCHEMA.JOBS (BigQuery), STL tables (Redshift)
- Partition large tables — by date column (most common), reducing scan size by 10-100x
- Add clustering — co-locate frequently filtered/joined columns within partitions
- Materialize expensive aggregations — materialized views for dashboards, pre-aggregated metrics
- Right-size warehouses — auto-suspend idle, auto-scale for concurrency, match size to workload
Core Capabilities
| Domain |
Technologies |
| Snowflake |
Micro-partitions, clustering keys, search optimization, warehouses |
| BigQuery |
Partitioning, clustering, BI Engine, materialized views |
| Redshift |
Sort keys, dist keys, VACUUM, WLM, Redshift Serverless |
| General |
Query plans, statistics, result caching, spill-to-disk analysis |
| Monitoring |
Snowflake Account Usage, BigQuery INFORMATION_SCHEMA, CloudWatch |
Architecture Patterns
Snowflake Clustering and Search Optimization
-- Cluster a large fact table by commonly filtered columns
ALTER TABLE fct_events
CLUSTER BY (event_date, customer_id);
-- Verify clustering depth (lower = better, target < 2.0)
SELECT SYSTEM$CLUSTERING_INFORMATION('fct_events', '(event_date, customer_id)');
-- Search optimization for point lookups on high-cardinality columns
ALTER TABLE fct_events ADD SEARCH OPTIMIZATION
ON EQUALITY(order_id), EQUALITY(email);
-- Result: range scans use clustering, point lookups use search optimization
BigQuery Partitioning + Clustering
-- Partition by date, cluster by high-cardinality filter columns
CREATE TABLE `project.dataset.fct_events`
PARTITION BY DATE(event_timestamp)
CLUSTER BY customer_id, event_type
AS
SELECT * FROM `project.dataset.raw_events`;
-- Query benefits: partition pruning + cluster pruning
-- Only scans partitions matching WHERE clause
SELECT customer_id, COUNT(*)
FROM `project.dataset.fct_events`
WHERE event_timestamp BETWEEN '2026-01-01' AND '2026-01-31'
AND event_type = 'purchase'
GROUP BY customer_id;
-- Check bytes scanned reduction
-- Target: 90%+ reduction vs unpartitioned table
Warehouse Sizing Strategy (Snowflake)
Workload Type Recommended Size Auto-Suspend Concurrency
───────────── ──────────────── ──────────── ───────────
Dashboard queries X-Small/Small 60s Auto-scale (max 3)
Analyst ad-hoc Medium 300s 1 cluster
dbt daily build Large Immediate 1 cluster
Data science / ML X-Large+ Immediate 1 cluster
Key: separate workloads into different warehouses
to prevent resource contention and enable per-workload billing
Anti-Patterns
- Scanning full tables — always partition by date; a full scan of a 1TB table costs 10-50x more than a pruned scan
- Too many clustering keys — 2-4 keys maximum; more keys reduce clustering effectiveness
- Oversized warehouses — bigger does not always mean faster; profile first, right-size second
- Ignoring spill-to-disk — queries spilling to remote storage are 10-100x slower; increase warehouse size or optimize query
- Materializing volatile data — materialized views on rapidly changing tables cause constant refresh overhead
Quality Checklist
1---2name: data-warehouse-optimizer3description: Snowflake, BigQuery, clustering, partitioning, and materialized views for warehouse performance. Activate on: Snowflake, BigQuery, Redshift, query optimization, clustering, partitioning, materialized view, warehouse cost, query profile. NOT for: dbt model structure (use dbt-analytics-engineer), data modeling (use dimensional-modeler).4license: Apache-2.05---6
7# Data Warehouse Optimizer
8
9Optimize query performance and resource utilization in Snowflake, BigQuery, and Redshift through clustering, partitioning, materialized views, and query profiling.
10
11## Activation Triggers
12
13**Activate on:** "Snowflake optimization", "BigQuery performance", "Redshift tuning", "query optimization", "clustering key", "partitioning", "materialized view", "warehouse sizing", "query profile", "slow query"
14
15**NOT for:** dbt project structure → `dbt-analytics-engineer` | Dimensional modeling → `dimensional-modeler` | Cost optimization beyond warehouse → `data-cost-optimizer`
16
17## Quick Start
18
191. **Profile slow queries** — use QUERY_PROFILE (Snowflake), INFORMATION_SCHEMA.JOBS (BigQuery), STL tables (Redshift)
202. **Partition large tables** — by date column (most common), reducing scan size by 10-100x
213. **Add clustering** — co-locate frequently filtered/joined columns within partitions
224. **Materialize expensive aggregations** — materialized views for dashboards, pre-aggregated metrics
235. **Right-size warehouses** — auto-suspend idle, auto-scale for concurrency, match size to workload
24
25## Core Capabilities
26
27| Domain | Technologies |
28|--------|-------------|
29| **Snowflake** | Micro-partitions, clustering keys, search optimization, warehouses |
30| **BigQuery** | Partitioning, clustering, BI Engine, materialized views |
31| **Redshift** | Sort keys, dist keys, VACUUM, WLM, Redshift Serverless |
32| **General** | Query plans, statistics, result caching, spill-to-disk analysis |
33| **Monitoring** | Snowflake Account Usage, BigQuery INFORMATION_SCHEMA, CloudWatch |
34
35## Architecture Patterns
36
37### Snowflake Clustering and Search Optimization
38
39```sql
40-- Cluster a large fact table by commonly filtered columns
41ALTER TABLE fct_events
42 CLUSTER BY (event_date, customer_id);
43
44-- Verify clustering depth (lower = better, target < 2.0)
45SELECT SYSTEM$CLUSTERING_INFORMATION('fct_events', '(event_date, customer_id)');
46
47-- Search optimization for point lookups on high-cardinality columns
48ALTER TABLE fct_events ADD SEARCH OPTIMIZATION
49 ON EQUALITY(order_id), EQUALITY(email);
50
51-- Result: range scans use clustering, point lookups use search optimization
52```
53
54### BigQuery Partitioning + Clustering
55
56```sql
57-- Partition by date, cluster by high-cardinality filter columns
58CREATE TABLE `project.dataset.fct_events`
59PARTITION BY DATE(event_timestamp)
60CLUSTER BY customer_id, event_type
61AS
62SELECT * FROM `project.dataset.raw_events`;
63
64-- Query benefits: partition pruning + cluster pruning
65-- Only scans partitions matching WHERE clause
66SELECT customer_id, COUNT(*)
67FROM `project.dataset.fct_events`
68WHERE event_timestamp BETWEEN '2026-01-01' AND '2026-01-31'
69 AND event_type = 'purchase'
70GROUP BY customer_id;
71
72-- Check bytes scanned reduction
73-- Target: 90%+ reduction vs unpartitioned table
74```
75
76### Warehouse Sizing Strategy (Snowflake)
77
78```
79Workload Type Recommended Size Auto-Suspend Concurrency
80───────────── ──────────────── ──────────── ───────────
81Dashboard queries X-Small/Small 60s Auto-scale (max 3)
82Analyst ad-hoc Medium 300s 1 cluster
83dbt daily build Large Immediate 1 cluster
84Data science / ML X-Large+ Immediate 1 cluster
85
86Key: separate workloads into different warehouses
87 to prevent resource contention and enable per-workload billing
88```
89
90## Anti-Patterns
91
921. **Scanning full tables** — always partition by date; a full scan of a 1TB table costs 10-50x more than a pruned scan
932. **Too many clustering keys** — 2-4 keys maximum; more keys reduce clustering effectiveness
943. **Oversized warehouses** — bigger does not always mean faster; profile first, right-size second
954. **Ignoring spill-to-disk** — queries spilling to remote storage are 10-100x slower; increase warehouse size or optimize query
965. **Materializing volatile data** — materialized views on rapidly changing tables cause constant refresh overhead
97
98## Quality Checklist
99
100- [ ] Large tables (>1B rows) partitioned by date column
101- [ ] Clustering keys set on top 2-3 filter/join columns
102- [ ] Query profile reviewed for top 10 slowest queries monthly
103- [ ] Spill-to-disk queries identified and optimized (increase size or rewrite)
104- [ ] Materialized views created for expensive dashboard aggregations
105- [ ] Warehouses auto-suspended when idle (60-300s)
106- [ ] Workloads separated into dedicated warehouses
107- [ ] Result cache hit rate >50% for repeated analytical queries
108- [ ] Bytes scanned tracked and reduced quarter-over-quarter
109- [ ] Unused tables/views identified and dropped quarterly