< blog
10 min read

7 Snowflake Query Optimization Tips: Boost Performance and Reduce Costs

Digital globe of blue hexagons with streaming fibre-optic light

In short: The largest Snowflake query savings come from scanning less data, reusing cached results, simplifying queries and joins, and sizing and suspending warehouses to match the workload. Snowflake bills a warehouse for the time it runs, not for each query, so a faster query only lowers the bill when the warehouse then runs for less time, runs at a smaller size or suspends sooner.

Snowflake has become a cornerstone of modern data warehousing with its ability to scale dynamically and handle massive datasets with ease. However, the flexibility and power of Snowflake come with the responsibility of optimizing queries to ensure performance and cost-efficiency.

In this blog, we’ll cover seven essential query optimization techniques to enhance query performance and achieve significant cost reductions. We’ll also explore common challenges, delve into its built-in tools, and demonstrate how Snowflake query optimization saves costs effectively.

Common Challenges

Snowflake is a highly efficient and versatile data platform, but like any system, its performance and cost-efficiency can degrade if queries are not properly optimized. A lack of awareness about common pitfalls often leads to unnecessary overheads, both in terms of time and expenses. Here, we delve into the most prevalent challenges users encounter when optimizing queries in Snowflake and their potential impacts.

1. What causes excessive Snowflake data scanning?

The Problem: One of the most frequent culprits of high query costs and long execution times in Snowflake is the excessive scanning of unnecessary data. Snowflake organizes data into micro-partitions to optimize access, but when queries do not align with these partitions, the platform ends up scanning more data than required. This inefficiency inflates costs and slows performance.

Impact: Increased compute costs due to unnecessary data processing. Delayed query results, especially when working with large datasets.

Key Insight: Proper table design, combined with efficient filtering and clustering, can significantly reduce the volume of data scanned, leading to faster queries and lower costs.

Save Big in 30 min

Ready to take the plunge? Hop on a 30 minute demo to see how much you can save in the first 30 days with Seemore.

Book a demo TODAY

2. Why is Snowflake query caching underused?

The Problem: Snowflake’s result cache and warehouse data cache are designed to minimize redundant computations, but they are often underutilized due to poor query structuring. This leads to avoidable processing of the same data multiple times.

Impact: Increased query execution time as results are recalculated. Higher compute costs due to repeated data scans and processing.

Key Insight: Structure queries so that identical queries can reuse Snowflake’s Result Cache, and match the auto-suspend timeout to the gaps in your workload: a suspended warehouse drops its data cache, while a timeout that is too long keeps paying for idle time.

3. How do complex queries affect Snowflake performance?

The Problem: Overly complex queries, including deeply nested subqueries, unnecessary joins, and layers of derived tables or views, are harder to optimize and often run slower than they need to. These complexities often result in inefficient execution plans, and the Query Profile shows which step takes the time.

Impact: Increased execution times due to suboptimal plans. Higher compute resource consumption, leading to escalated costs.

Key Insight: Simplifying queries and breaking down transformations into manageable steps reduces computational strain and improves query performance.

4. How do unoptimized joins slow Snowflake queries?

The Problem: Joins are integral to most analytical queries but can become major performance bottlenecks if not properly optimized. A missing or too-loose join condition can produce a Cartesian product (an exploding join, where the Join step returns far more rows than it reads), and non-equality join conditions can be significantly slower.

Impact: Longer query execution times, especially for large datasets. Increased storage and compute costs due to excessive intermediate data generation.

Key Insight: Joining on matching keys with plain equality conditions, ideally on clustered columns, and avoiding OR conditions where you can, keeps join operations efficient.

5. How does warehouse configuration affect Snowflake cost and speed?

The Problem: Snowflake’s virtual warehouses are the backbone of query execution, but poor configuration can lead to performance issues or unnecessary costs. A warehouse that is too small may result in slow queries due to insufficient compute resources, while an oversized warehouse wastes money on idle capacity.

Impact: Query delays, especially during peak usage. Wasted resources and higher operational expenses.

Key Insight: Tuning warehouse size and scaling policies based on workload requirements ensures a balance between performance and cost.

6. Why monitor Snowflake query performance?

The Problem: Many users overlook the importance of monitoring and analyzing query performance, making it difficult to identify inefficiencies or optimize resource usage.

Impact: Prolonged inefficiencies in query execution. Difficulty scaling operations effectively due to a lack of actionable insights.

Key Insight: Regularly monitoring query performance and understanding execution bottlenecks through tools like Query Profile and Query History are essential for maintaining an optimized Snowflake environment.

These challenges can result in poor performance and wasted resources. Fortunately, they can be addressed with careful planning and the following optimization strategies.

Find out where you stand

Ready to see where you stand? Let us take a peek under the hood with a free assessment and no commitment.

Find your savings

Optimization Tips

1. Minimize Data Scanned with Clustering and Partition Pruning

Snowflake keeps value-range metadata for every micro-partition and uses it to skip partitions that a filter cannot match, which is called pruning. Pruning works best when the values of the filtered column sit together in few partitions, and a clustering key on a large table is how you ask Snowflake to keep them that way. Clustering has a cost, though: Automatic Clustering consumes credits, so start with one or two large tables that you filter on often and measure the effect.

Example:

-- Add a clustering key on a frequently filtered column
ALTER TABLE orders CLUSTER BY (order_date);
-- Query with effective pruning
SELECT *
FROM orders
WHERE order_date > '2024-01-01';

 

2. Leverage Query Caches

