๐ŸŽ‰ 75% of content is free forever โ€” Unlock Premium from $10/mo โ†’
CW
NEWSLIVESearch All Content
๐Ÿ’ผ Servicesโ„น๏ธ Aboutโœ‰๏ธ ContactView Pricing Plansfrom $10

Topic: Query Performance & Result Cache

Snowflake AdvancedPerformanceโญ Premium

Snowflake Advanced ยท Interview Prep

Query Performance & Result Cache

Difficulty: Hard ยท Commonly asked at Netflix, Amazon, Google

Interview Question

"A query that processes 1TB of data takes 30 seconds. After a data load, it now takes 5 minutes. Walk me through your diagnostic process and optimization strategies."

โ„น๏ธ

Companies Asking This: Netflix (Staff Data Engineer), Amazon (L6 Data Engineer), Google (Senior Data Engineer), Meta (Data Platform Engineer)


Performance Diagnostic Framework

Snowflake reports three separate clocks for every statement, and separating them is the first thing an interviewer wants to see. Compilation time reflects planner work โ€” if it exceeds one second you usually have too many joins or a predicate the optimizer cannot push down. Queue time reflects warehouse contention, not SQL quality: a query that waits forty seconds for a slot and then runs for two seconds has a capacity problem, not a tuning problem. Execution time is the only number your rewrite can really move.

The order of operations matters. Capture the baseline first โ€” bytes scanned, partitions scanned, spill, elapsed time โ€” so that every later change produces a comparison instead of an opinion. Then read the profile stage by stage: which stage consumed the wall clock, whether one join produced far more rows than it consumed, whether a sort or hash aggregation spilled, and how many micro-partitions were read versus how many exist. Fix the largest contributor only, re-run, and measure again. Changing three things at once makes it impossible to attribute the win.

Profile signalWhat it meansFirst move
Compilation above 1,000 msPlanner struggling, usually join countMaterialize an intermediate result
Queue time larger than executionWarehouse saturated or resumingResize, add multi-cluster, check auto-suspend
Partitions scanned equals partitions totalPruning failed completelyFilter on the clustered column, add a clustering key
Local spill above zeroMemory too small for sort or hashRaise warehouse size one step
Remote spill above zeroIntermediate results written to cloud storageResize first, rewrite second
Bytes scanned high, rows returned lowLow selectivity or full scanPush predicates down, drop SELECT *

Step 1: Query Analysis with PROFILE

-- Run query with PROFILE to get detailed metrics
SELECT 
    c.customer_name,
    COUNT(o.order_id) AS order_count,
    SUM(o.amount) AS total_spent
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_date >= '2024-01-01'
GROUP BY 1
ORDER BY 3 DESC;

-- Get query profile
SELECT 
    query_id,
    query_text,
    user_name,
    warehouse_name,
    compilation_time_ms,
    execution_time_ms,
    total_elapsed_time_ms,
    bytes_scanned / (1024*1024*1024) AS gb_scanned,
    bytes_written / (1024*1024) AS mb_written,
    bytes_spilled_to_local_storage / (1024*1024) AS mb_spill_local,
    bytes_spilled_to_remote_storage / (1024*1024) AS mb_spill_remote,
    partitions_scanned,
    partitions_total,
    ROUND(partitions_scanned * 100.0 / NULLIF(partitions_total, 0), 2) AS scan_percentage,
    result_cache_hit
FROM snowflake.account_usage.query_history
WHERE query_text LIKE '%customers c JOIN orders o%'
  AND start_time >= DATEADD(hour, -24, CURRENT_TIMESTAMP())
ORDER BY total_elapsed_time_ms DESC
LIMIT 5;

Result Cache

The result cache stores the final result set of a query for twenty-four hours, and a repeated query returns from it in milliseconds without touching a warehouse at all โ€” provided the textual query is identical, the underlying objects have not changed, and the caller runs with a role that still has access to every object referenced. Snowflake validates all three conditions before it serves the cached result, which is why a cache hit disappears the moment anyone loads new rows, alters a table, drops and recreates a view, or changes the caller's role. Volatile functions such as CURRENT_DATE() or RANDOM() disqualify a query entirely, because the same text could produce a different result tomorrow. Dashboards should therefore pin literal dates instead of asking for "today", and they should keep the query text byte-identical across refreshes: formatting changes, trailing comments and regenerated IN lists all force a miss. You can prove a hit by checking result_cache_hit in query history, and you can suppress it deliberately with the NO_RESULT_CACHE hint when you need to benchmark real execution time.

