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?