Query Tags In Snowflake

Authors: Tejaswini R & Twinkle Viswanathan


Ever written a query and when you needed it you couldn’t find it? Need to group a set of queries and reuse it again? Mapping your queries with Query Tags would be an easy solution.


What are Query Tags?

Query tags are Snowflake parameters that help us to tag queries that can be set for an account, user or session that can be unset at any time. Query tags help us to map queries that are executed based on a user or a session where we execute a group of related queries together.


Query tag precedence

Account » User » Session.


Whenever a query tag is set for an account but a new query tag is set for a user in the account; the user level query tag replaces the account level query tag for the queries that this user executes; similarly when a query tag is set for a session but a query tag is already set for the user or account; the session-level query tag overrides the user level or account level query tag.


User-level query tag - sample scenario

Query tags can be handy in quite a handful of situations; Consider a scenario where you need to find the queries executed by a particular department, but the department has many users. Setting the department name query tag for each user in the department and we can find all the queries executed by that department.





An example where query execution time is analyzed for users from the same department


Session-level query tag - sample scenario

Session level query tags can be useful when we need to group all the queries that we used to solve an issue; they can be set up before running critical queries which if gone wrong may require time travel with the use of query tags it’ll be easy for us to find the exact query to use for time travel. For example, we created a session-level query tag called ‘tag creation’ before we set up object tagging in our account, so all the queries related to that can be grouped and reused easily.




Note: When a user sets a session-level Query Tag, the user-level Query Tag set for the User is overridden. So these queries with session-level query tags would not be present if we’re monitoring the queries based on users.


Conclusion:

Thus, query tags can be used for query management which groups queries which makes it easy to be retrieved later, resource management by grouping queries together and help us get insights on total resource utilization which could even be used for unit wise chargeback. For example, to calculate the total resource consumed by each department and thus helping us in effective query management.


References:


120 views0 comments

Recent Posts

See All