Cost optimization strategies in Snowflake - Time & Space Complexity
We want to understand how the cost of running Snowflake operations grows as we use more resources or run more queries.
How does the amount of work affect the cost and time spent?
Analyze the time complexity of the following operation sequence.
-- Create a warehouse
CREATE WAREHOUSE my_wh WITH WAREHOUSE_SIZE = 'SMALL';
-- Run a query using the warehouse
SELECT * FROM large_table WHERE condition = 'value';
-- Suspend the warehouse to save cost
ALTER WAREHOUSE my_wh SUSPEND;
-- Resume warehouse when needed
ALTER WAREHOUSE my_wh RESUME;
This sequence shows creating a compute resource, running a query, and managing the warehouse state to optimize cost.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Running queries on the warehouse.
- How many times: Each query run uses the warehouse resources once.
- Additional operations: Suspending and resuming the warehouse happen as needed to save cost.
As the number of queries increases, the total compute time and cost grow roughly in direct proportion.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 queries | 10 query executions + some suspend/resume calls |
| 100 queries | 100 query executions + some suspend/resume calls |
| 1000 queries | 1000 query executions + some suspend/resume calls |
Pattern observation: The cost and operations grow linearly with the number of queries run.
Time Complexity: O(n)
This means the total cost and operations increase directly in proportion to how many queries you run.
[X] Wrong: "Running more queries won't increase cost if the warehouse is already running."
[OK] Correct: Each query uses compute resources, so more queries mean more compute time and higher cost, even if the warehouse stays running.
Understanding how cost grows with usage helps you design efficient systems and explain trade-offs clearly in real work and interviews.
"What if we used auto-suspend and auto-resume features for the warehouse? How would the time complexity change?"
Practice
Solution
Step 1: Understand warehouse billing
Snowflake charges for compute resources while warehouses run, even if idle.Step 2: Identify cost-saving feature
Auto-suspend stops the warehouse after inactivity, preventing unnecessary charges.Final Answer:
Auto-suspend -> Option AQuick Check:
Auto-suspend = stops warehouse when idle [OK]
- Confusing auto-suspend with auto-resume
- Thinking multi-cluster reduces cost automatically
- Assuming data sharing affects compute cost
Solution
Step 1: Understand AUTO_SUSPEND unit
Snowflake expects AUTO_SUSPEND value in seconds, so 5 minutes = 300 seconds.Step 2: Check syntax correctness
ALTER WAREHOUSE my_wh SET AUTO_SUSPEND = 300; uses correct syntax and value (300 seconds). Others use wrong units or types.Final Answer:
ALTER WAREHOUSE my_wh SET AUTO_SUSPEND = 300; -> Option CQuick Check:
Auto-suspend time in seconds = 300 [OK]
- Using minutes instead of seconds
- Setting AUTO_SUSPEND to TRUE/FALSE
- Using string values instead of integers
ALTER WAREHOUSE wh1 SET AUTO_SUSPEND = 600; ALTER WAREHOUSE wh1 SET AUTO_RESUME = TRUE;
What is the effect on cost when no queries run for 15 minutes?
Solution
Step 1: Analyze AUTO_SUSPEND setting
Warehouse suspends after 600 seconds (10 minutes) of inactivity.Step 2: Understand AUTO_RESUME behavior
Warehouse auto-resumes on new queries, but here no queries run for 15 minutes.Step 3: Calculate cost impact
Charges stop after suspension at 10 minutes; no charges for last 5 minutes idle.Final Answer:
Warehouse suspends after 10 minutes, no charges after that -> Option AQuick Check:
Auto-suspend 600s stops charges after 10 min idle [OK]
- Assuming warehouse suspends immediately after last query
- Thinking charges continue for full 15 minutes
- Confusing auto-resume with auto-suspend timing
AUTO_SUSPEND = 0 and AUTO_RESUME = TRUE. What issue might this cause?Solution
Step 1: Understand AUTO_SUSPEND = 0 meaning
Setting AUTO_SUSPEND to 0 disables suspension; warehouse runs continuously.Step 2: Check AUTO_RESUME effect
AUTO_RESUME TRUE allows auto start but does not suspend warehouse automatically.Step 3: Identify cost impact
Warehouse never suspends, so charges accrue continuously even if idle.Final Answer:
Warehouse never suspends, causing continuous charges -> Option BQuick Check:
AUTO_SUSPEND=0 means no suspend, costs continue [OK]
- Thinking 0 means immediate suspend
- Assuming AUTO_RESUME disables charges
- Believing default suspend time applies
Solution
Step 1: Understand scheduling needs
Warehouse should run only during business hours to save costs outside those times.Step 2: Evaluate options
Use auto-suspend with 1-minute timeout and schedule warehouse start/stop tasks at 9am and 5pm combines auto-suspend (quick suspend) and scheduled start/stop tasks to control usage precisely.Step 3: Rule out other options
Set warehouse to always on and rely on auto-resume to start queries wastes cost by always on; C is manual and error-prone; D increases cost for peak handling.Final Answer:
Use auto-suspend with 1-minute timeout and schedule warehouse start/stop tasks at 9am and 5pm -> Option DQuick Check:
Scheduled start/stop + auto-suspend = cost control [OK]
- Leaving warehouse always on wastes money
- Relying on manual suspend risks forgetting
- Using multi-cluster increases cost, not reduces
Ready to prove it?
Pass the test and this pattern moves unknown