-- Check if result cache was used
SELECT 
    query_id,
    query_text,
    result_cache_hit,  -- 1 = cached, 0 = executed
    compilation_time_ms,
    execution_time_ms,
    total_elapsed_time_ms
FROM snowflake.account_usage.query_history
WHERE query_text LIKE '%SELECT%FROM orders%'
  AND start_time >= DATEADD(hour, -1, CURRENT_TIMESTAMP())
ORDER BY start_time DESC;

-- Force cache bypass for testing
SELECT * FROM orders 
    /* NO_RESULT_CACHE */
WHERE order_date = CURRENT_DATE();

-- Cache invalidation scenarios:
-- 1. Underlying data changes
-- 2. Volatile functions (CURRENT_DATE(), RANDOM())
-- 3. User role changes
-- 4. Schema changes

-- Test cache behavior
-- First execution (not cached)
SELECT COUNT(*) FROM orders WHERE order_date = CURRENT_DATE();
-- Second execution (cached)
SELECT COUNT(*) FROM orders WHERE order_date = CURRENT_DATE();
-- Check result
SELECT 
    query_id,
    result_cache_hit,
    total_elapsed_time_ms
FROM snowflake.account_usage.query_history
WHERE query_text LIKE '%COUNT(*)%orders%CURRENT_DATE%'
ORDER BY start_time DESC;

Scan Depth Analysis

Snowflake stores data in immutable micro-partitions of roughly 500 MB to 1 GB, and every query uses the per-partition minimum and maximum statistics stored in metadata to skip partitions whose range cannot satisfy the predicate. Pruning is therefore a function of how the data is clustered on disk, not of how selective the filter looks in isolation. A range filter on a date column clustering key prunes almost everything; the same filter on an unclustered column touches every partition. Two things break pruning even when the query text never changes. First, a bulk load appends partitions stamped with the new load date, so predicates that used to exclude old partitions now include a fresh batch and the scanned percentage climbs. Second, a multi-tenant table where the clustering column has low cardinality โ€” one status value repeated across millions of rows โ€” will never prune well no matter how often you re-cluster it.

-- Analyze micro-partition pruning efficiency
SELECT 
    query_id,
    partitions_scanned,
    partitions_total,
    ROUND(partitions_scanned * 100.0 / NULLIF(partitions_total, 0), 2) AS scan_pct,
    CASE 
        WHEN partitions_scanned = partitions_total THEN 'FULL SCAN - CRITICAL'
        WHEN partitions_scanned > partitions_total * 0.5 THEN 'POOR PRUNING - WARNING'
        WHEN partitions_scanned > partitions_total * 0.1 THEN 'MODERATE PRUNING'
        ELSE 'EXCELLENT PRUNING'
    END AS pruning_quality
FROM snowflake.account_usage.query_history
WHERE query_type = 'SELECT'
  AND start_time >= DATEADD(day, -7, CURRENT_TIMESTAMP())
  AND partitions_total > 100
ORDER BY scan_pct DESC
LIMIT 20;

-- Compare pruning before and after data load
-- Before load
SELECT 
    'BEFORE_LOAD' AS period,
    AVG(partitions_scanned * 100.0 / partitions_total) AS avg_scan_pct,
    AVG(total_elapsed_time_ms) AS avg_query_time_ms
FROM snowflake.account_usage.query_history
WHERE query_text LIKE '%orders%'
  AND start_time BETWEEN '2024-01-10' AND '2024-01-15'

UNION ALL

-- After load
SELECT 
    'AFTER_LOAD' AS period,
    AVG(partitions_scanned * 100.0 / partitions_total) AS avg_scan_pct,
    AVG(total_elapsed_time_ms) AS avg_query_time_ms
FROM snowflake.account_usage.query_history
WHERE query_text LIKE '%orders%'
  AND start_time BETWEEN '2024-01-16' AND '2024-01-20';

Real-World Scenario: Netflix

Question: "How do you optimize a dashboard that queries 50TB of data and needs sub-second response times?"

Solution: Multi-Layer Optimization

-- 1. Create materialized view for common aggregations
CREATE MATERIALIZED VIEW mv_dashboard_summary AS
SELECT 
    DATE_TRUNC('hour', event_timestamp) AS event_hour,
    event_type,
    region,
    device_type,
    COUNT(*) AS event_count,
    COUNT(DISTINCT user_id) AS unique_users,
    SUM(CASE WHEN event_type = 'purchase' THEN revenue ELSE 0 END) AS total_revenue
