共用方式為


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 主索引鍵。 參數化敏感性查詢變體的標識碼。
parent_query_id bigint 原始參數化查詢的標識碼。
dispatcher_plan_id bigint 參數敏感性計劃優化發送器計劃的標識碼。

備註

由於多個查詢變體可以與一個發送器計劃相關聯,因此會有多個屬於查詢變體的計劃,最終會新增至父查詢的整體資源使用量統計數據。 查詢變體的發送器計劃不會在 查詢存放區 中產生任何運行時間統計數據,這會導致收集整體統計數據時,現有的 查詢存放區 查詢不再足夠,除非包含其他聯結至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

另請參閱