Credit usage monitoring in Snowflake - Time & Space Complexity
When monitoring credit usage in Snowflake, it's important to understand how the time to gather usage data changes as the amount of data grows.
We want to know how the execution time scales when querying credit usage information.
Analyze the time complexity of the following Snowflake query that retrieves credit usage details.
SELECT
WAREHOUSE_NAME,
SUM(CREDITS_USED) AS TOTAL_CREDITS
FROM
SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY
WHERE
START_TIME >= DATEADD(day, -7, CURRENT_TIMESTAMP())
GROUP BY
WAREHOUSE_NAME;
This query sums the credits used by each warehouse over the last 7 days.
Look for repeated actions in the query.
- Primary operation: Scanning rows in the WAREHOUSE_METERING_HISTORY table.
- How many times: Once for each row in the last 7 days.
The number of rows scanned grows with the amount of usage data stored.
| Input Size (rows) | Approx. Operations |
|---|---|
| 10 | 10 scans and sums |
| 100 | 100 scans and sums |
| 1000 | 1000 scans and sums |
Pattern observation: The work grows directly with the number of rows scanned.
Time Complexity: O(n)
This means the time to get credit usage grows linearly with the number of usage records.
[X] Wrong: "The query time stays the same no matter how much data there is."
[OK] Correct: The query must scan more rows as data grows, so it takes longer.
Understanding how query time grows with data size helps you design efficient monitoring and alerting in real systems.
What if we added a clustering key on START_TIME? How would the time complexity change?
Practice
Solution
Step 1: Understand credit usage concept
Credit usage monitoring tracks how many credits Snowflake consumes, which directly affects billing.Step 2: Identify the goal of monitoring
The goal is to control and optimize costs by understanding usage patterns.Final Answer:
To control and optimize Snowflake costs -> Option AQuick Check:
Credit monitoring = Cost control [OK]
- Confusing credit monitoring with performance tuning
- Thinking it automatically scales warehouses
- Assuming it backs up data
Solution
Step 1: Identify views related to credit usage
ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY tracks credit consumption by warehouses.Step 2: Exclude unrelated views
INFORMATION_SCHEMA.TABLES shows table info, LOGIN_HISTORY tracks logins, QUERY_HISTORY tracks queries but not credit usage.Final Answer:
ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY -> Option BQuick Check:
Warehouse credit data = WAREHOUSE_METERING_HISTORY [OK]
- Choosing QUERY_HISTORY which tracks queries, not credits
- Confusing LOGIN_HISTORY with credit usage
- Selecting INFORMATION_SCHEMA views unrelated to credits
SELECT WAREHOUSE_NAME, SUM(CREDITS_USED) AS TOTAL_CREDITS FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY GROUP BY WAREHOUSE_NAME ORDER BY TOTAL_CREDITS DESC;
Solution
Step 1: Analyze the SELECT and GROUP BY
The query sums credits used grouped by warehouse name.Step 2: Understand ORDER BY clause
Ordering by TOTAL_CREDITS DESC sorts warehouses from highest to lowest credit usage.Final Answer:
Total credits used by each warehouse, sorted from highest to lowest -> Option CQuick Check:
SUM credits by warehouse, ordered DESC = Total credits used by each warehouse, sorted from highest to lowest [OK]
- Thinking it shows user credit usage
- Assuming it lists running queries
- Believing missing WHERE causes syntax error
SELECT WAREHOUSE_NAME, SUM(CREDITS_USED) FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY WHERE START_TIME > CURRENT_DATE() GROUP BY WAREHOUSE_NAME;
What is the likely cause?
Solution
Step 1: Check the WHERE condition
Filtering START_TIME > CURRENT_DATE() means only today's data is shown.Step 2: Consider data availability
If no credits used today yet, query returns empty result, not error.Final Answer:
No credit usage recorded today yet -> Option DQuick Check:
Empty result = no data matching date filter [OK]
- Assuming missing alias causes error
- Thinking table name is wrong
- Believing GROUP BY is missing
Solution
Step 1: Identify correct view for service type credit data
ACCOUNT_USAGE.METERING_HISTORY contains credit usage by service type; WAREHOUSE_METERING_HISTORY is only for warehouses.Step 2: Check query filters and grouping
Filtering last 7 days with DATEADD and grouping by SERVICE_TYPE is correct; ordering by total credits helps see highest usage.Final Answer:
SELECT SERVICE_TYPE, SUM(CREDITS_USED) AS TOTAL_CREDITS FROM SNOWFLAKE.ACCOUNT_USAGE.METERING_HISTORY WHERE START_TIME >= DATEADD(day, -7, CURRENT_DATE()) GROUP BY SERVICE_TYPE ORDER BY TOTAL_CREDITS DESC; -> Option AQuick Check:
Use METERING_HISTORY + SERVICE_TYPE + last 7 days = SELECT SERVICE_TYPE, SUM(CREDITS_USED) AS TOTAL_CREDITS FROM SNOWFLAKE.ACCOUNT_USAGE.METERING_HISTORY WHERE START_TIME >= DATEADD(day, -7, CURRENT_DATE()) GROUP BY SERVICE_TYPE ORDER BY TOTAL_CREDITS DESC; [OK]
- Using WAREHOUSE_METERING_HISTORY for service types
- Not grouping by SERVICE_TYPE
- Selecting credits without aggregation
Ready to prove it?
Pass the test and this pattern moves unknown
