Introduction
Whenever we need to run some queries in Snowflake web UI , we use worksheet(query editor) where we select the warehouse(compute) , database and the schema (optional). And when we run the query, the output is displayed in the results tab below.
UI is mostly used when we are doing development or testing out something, but once we are through with it and deployed all the objects, scripts/queries are run/executed without involving UI.
Irrespective of how the query was executed, after execution of each query (succeeded or failed), it get's an identifier for it i.e. unique query ID. This ID can be used to get all the details about that particular query.
Where can we find details of the query? It's the History Tab
Snowflake History Page
History page is an interface provided by Snowflake UI to see the details of all the queries executed till date (historical listing of queries) like result of the query, time it took to execute(duration), number of rows returned, the caller of the query(client info) , the query text etc.
Note: History has details of all queries executed in the last 14 days only, anything older than that is purged.
As can be seen in above image, there is a provision to filter the history results based on criteria.
You can have multiple filter criteria added, just click on plus sign and define the filter. In below example - you want to see only those queries which are specific to particular user, and status is failed.
Note : Applying filter doesn't change the returned columns, columns are fixed.
How Snowflake History Page works
Fetch history data from Snowflake without using UI
- QUERY_HISTORY_BY_SESSION
- QUERY_HISTORY_BY_USER
- QUERY_HISTORY_BY_WAREHOUSE
e.g., Say if we are only interested in getting Query_Text, Error_Code, Error_Description
Like wise based on your need you can create queries, you can also query on another views or join multiple views
from table(information_schema.task_history(RESULT_LIMIT=>10000, scheduled_time_range_start=>dateadd('hour',-1,current_timestamp())))t
LEFT JOIN table(information_schema.QUERY_HISTORY_BY_WAREHOUSE(RESULT_LIMIT =>10000, WAREHOUSE_NAME => 'WH_DEV')) q ON q.QUERY_ID = t.QUERY_ID
WHERE t.QUERY_ID IS NOT NULL AND t.STATE != 'EXECUTING'
Conclusion
We tried to get an understanding on how the queries which are executed on Snowflake can be monitored. How to get details about them and utilize them as per need.
If there is any approach you are aware of, feel free to share in comments.
Knowledge Sharing is Caring !!!!!!