Snowflake can reuse a persisted query result when a later query matches the earlier one exactly and the underlying data has not changed. Avoid functions that return a different result on every run, such as RANDOM() or UUID_STRING(), which prevent result reuse.

Example:

-- Cache-friendly query structure
SELECT region, SUM(sales) AS total_sales
FROM sales_data
WHERE sales_year = 2024;

 

3. Simplify Query Logic

Break down complex queries into smaller, manageable steps. Use intermediate tables or materialized views to store partial results.

Example:

-- Use intermediate table for simplified queries
CREATE OR REPLACE TEMPORARY TABLE region_summary AS
SELECT region, SUM(sales) AS total_sales
FROM sales_data
GROUP BY region;

 

4. Pre-Aggregate Frequently Used Metrics

Roll-up tables store pre-aggregated results for common queries, eliminating the need to process raw data repeatedly.

Example:

-- Pre-aggregated summary table
CREATE OR REPLACE TABLE daily_sales_summary AS
SELECT sales_date, region, SUM(amount) AS total_sales
FROM sales_data
GROUP BY sales_date, region;

 

5. Optimize Joins

Use clustered columns in join predicates to enhance performance, and prefer plain equality conditions. The Query Profile documentation warns that non-equality join predicates can be significantly slower, and a condition built with OR is usually not a plain equality, so rewrite it where you can.

Example:

-- Optimized join using clustered keys
SELECT o.order_id, c.customer_name
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
WHERE c.region = 'North America';

 

6. Tune Virtual Warehouse Configurations

Scale warehouses based on workload needs. Use Auto-Suspend to minimize idle costs and enable multi-cluster warehouses for high-concurrency workloads. AUTO_SUSPEND is set in seconds, so 300 suspends a warehouse after five idle minutes, and Snowflake recommends a low value that matches the gaps in your workload. Seemore’s warehouse optimization shows the size, suspend timer and scaling policy of every warehouse in one place, with recommendations for what to change.

Example:

-- Optimize warehouse settings
ALTER WAREHOUSE analytics_warehouse SET WAREHOUSE_SIZE = LARGE;
ALTER WAREHOUSE analytics_warehouse SET AUTO_SUSPEND = 300;

 

7. Monitor and Refactor Inefficient Queries

Use Snowflake’s Query Profile to analyze slow or expensive queries. Focus on addressing bottlenecks like TableScans or Sorts.

Example Workflow:

  1. Open Query History to locate slow queries.
  2. Use Query Profile to identify expensive nodes.
  3. Refactor queries based on identified inefficiencies.

 

Leveraging Snowflake’s Built-in Tools for Optimization

Snowflake offers a robust set of tools to monitor and optimize query performance. These tools are invaluable for identifying inefficiencies and guiding optimization efforts.

1. What does Snowflake Query Profile show?

The Query Profile visualizes query execution, highlighting bottlenecks like TableScans or Joins. It helps you focus optimization efforts on the most resource-intensive parts of the query.

2. How can Query History reveal performance issues?

Track execution times, data scanned, and performance trends over time. This data helps you identify recurring issues or anomalies in query performance. To see the compute credits attributed to each query, use the QUERY_ATTRIBUTION_HISTORY view in the Account Usage schema, which leaves out warehouse idle time.

3. What do Snowflake Account Usage Views show?

These views provide detailed insights into warehouse utilization, query costs, and resource consumption. Use them to evaluate and adjust warehouse configurations.

Pro Tip: Combine insights from these tools to build a comprehensive optimization strategy. For instance, if Query Profile shows a TableScan reading nearly as many partitions as the table holds, check whether the filtered column is well clustered before you add or change a clustering key.

 

How Query Optimization Saves Costs

Query optimization lowers the bill when it changes how long, or how large, a warehouse has to run. Snowflake bills a virtual warehouse per second while it runs, with a 60-second minimum each time it starts or resumes, whether or not it is busy. A faster query on a warehouse that stays on therefore saves nothing by itself. The savings arrive when faster queries let the warehouse suspend sooner, run at a smaller size or serve more work.

Key Cost-Saving Strategies:

  • Reduce Data Scanned: Proper clustering and pruning lower the compute required for queries.
  • Optimize Warehouses: Scale warehouses to meet workload demands without over-provisioning.
  • Leverage Caching: Reuse results to minimize redundant computations.
  • Pre-Aggregate Data: Save resources by querying smaller, roll-up tables instead of raw datasets.

 

Example: a query that scans 10TB of raw data every day can be replaced with a pre-aggregated table that scans only 100GB (illustrative figures). That is 99 percent less data to scan, and it saves money once the warehouse spends less time running or can run at a smaller size.

To confirm that a change worked, compare a warehouse’s credits before and after in the WAREHOUSE_METERING_HISTORY view, and use QUERY_ATTRIBUTION_HISTORY to see which queries account for the compute credits.

Tracing a bill that suddenly jumped back to its cause by hand takes time. Seemore’s Cost Anomaly Detection compares each day’s spend across accounts, warehouses and services with the previous 14 days of your own cost history, and traces a spike to the warehouse, job or query behind it.

 

Conclusion: Mastering Query Optimization in Snowflake

Mastering Snowflake query optimization techniques is essential for balancing performance and cost-efficiency. By addressing common challenges, leveraging Snowflake’s built-in tools, and following these seven tips, you can unlock the full potential of your Snowflake environment.

Whether you’re a data engineer or an analyst, these strategies will help you streamline operations, reduce costs, and achieve faster query performance. Implement these best practices today and take your Snowflake workloads to the next level.

Find out where you stand

Ready to see where you stand? Let us take a peek under the hood with a free assessment and no commitment.

Find your savings