适用于:
Databricks SQL
重要
此功能目前以公共预览版提供。
设置、读取或删除当前会话的查询标记。
查询标记是可应用于 SQL 工作负荷的自定义键值对,用于在表中启用分组、筛选和成本归因 system.query.history 。
Syntax
-- Set or update one or more query tags for the current session
SET QUERY_TAGS[<tag_key>] = <tag_value> [, ...]
-- Read the query tags defined on the session
SET QUERY_TAGS;
参数
tag_key
字符串文本。 去除前导空格和尾随空格。
tag_value
字符串字面值、
NULL或UNSET。- 删除前导空格和尾随空格。
- 如果剪裁的结果为空字符串,则视为“仅键标记”
NULL。 -
UNSET从查询标记中删除指定的键。
示例
-- Set two key-value pairs to annotate subsequent statement executions in this session.
> SET QUERY_TAGS['team'] = 'marketing', QUERY_TAGS['cost_center'] = '701';
-- Execute a query with the tags attached
> SELECT * FROM sales_data;
-- Update tags: change team value, add new tag, and remove cost_center tag
> SET QUERY_TAGS['team'] = 'engineering', QUERY_TAGS['env'] = 'prod', QUERY_TAGS['cost_center'] = UNSET;
-- Display all tags currently set on the session
> SET QUERY_TAGS;
key value
---------- -----------
team engineering
env prod
-- Set a key-only tag (value is NULL)
> SET QUERY_TAGS['experiment'] = NULL;
-- Execute another query with the updated tags
-- This statement execution will be tagged with team:engineering, env:prod, experiment
> SELECT * FROM sales_data;