FROM events
WHERE event_timestamp >= DATEADD(day, -90, CURRENT_DATE())
GROUP BY 1, 2, 3, 4;

-- 2. Create clustering key aligned with dashboard queries
ALTER TABLE events CLUSTER BY (event_timestamp, event_type, region);

-- 3. Use result cache aggressively
-- Dashboard queries should use same query structure for cache hits
-- Instead of:
SELECT * FROM events WHERE event_date = CURRENT_DATE() AND event_type = 'click';
-- Use:
SELECT * FROM events WHERE event_date = '2024-01-15' AND event_type = 'click';

-- 4. Create search optimization for common filters
ALTER TABLE events SET SEARCH_OPTIMIZATION = ON;
ALTER TABLE events ADD SEARCH OPTIMIZATION ON (event_type, region, device_type);

-- 5. Monitor dashboard performance
SELECT 
    query_id,
    query_text,
    total_elapsed_time_ms,
    bytes_scanned / (1024*1024*1024) AS gb_scanned,
    result_cache_hit,
    CASE 
        WHEN result_cache_hit = 1 THEN 'CACHED'
        WHEN total_elapsed_time_ms < 1000 THEN 'FAST'
        WHEN total_elapsed_time_ms < 5000 THEN 'ACCEPTABLE'
        ELSE 'SLOW - NEEDS OPTIMIZATION'
    END AS performance_status
FROM snowflake.account_usage.query_history
WHERE query_text LIKE '%dashboard%'
  AND start_time >= DATEADD(hour, -1, CURRENT_TIMESTAMP())
ORDER BY total_elapsed_time_ms DESC;

Real-World Scenario: Amazon

Question: "A query joins 5 tables and spills to remote storage. How do you optimize without changing the query?"

Optimization Without Query Changes

-- 1. Analyze spill metrics
SELECT 
    query_id,
    bytes_spilled_to_local_storage / (1024*1024*1024) AS local_spill_gb,
    bytes_spilled_to_remote_storage / (1024*1024*1024) AS remote_spill_gb,
    execution_time_ms / 1000 AS execution_seconds
FROM snowflake.account_usage.query_history
WHERE query_id = 'your-query-id';

-- 2. Increase warehouse size
ALTER WAREHOUSE analytics_wh SET WAREHOUSE_SIZE = 'X-LARGE';

-- 3. Add clustering keys to joined tables
ALTER TABLE orders CLUSTER BY (customer_id, order_date);
ALTER TABLE customers CLUSTER BY (customer_id);
ALTER TABLE products CLUSTER BY (product_id);

-- 4. Create materialized view for the join
CREATE MATERIALIZED VIEW orders_customers_products AS
SELECT 
    o.order_id,
    o.order_date,
    o.amount,
    c.customer_name,
    c.segment,
    p.product_name,
    p.category
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id
JOIN products p ON o.product_id = p.product_id;

-- 5. Monitor improvement
SELECT 
    'BEFORE' AS optimization,
    AVG(bytes_spilled_to_remote_storage / (1024*1024*1024)) AS avg_remote_spill_gb,
    AVG(execution_time_ms / 1000) AS avg_execution_seconds
FROM snowflake.account_usage.query_history
WHERE query_id = 'before-optimization-query-id'

UNION ALL

SELECT 
    'AFTER' AS optimization,
    AVG(bytes_spilled_to_remote_storage / (1024*1024*1024)) AS avg_remote_spill_gb,
    AVG(execution_time_ms / 1000) AS avg_execution_seconds
FROM snowflake.account_usage.query_history
WHERE query_id = 'after-optimization-query-id';

When to Resize, Rewrite or Redesign

Interviewers care about the decision, not the incantation, so work through a fixed ladder. If queue time dominates, capacity is the bottleneck: an X-Small that waits sixty seconds for a slot is not fixed by tighter SQL, so move one size up and re-measure. If execution time dominates and partitions scanned sits close to partitions total, resize will only make the same full scan marginally faster โ€” fix the predicate or the clustering instead. If spill appears, add memory first, since one size step usually clears it, and consider pre-aggregating only after that.

Worked example: a dashboard reading 400 GB in 30 seconds on a Large drops to roughly 15 seconds on an X-Large with an unchanged scan, while a clustering key that prunes the scan down to 40 GB brings the original Large close to 4 seconds. Pruning beats sizing whenever pruning is available. Cheap reversible fixes come first, structural rewrites last, and each step gets measured against the baseline you captured before touching anything.

