Does SQL cache the entire TSQL batch or each individual TFS statement in a batch? For cache match purposes, does it match on the entire batch or each statement within the batch will attempt to match a previously cached plan?
When I batch together two TSQL queries in mgmt studio and query the dmvs (dm_exec views/functions), I get two separate rows back where each row has a a different plan_handle, sql_handle, query_hash,query_plan_hash, and text. The text for each row represents
a single query statement than the entire batch as the MSDN docs suggest.
select * from mytable1 where id = 1
select * from mytable2 where id = 2
go
SELECTcp.objtype
,qs.plan_handle
,qs.SQL_HANDLE
,QS.query_hash
,QS.query_plan_hash
,ST.[TEXT]
,cp.usecounts
,QS.EXECUTION_COUNT
,qs.total_physical_reads
,qs.total_logical_reads
,P.query_plan
FROM [SYS].[DM_EXEC_QUERY_STATS] AS [QS]
INNER JOIN SYS.dm_exec_cached_plans cp on cp.plan_handle = qs.plan_handle
CROSS APPLY [SYS].[DM_EXEC_SQL_TEXT]([QS].[SQL_HANDLE]) AS [ST]
CROSS APPLY [sys].[dm_exec_query_plan]([qs].[plan_handle]) as [p]
WHERE [st].[text] like '%mytable1%' or [st].[text] like '%mytable2%'
ORDER BY 1, [qs].[execution_count] desc;
go
The MSDN docs suggest that sql handle from dm_exec_query_stats represent a given TSQL batch of statements. For caching purposes what constitutes a batch?
SQL2008