What are Snowflake Query Tags?

Published
July 5, 2024
Author

Snowflake query tags are descriptive string labels up to 2000 characters long that can be attached to individual queries or groups of queries to help organize and categorize them for tracking and reporting purposes. These tags facilitate query tracking, tuning, and optimization within the Snowflake ecosystem.

Snowflake Query Tag Uses:

  • Name: Assign meaningful names to queries for easy identification.
  • Group: Categorize queries based on different criteria such as functionality, project, or user.
  • Track: Monitor query performance, usage patterns, and optimization efforts.

Configuring Snowflake Query Tags

Snowflake query tags can be configured at various levels to provide flexibility and granular control:

  • Account-Level: Applies to all queries across the account.
  • User-Level: Overrides account-level tags for specific users.
  • Session-Level: Overrides both account and user-level tags for queries in a session.

How Do Query Tags Differ from Object Tags?

Query tags are specifically designed to be attached to queries. They help in organizing and categorizing queries to facilitate performance optimization and tracking. In contrast, object tags are attached to database objects such as tables, views, and schemas. They are used for data management, access control, and data retention policies.

Feature Query Tags Object Tags Purpose Organize and track queries Manage and control data objects Attachment Queries Database objects (tables, views) Usage Performance optimization Data management and policies

How to Add a Query Tag in Snowflake?

1. Log in and create a worksheet

Begin by logging into your Snowflake account and creating a worksheet where you will execute your SQL commands.

2. Create a database

CREATE DATABASE awesome_database;

This command creates a new database named awesome_database.

3. Use the database

USE DATABASE awesome_database;

This command sets the context to the newly created database.

4. Create a warehouse

CREATE WAREHOUSE awesome_warehouse;

This command creates a new warehouse named awesome_warehouse.

5. Switch to the warehouse

USE WAREHOUSE awesome_warehouse;

This command sets the context to the newly created warehouse.

6. Create a table

CREATE TABLE awesome_table (
id INT,
name VARCHAR,
age INT
);

This command creates a new table named awesome_table with columns for id, name, and age.

7. Insert sample data

INSERT INTO awesome_table VALUES (1, 'Johnny', 25), (2, 'Happy', 30), (3, 'Chaos', 40);

This command inserts sample data into the awesome_table.

8. Set up session-level query tag

ALTER SESSION SET QUERY_TAG = 'Some_query_tag_name';

This command sets a session-level query tag named Some_query_tag_name.

9. Run a query with the tag

SELECT * FROM awesome_table;

This command runs a query to select all data from the awesome_table with the session-level query tag applied.

10. Verify the query tag

SELECT QUERY_TAG FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY()) WHERE QUERY_TEXT LIKE 'SELECT * FROM awesome_table%';

This command verifies the query tag by checking the query history.

How to Tag Queries by User, Account, and Session Levels?

Account-level query tags apply to all queries within the account, user-level query tags override account-level tags for specific users, and session-level query tags have the highest priority and override both account and user-level tags.

  • Account-Level Query Tagging: USE ROLE ACCOUNTADMIN;
    ALTER ACCOUNT SET QUERY_TAG = 'Account_level_query_tag';
  • User-Level Query Tagging: User-level query tags override account-level tags for specific users, allowing for more granular control over query tagging.
  • Session-Level Query Tagging: Session-level query tags have the highest priority and override both account and user-level tags, useful for tagging queries in a specific session.

What is the Order of Precedence for Query Tags?

The order of precedence for query tags ensures that the most specific tag is applied:

Level Priority Session Highest User Overrides account-level if set Account Default if no user or session tags

Common Challenges and Solutions

  • Ensuring correct tag precedence: Always verify the order of precedence to ensure the correct tag is applied.
  • Tagging consistency: Maintain a consistent tagging strategy across the organization to avoid confusion.
  • Performance monitoring: Regularly monitor query performance to ensure tags are being used effectively for optimization.

Recap

  • Snowflake query tags help in organizing and categorizing queries for better tracking and optimization.
  • Tags can be applied at account, user, and session levels, with session-level having the highest priority.
  • Maintaining a consistent tagging strategy is crucial for effective query performance monitoring and optimization.

Keep reading

See all