I recently attended the Snowflake Summit 2024 in San Francisco and had the opportunity to join a session focused on enhancing the efficiency and performance of queries using Snowflake. The session highlighted several critical strategies and common pitfalls. Here are some key takeaways:

Key Strategies for Improving Query Performance

1. Data Model

A well-structured data model is the foundation for efficient querying. Ensure that your data is organized in a way that supports the queries you need to run. Normalization, appropriate use of schemas, and understanding the relationships between your tables can significantly impact performance.

2. Search Optimization

Optimizing your search queries involves using indexes effectively, minimizing the use of wildcard searches, and leveraging query optimization techniques provided by Snowflake.

3. Auto Clustering

Snowflake’s automatic clustering feature helps maintain performance without manual intervention. It automatically manages the physical organization of the data to improve query performance, especially for large and frequently queried tables.

4. Materialized View

Materialized views store the results of a query physically, which can speed up frequent and repetitive queries. Use them when you have complex queries that need to be run often, as they can significantly reduce the time taken to retrieve results.

The Impact of Query Order on Cost and Performance

The order in which you connect queries can significantly affect both cost and performance.
For instance:

-- Less efficient query order
SELECT MY_EL.account_id,
FROM db.my_extra_large_table MY_EL
JOIN db.my_medium_table MY_MD ON MY_EL.account_id = MY_MD.account_id
JOIN db.my_small_table MY_SM ON MY_MD.account_id = MY_SM.account_id;

-- More efficient query order
WITH med_email AS (
    SELECT MY_MD.account_id,
    FROM db.my_medium_table MY_MD
    JOIN db.my_small_table MY_SM ON MY_MD.account_id = MY_SM.account_id
SELECT MY_EL.account_id,
FROM db.my_extra_large_table MY_EL
JOIN med_email ON MY_EL.account_id = med_email.account_id;

By rearranging the joins and using common table expressions (CTEs), you can optimize the query to process smaller datasets first, thereby reducing the cost and improving performance.

Common Mistakes Leading to Inefficient Queries

  1. Single Row Inserts (DML)
    Avoid inserting single rows in a loop. Instead, batch inserts can help improve performance significantly.
  2. Cartesian Product Joins (JOIN)
    Be cautious with joins that may inadvertently produce Cartesian products, which can lead to massive result sets and degrade performance.
  3. Spilling to Disk (DISK)
    Ensure your queries are optimized to fit in-memory processing whenever possible. Spilling to disk can slow down performance.
  4. Aggregate on 10+ Columns (SQL)
    Aggregating on too many columns can be inefficient. Try to limit the number of columns you aggregate on or use intermediate steps to aggregate progressively.
  5. Deep Nested Views (VIEW)
    Avoid using excessively nested views as they can complicate query plans and degrade performance.
  6. Select * From a Large Table (SQL)
    Avoid selecting all columns from large tables. Only select the columns you need to improve query performance and reduce data transfer costs.

By keeping these points in mind and applying best practices, you can significantly enhance the performance of your queries in Snowflake.

Thank you for reading this blog. Also check out our other blogs page to view more blogs on Power BI, Tableau, Alteryx, and Snowflake here.

Work together with one of our consultants and maximize the effects of your data. 

Contact us, and we'll help you right away.