Use Snowflake’s system views, query profile, and warehouse tuning to detect and fix slow-running SQL.
Large scans, skewed joins, insufficient warehouse size, and missing micro-partition pruning top the list. Identifying the exact cause requires inspecting execution history and profiles.
Run SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
or INFORMATION_SCHEMA.QUERY_HISTORY
filtering on long EXECUTION_TIME
. Sort descending to surface the worst offenders.
SELECT query_id, user_name, execution_time/1000 AS secs, start_time, query_textFROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORYWHERE start_time >= DATEADD(day,-7,CURRENT_TIMESTAMP()) AND execution_time > 30000 -- >30sORDER BY execution_time DESC;
Click the query_id in Snowflake UI or run SELECT SYSTEM$GET_QUERY_PROFILE('query_id');
. Focus on steps with high percentages in "Elapsed" or "Partitions scanned".
Unrestricted SELECT *
, joining Orders
to OrderItems
without filtering dates, and using functions on created_at
prevent pruning and create giant scans.
Filter early, select needed columns, and cast predicates to match storage types. Replace:
SELECT * FROM Orders oJOIN OrderItems oi ON oi.order_id=o.idWHERE DATE(o.order_date)='2024-01-01';
with:
SELECT o.id, o.total_amount, SUM(oi.quantity) qtyFROM Orders oJOIN OrderItems oi USING (id)WHERE o.order_date = '2024-01-01'GROUP BY o.id,o.total_amount;
Scale up (larger X-L) for CPU-bound queries; scale out (multi-cluster) for concurrency; suspend when idle to save cost. Use AUTO_SUSPEND=60
, AUTO_RESUME=TRUE
.
Yes. Set ALTER SESSION SET STATEMENT_TIMEOUT_IN_SECONDS = 30;
or apply a resource monitor with EXECUTION_TIME_LIMIT_IN_SECONDS
.
1) Always filter on partition columns.
2) Avoid functions on columns in predicates.
3) Inspect QUERY_HISTORY
weekly.
4) Keep warehouses right-sized.
5) Prefer clustering for high-value tables.
No. Clustering helps when filters target a narrow range. On small or rarely filtered tables clustering can add cost without benefits.
Review weekly for production warehouses or after releasing new features to catch regressions quickly.