Typical workflow of the Snowflake query optimizer:
- Row Operations: Inspect
FROM,JOINandWHEREclauses for partition pruning- Static pruning based on the columns in the
WHEREclause based on theMIN/MAXmicro-partition metadata - Dynamic pruning based on the
JOINcolumns
- Static pruning based on the columns in the
- Inspect
GROUP BYandHAVING - Inspect
SELECT,DISTINCT,ORDER BYandLIMIT
- Take advantage of partition pruning by limiting the initial data set as much as possible.
- Use appropriate filters as early as possible
- Filter on columns which would be naturally ordered (or have been clustered to be ordered) in micro-partitions
- Natural clustering occurs on columns which have high correlation to ingestion order, e.g. date columns
- When filtering by string value, avoid wildcards at the beginning of the string as the optimizer cannot take advantage of the
MIN/MAXmicro-partition metadata - Prefer uncorrelated over correlated subqueries. A correlated subquery is one which would have to execute multiple times since its query clause (or, worse, clauses!) depends on multiple values from the parent query.
- Join on unique keys
- Ensure keys are distinct to avoid join explosions
- Understand relationships between tables before joining
- Avoid many-to-main joins
- Avoid unintentional cross joins
- Prefer low cardinality columns for
GROUP BYclauses - Add
LIMITtoORDER BYclauses where possible - Limit
ORDER BYusage to a single instance in the top levelSELECTwherever possible - Review the query
Profile Overviewfor- spilling to disk
- high network usage
These can be set for at the Account/Session/Role or Virtual Warehouse level:
STATEMENT_TIMEOUT_IN_SECONDS: How long can a query run before being cancelled by the system, Default: 2 daysSTATEMENT_QUEUED_TIMEOUT_IN_SECONDS: How long can a queued query wait before being cancelled by the system, Default: 0 (forever)