Dominant symptomResize firstRewrite firstRedesign
Queue time exceeds executionYes, one sizeNoOnly if load is permanent
Partitions scanned equals totalBarely helpsYes, filter or clusterMaterialize if queried daily
Local or remote spillYes, one stepLater, after memoryPre-aggregate heavy joins
Compilation above one secondNoSimplify join countStage intermediate results
Sustained concurrency spikesAdd multi-clusterBatch the refreshSplit hot and cold paths

Performance Monitoring Dashboard

-- 1. Query performance trends
SELECT 
    DATE(start_time) AS query_date,
    AVG(total_elapsed_time_ms) / 1000 AS avg_seconds,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY total_elapsed_time_ms) / 1000 AS median_seconds,
    PERCENTILE_CONT(0.95) WITHIN GROUP (ORDER BY total_elapsed_time_ms) / 1000 AS p95_seconds,
    COUNT(*) AS query_count,
    SUM(CASE WHEN result_cache_hit = 1 THEN 1 ELSE 0 END) AS cached_queries
FROM snowflake.account_usage.query_history
WHERE query_type = 'SELECT'
  AND start_time >= DATEADD(day, -30, CURRENT_TIMESTAMP())
GROUP BY 1
ORDER BY 1;

-- 2. Warehouse performance comparison
SELECT 
    warehouse_name,
    warehouse_size,
    AVG(total_elapsed_time_ms) / 1000 AS avg_query_seconds,
    SUM(bytes_scanned / (1024*1024*1024)) AS total_tb_scanned,
    SUM(credits_used) AS total_credits,
    SUM(bytes_scanned / (1024*1024*1024)) / NULLIF(SUM(credits_used), 0) AS tb_per_credit
FROM snowflake.account_usage.query_history q
JOIN snowflake.account_usage.warehouse_metering_history w
    ON q.warehouse_name = w.warehouse_name
WHERE q.start_time >= DATEADD(day, -7, CURRENT_TIMESTAMP())
GROUP BY 1, 2
ORDER BY 5 DESC;

-- 3. Identify performance bottlenecks
SELECT 
    CASE 
        WHEN compilation_time_ms > 1000 THEN 'COMPILATION'
        WHEN bytes_spilled_to_remote_storage > 0 THEN 'REMOTE_SPILL'
        WHEN partitions_scanned = partitions_total THEN 'FULL_SCAN'
        WHEN total_elapsed_time_ms > 30000 THEN 'LONG_RUNNING'
        ELSE 'NORMAL'
    END AS bottleneck_type,
    COUNT(*) AS query_count,
    AVG(total_elapsed_time_ms) / 1000 AS avg_seconds
FROM snowflake.account_usage.query_history
WHERE query_type = 'SELECT'
  AND start_time >= DATEADD(day, -7, CURRENT_TIMESTAMP())
GROUP BY 1
ORDER BY 2 DESC;

Best Practices

IssueSolution
Full table scansAdd clustering keys, use search optimization
Remote spillIncrease warehouse size, reduce data volume
Slow compilationSimplify queries, use materialized views
Low cache hit rateUse consistent query patterns, avoid volatile functions
High queue depthUse multi-cluster warehouse
Slow joinsCluster tables by join keys

โš ๏ธ

Performance Anti-Patterns:

  1. **SELECT *** โ€” Only select needed columns
  2. Missing WHERE clauses โ€” Filter early to reduce data scanned
  3. Using functions in WHERE โ€” Prevents predicate pushdown
  4. Not monitoring โ€” Set up alerts for query time degradation
  5. Ignoring spill โ€” Remote spill kills performance

Key Takeaways

  • Separate compilation, queue and execution time first โ€” each points at a different fix and only one of them is SQL.
  • Check partitions scanned versus partitions total before touching warehouse size, because pruning beats sizing whenever pruning is possible.
  • The result cache holds results for twenty-four hours and is invalidated by any data change, schema change, role change or different query text.
  • Capture a baseline, change exactly one thing, re-measure โ€” performance claims without before and after numbers are opinions.
๐Ÿ”’

Premium Content

Topic: Query Performance & Result Cache

You've previewed the first section. Unlock this full lesson and 900+ advanced tutorials with a Premium plan.

๐ŸŽฏEnd-to-end Projects
๐Ÿ’ผInterview Prep
๐Ÿ“œCertificates
๐ŸคCommunity Access

Already a member? Log in

CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-15

โ€”
โ˜†โ˜†โ˜†โ˜†โ˜†
0 ratings

Rate & Feedback

Advertisement