What if you could instantly spot the slowest parts of your database without guessing?
Why Query history and profiling in Snowflake? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you run a busy restaurant and want to know which dishes customers order most and how long each takes to prepare.
Without a system, you'd have to remember every order and cooking time yourself.
Trying to track every query manually is like writing down every dish order on scraps of paper.
It's slow, easy to lose information, and hard to spot patterns or problems.
Query history and profiling tools automatically record every query run and how long it took.
This helps you quickly see which queries are slow or used most, just like a digital kitchen logbook.
SELECT * FROM queries WHERE time > 'yesterday'; -- manually check logsSELECT query_text, total_elapsed_time FROM table(information_schema.query_history()) WHERE start_time > DATEADD(day, -1, CURRENT_TIMESTAMP());It lets you easily find slow queries and optimize your database performance like a chef improving kitchen speed.
A data analyst notices a report runs slowly every morning.
Using query profiling, they find a specific query causing delays and fix it, speeding up the report.
Manual tracking of queries is slow and error-prone.
Query history automatically logs all queries and their details.
This helps find and fix slow or costly queries quickly.
Practice
QUERY_HISTORY view in Snowflake?Solution
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.Final Answer:
To see details of past queries executed in the system -> Option AQuick Check:
QUERY_HISTORY = past query details [OK]
- Confusing QUERY_HISTORY with user management
- Thinking it manages network or security settings
- Assuming it creates or modifies database objects
QUERY_HISTORY view?Solution
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.Final Answer:
WHERE USER_NAME = 'john_doe' -> Option BQuick Check:
Filter with WHERE clause = WHERE USER_NAME = 'john_doe' [OK]
- Using FILTER BY instead of WHERE
- Misplacing HAVING without GROUP BY
- Incorrect SELECT syntax without WHERE
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?
Solution
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.Final Answer:
The most recent failed query's text and its total elapsed time -> Option AQuick Check:
Filter failed + order desc + limit 1 = most recent failed query [OK]
- Thinking QUERY_HISTORY is a normal table
- Confusing oldest vs most recent due to ORDER BY
- Ignoring the WHERE filter on execution_status
SELECT query_text FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) WHERE total_elapsed_time > 1000;
What is the likely issue?
Solution
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.Final Answer:
total_elapsed_time is in microseconds, so 1000 is too small a threshold -> Option DQuick Check:
Elapsed time unit = microseconds, threshold too low [OK]
- Assuming elapsed time is in seconds or milliseconds
- Thinking QUERY_HISTORY lacks columns
- Misusing TABLE() function syntax
Solution
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.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 CQuick 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]
- Missing GROUP BY when using aggregation
- Selecting columns without aggregation
- Not using TABLE() function for QUERY_HISTORY
