sys.query_store_query_variant (Transact-SQL)
适用于: SQL Server 2022 (16.x)
包含有关原始参数化查询 (也称为父查询) 、调度程序计划及其子查询变体之间的父子关系的信息。 此目录视图提供查看与调度程序关联的所有查询变体以及原始参数化查询的功能。 查询变体的query_hash值与从sys.query_store_query目录视图中查看的值相同,当与sys.query_store_query_variant和sys.query_store_runtime_stats目录视图联接时,可以为仅根据其输入值不同的查询获取聚合资源使用情况统计信息。
列名称 | 数据类型 | 说明 |
---|---|---|
query_variant_query_id | bigint | 主密钥。 参数化敏感查询变体的 ID。 |
parent_query_id | bigint | 原始参数化查询的 ID。 |
dispatcher_plan_id | bigint | 参数敏感计划优化调度程序计划的 ID。 |
备注
由于多个查询变体可以与一个调度程序计划相关联,因此将有多个属于查询变体的计划,这些计划最终将添加到父查询的总体资源使用情况统计信息中。 查询变体的调度程序计划不会在查询存储中生成任何运行时统计信息,这将导致现有的查询存储查询在收集整体统计信息时不再足够,除非包含与query_store_query_variant视图的其他联接。
权限
需要 VIEW DATABASE STATE 权限。
示例
查看查询存储变体信息
SELECT
qspl.plan_type_desc AS query_plan_type,
qspl.plan_id as query_store_planid,
qspl.query_id as query_store_queryid,
qsqv.query_variant_query_id as query_store_variant_queryid,
qsqv.parent_query_id as query_store_parent_queryid,
qsqv.dispatcher_plan_id as query_store_dispatcher_planid,
OBJECT_NAME(qsq.object_id) as module_name,
qsq.query_hash,
qsqtxt.query_sql_text,
convert(xml,qspl.query_plan)as show_plan_xml,
qsrs.last_execution_time as last_execution_time,
qsrs.count_executions AS number_of_executions,
qsq.count_compiles AS number_of_compiles
FROM sys.query_store_runtime_stats AS qsrs
JOIN sys.query_store_plan AS qspl
ON qsrs.plan_id = qspl.plan_id
JOIN sys.query_store_query_variant qsqv
ON qspl.query_id = qsqv.query_variant_query_id
JOIN sys.query_store_query as qsq
ON qsqv.parent_query_id = qsq.query_id
JOIN sys.query_store_query_text AS qsqtxt
ON qsq.query_text_id = qsqtxt .query_text_id
ORDER BY qspl.query_id, qsrs.last_execution_time;
GO
查看查询存储调度程序和变体信息
SELECT
qspl.plan_type_desc AS query_plan_type,
qspl.plan_id as query_store_planid,
qspl.query_id as query_store_queryid,
qsqv.query_variant_query_id as query_store_variant_queryid,
qsqv.parent_query_id as query_store_parent_queryid,
qsqv.dispatcher_plan_id as query_store_dispatcher_planid,
qsq.query_hash,
qsqtxt.query_sql_text,
CONVERT(xml,qspl.query_plan)as show_plan_xml,
qsq.count_compiles AS number_of_compiles,
qsrs.last_execution_time as last_execution_time,
qsrs.count_executions AS number_of_executions
FROM sys.query_store_query qsq
LEFT JOIN sys.query_store_query_text qsqtxt
ON qsq.query_text_id = qsqtxt.query_text_id
LEFT JOIN sys.query_store_plan qspl
ON qsq.query_id = qspl.query_id
LEFT JOIN sys.query_store_query_variant qsqv
ON qsq.query_id = qsqv.query_variant_query_id
LEFT JOIN sys.query_store_runtime_stats qsrs
ON qspl.plan_id = qsrs.plan_id
LEFT JOIN sys.query_store_runtime_stats_interval qsrsi
ON qsrs.runtime_stats_interval_id = qsrsi.runtime_stats_interval_id
WHERE qspl.plan_type = 1 or qspl.plan_type = 2
ORDER BY qspl.query_id, qsrs.last_execution_time;
GO