For Day 20 of this series, we start out with Query #46<\/strong>, which is Query Execution Counts. This query retrieves information from the sys.dm_exec_query_stats<\/a> dynamic management view, the sys.dm_exec_sql_text<\/a> dynamic management function, and the sys.dm_exec_query_plan<\/a> dynamic management function about the most frequently executed cached queries in the current database. Query #46 is shown in Figure 1.<\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/div>\n<\/div>\n Figure 1: Query #46 Query Execution Counts<\/strong><\/p>\n This query shows you which cached queries (which might be part of a stored procedure or not) are being called the most often. This is useful as a part of understanding the nature of your workload. Keep in mind that just because a query is called a lot does not necessarily mean that it is a key part of your workload. It might be, but it could be that it is not actually that expensive for individual calls or cumulatively. You will need to look at the other metrics for that query to determine that.<\/p>\n You may notice that I have one line of this query commented out. This is because Excel does not deal very well with large quantities of text or XML. If you are working with this in real time, you should probably uncomment that line, so you see the extra information that it retrieves.<\/p>\n <\/p>\n Query #47<\/strong> is SP Execution Counts. This query retrieves information from the sys.procedures<\/a> object catalog view and the sys.dm_exec_procedure_stats<\/a> dynamic management view about the most frequently executed cached stored procedures in the current database. Query #47 is shown in Figure 2.<\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/p>\n <\/div>\n<\/div>\n Figure 2: Query #47 SP Execution Counts<\/strong><\/p>\n This query shows you which stored procedures with cached query plans are being called the most often. This helps you understand the nature and magnitude of your workload. Ideally, you should have a general idea of what your normal workload looks like, in terms of how many calls\/minute or per second you are seeing for your top stored procedures. <\/p>\n If this rate suddenly changes, you would want to investigate further to understand what might have happened. Understanding which stored procedures are called the most often, can also help you identify possible candidates for middle-tier caching.<\/p>\n","protected":false},"excerpt":{"rendered":" For Day 20 of this series, we start out with Query #46, which is Query Execution Counts. This query retrieves information from the sys.dm_exec_query_stats dynamic management view, the sys.dm_exec_sql_text dynamic management function, and the sys.dm_exec_query_plan dynamic management function about the most frequently executed cached queries in the current database. Query #46 is shown in Figure […]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[272],"class_list":["post-1048","post","type-post","status-publish","format-standard","hentry","category-dmv-diagnostic-queries-detailed","tag-dmv-diagnostic-queries-detailed"],"yoast_head":"\n 1:<\/span> -- Get most frequently executed queries for this database (Query 46) (Query Execution Counts)<\/span><\/pre>\n 2:<\/span> SELECT<\/span> TOP<\/span>(50) LEFT<\/span>(t.[text][\/text], 50) AS<\/span> [Short Query Text], qs.execution_count AS<\/span> [Execution Count<\/span>],<\/pre>\n 3:<\/span> qs.total_logical_reads AS<\/span> [Total Logical Reads<\/span>],<\/pre>\n 4:<\/span> qs.total_logical_reads\/qs.execution_count AS<\/span> [Avg<\/span> Logical Reads<\/span>],<\/pre>\n 5:<\/span> qs.total_worker_time AS<\/span> [Total Worker Time<\/span>],<\/pre>\n 6:<\/span> qs.total_worker_time\/qs.execution_count AS<\/span> [Avg<\/span> Worker Time<\/span>], <\/pre>\n 7:<\/span> qs.total_elapsed_time AS<\/span> [Total Elapsed Time<\/span>],<\/pre>\n 8:<\/span> qs.total_elapsed_time\/qs.execution_count AS<\/span> [Avg<\/span> Elapsed Time<\/span>], <\/pre>\n 9:<\/span> qs.creation_time AS<\/span> [Creation Time<\/span>]<\/pre>\n 10:<\/span> --,t.[text][\/text] AS<\/span> [Complete Query Text], qp.query_plan AS<\/span> [Query Plan<\/span>] -- uncomment out these columns if not copying results to Excel<\/span><\/pre>\n 11:<\/span> FROM<\/span> sys.dm_exec_query_stats AS<\/span> qs WITH<\/span> (NOLOCK)<\/pre>\n 12:<\/span> CROSS<\/span> APPLY sys.dm_exec_sql_text(plan_handle) AS<\/span> t <\/pre>\n 13:<\/span> CROSS<\/span> APPLY sys.dm_exec_query_plan(plan_handle) AS<\/span> qp <\/pre>\n 14:<\/span> WHERE<\/span> t.dbid = DB_ID()<\/pre>\n 15:<\/span> ORDER<\/span> BY<\/span> qs.execution_count DESC<\/span> OPTION<\/span> (RECOMPILE);<\/pre>\n 1:<\/span> -- Top Cached SPs By Execution Count (Query 47) (SP Execution Counts)<\/span><\/pre>\n 2:<\/span> SELECT<\/span> TOP<\/span>(100) p.name AS<\/span> [SP Name], qs.execution_count,<\/pre>\n 3:<\/span> ISNULL(qs.execution_count\/DATEDIFF(Minute<\/span>, qs.cached_time, GETDATE()), 0) AS<\/span> [Calls\/Minute<\/span>],<\/pre>\n 4:<\/span> qs.total_worker_time\/qs.execution_count AS<\/span> [AvgWorkerTime], qs.total_worker_time AS<\/span> [TotalWorkerTime], <\/pre>\n 5:<\/span> qs.total_elapsed_time, qs.total_elapsed_time\/qs.execution_count AS<\/span> [avg_elapsed_time],<\/pre>\n 6:<\/span> qs.cached_time<\/pre>\n 7:<\/span> FROM<\/span> sys.procedures AS<\/span> p WITH<\/span> (NOLOCK)<\/pre>\n 8:<\/span> INNER<\/span> JOIN<\/span> sys.dm_exec_procedure_stats AS<\/span> qs WITH<\/span> (NOLOCK)<\/pre>\n 9:<\/span> ON<\/span> p.[object_id] = qs.[object_id]<\/pre>\n 10:<\/span> WHERE<\/span> qs.database_id = DB_ID()<\/pre>\n 11:<\/span> ORDER<\/span> BY<\/span> qs.execution_count DESC<\/span> OPTION<\/span> (RECOMPILE);<\/pre>\n 12:<\/span> <\/pre>\n 13:<\/span> -- Tells you which cached stored procedures are called the most often<\/span><\/pre>\n 14:<\/span> -- This helps you characterize and<\/span> baseline your workload<\/pre>\n