Snowflake Query History

Overview

Every time a query is executed, information about its execution is logged into the metadata layer of Snowflake. In this section, we learn how to get Snowflake query history data.

Explanation

You can query this data, either through a query – if you have the appropriate permissions – or through the user interface.

History in the User Interface

When you’ve executed at least one query in a worksheet, you can click on the clock icon on the right-side of the window.

open history

This will open a new pane, where you can view the history of the queries you executed in the worksheet.

query history pane

You get the timestamp, the duration and the SQL query text of the statements you executed. At the top of the pane, there are some options to filter the list. In the Query Details pane, you can find the query ID. When you click on this ID, it will take you to a new window with more detailed information of the query. It’s important to notice this also possible for queries that are still executing.

query history detail

If the query has finished successfully and a result set is returned, you can view it in the results pane.

query results in query history

Query Execution Plan

Ideal during debugging and you are wondering what your previous query returned before you made that change. If you click on Profile at the top, you are taken to the query execution plan. Here’s the query plan of the query we executed in the previous section of the tutorial:

query plan

If you click on one of the nodes, you get a bit more detail.

join node example

Unfortunately, the query plan is nowhere as detailed as in SQL Server.

If you’re interested in more than your own queries, you can go to the query history pane in the browser.

go to history

There you can view all queries executed in the Snowflake account, up to a certain period in time.

full history in browser

There are filters available for searching through the available Snowflake query history.

history filters
history filters bis

If you go to the query history detail of a query executed by another user, you cannot see the result set. This would otherwise be a serious security risk. You can however see the actual SQL statement that was executed.

Query Result Cache

An interesting feature of Snowflake is the result set cache. This means if an executed query is unchanged and the underlying tables aren’t modified, each subsequent execution of the same query will just pull the result set from cache. This is ideal for speeding up reports that execute the same queries repeatedly. How can you check if a query has retrieved the results from cache? If you go to the query history detail, you’ll see there’s no warehouse size displayed, meaning the query was essentially free.

result set cache verify

You can also check the query profile:

query cache in execution plan

Querying History

If you want more flexibility than the browser, or go further back in time, you can query the metadata database for the query history. If you have the correct permissions of course (by default ACCOUNTADMIN is needed). An example query can be the following:

SELECT *
FROM "SNOWFLAKE"."ACCOUNT_USAGE"."QUERY_HISTORY"
WHERE 1 = 1
    AND start_time >= '2024-01-01'
    AND database_name = 'TEST'
    AND USER_NAME = 'KOEN'
    AND EXECUTION_STATUS <> 'SUCCESS'
ORDER BY start_time DESC;

Additional Information

  • More information about the ACCOUNT_USAGE schema in the Snowflake database. You can query all sorts of information. You can for example create a report on the longest running query and how much compute was used (and thus how much money those queries costed).

Leave a Reply

Your email address will not be published. Required fields are marked *