Snowflake Query Optimization

Introduction to Snowflake SQL

George Boorman

Senior Curriculum Manager, DataCamp

Why Optimize Queries in Snowflake?

  • Achieve faster results
  • Cost efficiency
    • Shorter query times consumes fewer resources like CPU and memory.

Image depicting high usage of CPU and memory leading to money

Introduction to Snowflake SQL

Common query problems

  • Exploding Joins: Be cautious!

Incorrect

SELECT *
FROM order_details AS od
JOIN pizzas AS p -- Missing ON condition leading to exploding joins

Query results rows and time details with no ON condition

Introduction to Snowflake SQL

Common query problems

  • Exploding Joins: Be cautious!

Correct

SELECT *
FROM order_details AS od
JOIN pizzas AS p
ON od.pizza_id = p.pizza_id

Query results rows and time details with ON condition

Introduction to Snowflake SQL

Common query problems

  • UNION or UNION ALL: Know the difference
    • UNION removes duplicates, slows down the query
    • UNION ALL is faster if no duplicates
  • Handling big data
    • Use filters to narrow down data
    • Apply limits for quicker results
Introduction to Snowflake SQL

How to optimize queries?

SELECT * ⌛

SELECT
    *
FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF100.ORDERS

Query details, time and rows details

SELECT LIMIT 10* ⚡

SELECT *
FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF100.ORDERS
LIMIT 10

Query details, time and rows details using LIMIT 10

Introduction to Snowflake SQL

How to optimize queries?

Using SELECT *

SELECT 
    *
FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF100.ORDERS

Query details, time and rows details

Avoid SELECT *

SELECT o_orderdate, 
    o_orderstatus 
FROM SNOWFLAKE_SAMPLE_DATA.TPCH_SF100.ORDERS

Query details, time and rows details using select column names

Introduction to Snowflake SQL

How to optimize queries?

Filter Early

  • Use WHERE Clause Early On
  • Apply filters before JOINs
    • JOIN will process fewer rows
Introduction to Snowflake SQL

Without early filtering

SELECT orders.order_id, 
       orders.order_date, 
       pizza_type.name, 
       pizzas.pizza_size
FROM orders
JOIN order_details 
ON orders.order_id = order_details.order_id
JOIN pizzas 
ON order_details.pizza_id = pizzas.pizza_id
JOIN pizza_type 
ON pizzas.pizza_type_id = pizza_type.pizza_type_id
WHERE orders.order_date = '2015-01-01';  -- Filtering after JOIN
Introduction to Snowflake SQL

With early filtering

WITH filtered_orders AS (
  SELECT *
  FROM orders
  WHERE order_date = '2015-01-01'  -- Filtering in CTE before JOIN
)
SELECT filtered_orders.order_id, 
       filtered_orders.order_date, 
       pizza_type.name, 
       pizzas.pizza_size
FROM filtered_orders -- Joining with CTE
JOIN order_details
ON filtered_orders.order_id = order_details.order_id
JOIN pizzas
ON order_details.pizza_id = pizzas.pizza_id
JOIN pizza_type
ON pizzas.pizza_type_id = pizza_type.pizza_type_id;
Introduction to Snowflake SQL

Query history

  • Query History
    • snowflake.account_usage.query_history
    • Query History provides different metrics such as execution time
SELECT query_text, start_time, end_time, execution_time 
FROM
   snowflake.account_usage.query_history
WHERE query_text ILIKE '%order_details%'

Screenshot 2023-09-01 at 14.32.29.png

  • ILIKE: Case-insensitive string-matching
Introduction to Snowflake SQL

Query history

  • Spot slow or frequently running queries
SELECT query_text, 
    start_time, 
    end_time, 
    execution_time 
FROM
   snowflake.account_usage.query_history
WHERE
   execution_time > 1000

Filtering result based on execution time greater than 1000ms

Introduction to Snowflake SQL

Let's practice!

Introduction to Snowflake SQL

Preparing Video For Download...