Slow queries waste time and, on cloud warehouses, money.
Read the Execution Plan
Use EXPLAIN to see how the database runs a query: which tables are scanned, how joins are performed, and where time goes.
Reduce Data Scanned
- Select only needed columns, especially in columnar warehouses.
- Filter early.
- Use partitioning and clustering so filters skip irrelevant data.
Indexes
In transactional databases, indexes on join and filter columns speed up queries. Warehouses rely more on partitioning, clustering and columnar storage.
Join Carefully
- Join on keys with matching types.
- Avoid accidental many-to-many joins.
- Aggregate before joining when possible.
Avoid Common Anti-Patterns
- Functions on filtered columns that prevent index or partition use.
SELECT DISTINCTmasking join problems.- Repeated subqueries that could be computed once.
Materialise
Precompute expensive, frequently used results as tables or materialised views.
Monitor
Track the most expensive queries and optimise those first.