Result caching layers in Snowflake - Time & Space Complexity
When using result caching layers in Snowflake, it's important to understand how the time to get query results changes as data or query volume grows.
We want to know how the caching affects the number of operations Snowflake performs as input size changes.
Analyze the time complexity of querying with result caching enabled.
-- Run a query that may use result cache
SELECT customer_id, SUM(amount) AS total_spent
FROM sales
WHERE sale_date >= '2024-01-01'
GROUP BY customer_id;
-- Repeat the same query multiple times
This sequence runs a query that aggregates sales data and may hit the result cache if repeated.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Query execution and result retrieval from cache or compute cluster.
- How many times: Each time the query runs, it either reads from cache or recomputes results.
When the query result is cached, repeated runs return results quickly without reprocessing data.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | ~10 cache lookups, 0 recomputations |
| 100 | ~100 cache lookups, 0 recomputations |
| 1000 | ~1000 cache lookups, 0 recomputations |
Pattern observation: The number of cache lookups grows linearly with query runs, but heavy recomputation does not happen if cache is hit.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of queries, but each query is fast due to caching.
[X] Wrong: "Result caching makes query time constant no matter how many times we run it."
[OK] Correct: Each query still requires a cache lookup, so time grows with the number of queries, even if each is fast.
Understanding how caching layers affect query time helps you explain performance improvements clearly and shows you grasp cloud data platform behavior.
What if the query changes slightly each time so the cache cannot be used? How would the time complexity change?
Practice
Solution
Step 1: Understand Snowflake's caching behavior
Snowflake stores query results for 24 hours to speed up repeated queries.Step 2: Identify the effect of caching on repeated queries
When the same query runs again within 24 hours, Snowflake returns the cached result instantly without rerunning the query.Final Answer:
Returns the cached result instantly without rerunning the query -> Option DQuick Check:
Result caching = instant cached return [OK]
- Thinking query always reruns
- Assuming cache expires immediately
- Believing manual refresh is needed
Solution
Step 1: Recall how to disable result caching
Snowflake uses ALTER SESSION SET USE_CACHED_RESULT = FALSE to disable caching for the session.Step 2: Match the correct syntax
The correct syntax is ALTER SESSION SET USE_CACHED_RESULT = FALSE;Final Answer:
ALTER SESSION SET USE_CACHED_RESULT = FALSE; -> Option BQuick Check:
Disable cache param = USE_CACHED_RESULT = FALSE [OK]
- Using wrong parameter names
- Incorrect ALTER SESSION syntax
- Confusing with other cache parameters
SELECT COUNT(*) FROM sales;Immediately followed by:
SELECT COUNT(*) FROM sales;What will happen on the second query?
Solution
Step 1: Identify caching behavior for repeated queries
Snowflake caches query results for 24 hours and returns cached results if the same query runs again.Step 2: Apply to the given queries
The second SELECT COUNT(*) query is identical and runs immediately, so Snowflake returns the cached result.Final Answer:
Snowflake returns the cached result from the first query -> Option AQuick Check:
Repeated query = cached result return [OK]
- Assuming query always reruns
- Expecting partial or error results
- Confusing cache with data freshness
ALTER SESSION SET USE_CACHED_RESUTL = FALSE;But you notice subsequent queries still return cached results. What is the likely cause?
Solution
Step 1: Verify the USE_CACHED_RESULT parameter usage
The parameter must be spelled exactly USE_CACHED_RESULT and set to FALSE to disable caching.Step 2: Identify why caching still happens
If caching still occurs, the parameter might be misspelled or incorrectly specified.Final Answer:
The USE_CACHED_RESULT parameter is misspelled or incorrect -> Option CQuick Check:
Correct parameter spelling and value disables cache [OK]
- Assuming caching can't be disabled
- Misspelling the parameter name
- Ignoring exact parameter syntax
Solution
Step 1: Identify how to disable result caching per session
Snowflake allows disabling result caching using ALTER SESSION SET USE_CACHED_RESULT = FALSE before the query.Step 2: Evaluate other options for cost and effectiveness
Manually clearing cache or switching warehouses is inefficient and costly; running multiple times does not guarantee fresh data.Final Answer:
Execute ALTER SESSION SET USE_CACHED_RESULT = FALSE; before the query to disable caching -> Option AQuick Check:
Use USE_CACHED_RESULT = FALSE for fresh data [OK]
- Trying to clear cache manually
- Switching warehouses unnecessarily
- Relying on repeated runs to refresh data
Ready to prove it?
Pass the test and this pattern moves unknown
