Bird
Raised Fist0
Snowflakecloud~5 mins

Query history and profiling in Snowflake - Time & Space Complexity

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Time Complexity: Query history and profiling
O(n)
Understanding Time Complexity

When we look at query history and profiling in Snowflake, we want to know how the time to get this data changes as we ask for more records.

We ask: How does the effort grow when we request more query history entries?

Scenario Under Consideration

Analyze the time complexity of the following operation sequence.


SELECT query_id, user_name, start_time, total_elapsed_time
FROM snowflake.account_usage.query_history
WHERE start_time > DATEADD(day, -7, CURRENT_TIMESTAMP())
ORDER BY start_time DESC
LIMIT 1000;
    

This query fetches the last 1000 queries run in the past week, ordered by start time.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Reading rows from the query_history table in the account_usage schema.
  • How many times: The system scans or filters rows for the past 7 days, then returns up to the requested limit (e.g., 1000 rows).
How Execution Grows With Input

As you ask for more query history rows, the system reads more data to find and return those rows.

Input Size (n)Approx. API Calls/Operations
10Reads enough rows to find 10 recent queries.
100Reads more rows to find 100 recent queries.
1000Reads even more rows to find 1000 recent queries.

Pattern observation: The work grows roughly in direct proportion to how many rows you request.

Final Time Complexity

Time Complexity: O(n)

This means the time to get query history grows linearly with the number of rows requested.

Common Mistake

[X] Wrong: "Getting query history is always instant no matter how many rows I ask for."

[OK] Correct: More rows mean more data to scan and transfer, so it takes more time.

Interview Connect

Understanding how data retrieval scales helps you design efficient monitoring and troubleshooting tools in real projects.

Self-Check

"What if we added a filter on user_name to only get queries from one user? How would the time complexity change?"

Practice

(1/5)
1. What is the main purpose of the QUERY_HISTORY view in Snowflake?
easy
A. To see details of past queries executed in the system
B. To create new tables and schemas
C. To manage user permissions and roles
D. To monitor network traffic between Snowflake and clients

Solution

  1. Step 1: Understand the role of QUERY_HISTORY

    The QUERY_HISTORY view stores information about queries that have already run, including their text, execution time, and status. Its main purpose is to see details of past queries executed in the system.
  2. Final Answer:

    To see details of past queries executed in the system -> Option A
  3. Quick Check:

    QUERY_HISTORY = past query details [OK]
Hint: QUERY_HISTORY shows past queries and their info [OK]
Common Mistakes:
  • Confusing QUERY_HISTORY with user management
  • Thinking it manages network or security settings
  • Assuming it creates or modifies database objects
2. Which SQL clause correctly filters queries executed by a specific user in the QUERY_HISTORY view?
easy
A. FILTER BY USER = 'john_doe'
B. WHERE USER_NAME = 'john_doe'
C. SELECT USER_NAME FROM QUERY_HISTORY WHERE 'john_doe'
D. HAVING USER_NAME = 'john_doe'

Solution

  1. Step 1: Recall SQL filtering syntax

    To filter rows in SQL, the WHERE clause is used with a condition like USER_NAME = 'value'. WHERE USER_NAME = 'john_doe' is valid and standard SQL syntax.
  2. Final Answer:

    WHERE USER_NAME = 'john_doe' -> Option B
  3. Quick Check:

    Filter with WHERE clause = WHERE USER_NAME = 'john_doe' [OK]
Hint: Use WHERE to filter rows by user name [OK]
Common Mistakes:
  • Using FILTER BY instead of WHERE
  • Misplacing HAVING without GROUP BY
  • Incorrect SELECT syntax without WHERE
3. Given the query:
SELECT query_text, total_elapsed_time FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) WHERE execution_status = 'FAILED' ORDER BY start_time DESC LIMIT 1;

What does this query return?
medium
A. The most recent failed query's text and its total elapsed time
B. All successful queries ordered by start time
C. The oldest failed query's text and elapsed time
D. An error because QUERY_HISTORY is not a table

Solution

  1. Step 1: Analyze the query clauses

    The query uses TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) to access query history, filters for execution_status = 'FAILED', orders by start_time DESC (most recent first), and limits to 1 row, returning the most recent failed query's text and total elapsed time.
  2. Final Answer:

    The most recent failed query's text and its total elapsed time -> Option A
  3. Quick Check:

    Filter failed + order desc + limit 1 = most recent failed query [OK]
Hint: ORDER BY DESC + LIMIT 1 gets latest record [OK]
Common Mistakes:
  • Thinking QUERY_HISTORY is a normal table
  • Confusing oldest vs most recent due to ORDER BY
  • Ignoring the WHERE filter on execution_status
4. You wrote this query to find slow queries but it returns no results:
SELECT query_text FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) WHERE total_elapsed_time > 1000;

What is the likely issue?
medium
A. The TABLE() function cannot be used with QUERY_HISTORY
B. QUERY_HISTORY does not have total_elapsed_time column
C. The query_text column cannot be selected from QUERY_HISTORY
D. total_elapsed_time is in microseconds, so 1000 is too small a threshold

Solution

  1. Step 1: Check the unit of total_elapsed_time

    In Snowflake, total_elapsed_time is measured in microseconds, so 1000 microseconds (1 millisecond) is too small a threshold, and few or no queries exceed it, resulting in no results.
  2. Final Answer:

    total_elapsed_time is in microseconds, so 1000 is too small a threshold -> Option D
  3. Quick Check:

    Elapsed time unit = microseconds, threshold too low [OK]
Hint: Check units: elapsed time is microseconds, not milliseconds [OK]
Common Mistakes:
  • Assuming elapsed time is in seconds or milliseconds
  • Thinking QUERY_HISTORY lacks columns
  • Misusing TABLE() function syntax
5. You want to profile query performance by grouping queries by user and calculating average execution time. Which query correctly achieves this?
hard
A. SELECT user_name, SUM(total_elapsed_time) FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) WHERE execution_status = 'SUCCESS';
B. SELECT user_name, total_elapsed_time FROM QUERY_HISTORY GROUP BY user_name;
C. SELECT user_name, AVG(total_elapsed_time) AS avg_time FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) GROUP BY user_name ORDER BY avg_time DESC;
D. SELECT user_name, AVG(total_elapsed_time) FROM QUERY_HISTORY WHERE total_elapsed_time > 1000 ORDER BY user_name;

Solution

  1. Step 1: Identify correct aggregation and grouping

    To get average execution time per user, use AVG(total_elapsed_time) with GROUP BY user_name from TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()), as in SELECT user_name, AVG(total_elapsed_time) AS avg_time ... GROUP BY user_name ORDER BY avg_time DESC.
  2. Final Answer:

    SELECT user_name, AVG(total_elapsed_time) AS avg_time FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) GROUP BY user_name ORDER BY avg_time DESC; -> Option C
  3. Quick Check:

    Group by user + AVG + ORDER BY avg_time = SELECT user_name, AVG(total_elapsed_time) AS avg_time FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) GROUP BY user_name ORDER BY avg_time DESC; [OK]
Hint: Use GROUP BY user_name with AVG for profiling [OK]
Common Mistakes:
  • Missing GROUP BY when using aggregation
  • Selecting columns without aggregation
  • Not using TABLE() function for QUERY_HISTORY