Analytical SQL & Cloud Data Warehouse Optimization¶
1. Partitioning & Clustering (BigQuery / Snowflake)¶
- Always partition by date/timestamp (e.g.
event_timestamp) to prune scanned byte volume. - Cluster by high-cardinality filter fields (e.g.
customer_id,region,status).
Optimized Table Definition (BigQuery DDL Example)¶
CREATE OR REPLACE TABLE `analytics_prod.events_partitioned`
PARTITION BY DATE(event_timestamp)
CLUSTER BY customer_id, event_type
AS
SELECT * FROM `analytics_prod.raw_events`;
2. Query Anti-Patterns to Eliminate¶
- Ban
SELECT *: Reading unneeded columns inflates BigQuery slot time and egress costs. Always list required columns explicitly. - Filter Early (Pushdown Predicates): Apply
WHEREfilters beforeJOINoperations or in CTEs. - Avoid Cross Joins: Never join without explicit
ONjoin keys unless computing a Cartesian product intentionally. - Approximate Aggregations for Large Data: Use
APPROX_COUNT_DISTINCT()instead ofCOUNT(DISTINCT)on massive datasets (reduces query runtime by 80%+ with < 1% error).
3. Query Execution Plan Profiling¶
Before deploying an analytical query to production pipelines: