|
| 1 | +/* |
| 2 | +Author: Kendra Little |
| 3 | +Original link: https://www.littlekendra.com/2017/01/24/how-to-find-queries-using-an-index-and-queries-using-index-hints/ |
| 4 | +*/ |
| 5 | + |
| 6 | +--Search for queries in the execution plan cache |
| 7 | +--Simply plug the name of the index you’re looking for into this query. If you have multiple databases with the same index name, |
| 8 | +--you’ll need to add additional criteria to get just the database you’re looking for. |
| 9 | +SELECT |
| 10 | + querystats.plan_handle, |
| 11 | + querystats.query_hash, |
| 12 | + SUBSTRING(sqltext.text, (querystats.statement_start_offset / 2) + 1, |
| 13 | + (CASE querystats.statement_end_offset |
| 14 | + WHEN -1 THEN DATALENGTH(sqltext.text) |
| 15 | + ELSE querystats.statement_end_offset |
| 16 | + END - querystats.statement_start_offset) / 2 + 1) AS sqltext, |
| 17 | + querystats.execution_count, |
| 18 | + querystats.total_logical_reads, |
| 19 | + querystats.total_logical_writes, |
| 20 | + querystats.creation_time, |
| 21 | + querystats.last_execution_time, |
| 22 | + CAST(query_plan AS xml) as plan_xml |
| 23 | +FROM sys.dm_exec_query_stats as querystats |
| 24 | +CROSS APPLY sys.dm_exec_text_query_plan |
| 25 | + (querystats.plan_handle, querystats.statement_start_offset, querystats.statement_end_offset) |
| 26 | + as textplan |
| 27 | +CROSS APPLY sys.dm_exec_sql_text(querystats.sql_handle) AS sqltext |
| 28 | +WHERE |
| 29 | + textplan.query_plan like '%PK_Sales_Invoices%' |
| 30 | +ORDER BY querystats.last_execution_time DESC |
| 31 | +OPTION (RECOMPILE); |
| 32 | +GO |
| 33 | + |
| 34 | + |
| 35 | +--Find queries using the index in Query Store |
| 36 | +--If you’ve enabled the SQL Server 2016+ Query Store on your databases, you’ve got something better to search than the plan cache |
| 37 | +SELECT |
| 38 | + qsq.query_id, |
| 39 | + qsq.query_hash, |
| 40 | + (SELECT TOP 1 qsqt.query_sql_text FROM sys.query_store_query_text qsqt |
| 41 | + WHERE qsqt.query_text_id = MAX(qsq.query_text_id)) AS sqltext, |
| 42 | + SUM(qrs.count_executions) AS execution_count, |
| 43 | + SUM(qrs.count_executions) * AVG(qrs.avg_logical_io_reads) as est_logical_reads, |
| 44 | + SUM(qrs.count_executions) * AVG(qrs.avg_logical_io_writes) as est_writes, |
| 45 | + MIN(qrs.last_execution_time AT TIME ZONE 'Pacific Standard Time') as min_execution_time_PST, |
| 46 | + MAX(qrs.last_execution_time AT TIME ZONE 'Pacific Standard Time') as last_execution_time_PST, |
| 47 | + SUM(qsq.count_compiles) AS sum_compiles, |
| 48 | + TRY_CONVERT(XML, (SELECT TOP 1 qsp2.query_plan from sys.query_store_plan qsp2 |
| 49 | + WHERE qsp2.query_id=qsq.query_id |
| 50 | + ORDER BY qsp2.plan_id DESC)) AS query_plan |
| 51 | +FROM sys.query_store_query qsq |
| 52 | +JOIN sys.query_store_plan qsp on qsq.query_id=qsp.query_id |
| 53 | +CROSS APPLY (SELECT TRY_CONVERT(XML, qsp.query_plan) AS query_plan_xml) AS qpx |
| 54 | +JOIN sys.query_store_runtime_stats qrs on qsp.plan_id = qrs.plan_id |
| 55 | +JOIN sys.query_store_runtime_stats_interval qsrsi on qrs.runtime_stats_interval_id=qsrsi.runtime_stats_interval_id |
| 56 | +WHERE |
| 57 | + qsp.query_plan like N'%PK_Sales_Invoices%' |
| 58 | + AND qsp.query_plan not like '%query_store_runtime_stats%' /* Not a query store query */ |
| 59 | + AND qsp.query_plan not like '%dm_exec_sql_text%' /* Not a query searching the plan cache */ |
| 60 | +GROUP BY |
| 61 | + qsq.query_id, qsq.query_hash |
| 62 | +ORDER BY est_logical_reads DESC |
| 63 | +OPTION (RECOMPILE); |
| 64 | +GO |
| 65 | + |
| 66 | + |
| 67 | +--Search the execution plan cache for index hints |
| 68 | +--To find forced indexes in the plan cache, look for plans that contain ‘%ForcedIndex=”1″%’ |
| 69 | +SELECT |
| 70 | + querystats.plan_handle, |
| 71 | + querystats.query_hash, |
| 72 | + SUBSTRING(sqltext.text, (querystats.statement_start_offset / 2) + 1, |
| 73 | + (CASE querystats.statement_end_offset |
| 74 | + WHEN -1 THEN DATALENGTH(sqltext.text) |
| 75 | + ELSE querystats.statement_end_offset |
| 76 | + END - querystats.statement_start_offset) / 2 + 1) AS sqltext, |
| 77 | + querystats.execution_count, |
| 78 | + querystats.total_logical_reads, |
| 79 | + querystats.total_logical_writes, |
| 80 | + querystats.creation_time, |
| 81 | + querystats.last_execution_time, |
| 82 | + CAST(query_plan AS xml) as plan_xml |
| 83 | +FROM sys.dm_exec_query_stats as querystats |
| 84 | +CROSS APPLY sys.dm_exec_text_query_plan |
| 85 | + (querystats.plan_handle, querystats.statement_start_offset, querystats.statement_end_offset) |
| 86 | + as textplan |
| 87 | +CROSS APPLY sys.dm_exec_sql_text(querystats.sql_handle) AS sqltext |
| 88 | +WHERE |
| 89 | + textplan.query_plan like N'%ForcedIndex="1"%' |
| 90 | + and UPPER(sqltext.text) like N'%INDEX%' |
| 91 | +OPTION (RECOMPILE); |
| 92 | +GO |
| 93 | + |
| 94 | + |
| 95 | +--Find index hints in Query Store |
| 96 | +--If you’ve enabled the SQL Server 2016+ Query Store on your databases |
| 97 | +SELECT |
| 98 | + qsq.query_id, |
| 99 | + qsq.query_hash, |
| 100 | + (SELECT TOP 1 qsqt.query_sql_text FROM sys.query_store_query_text qsqt |
| 101 | + WHERE qsqt.query_text_id = MAX(qsq.query_text_id)) AS sqltext, |
| 102 | + SUM(qrs.count_executions) AS execution_count, |
| 103 | + SUM(qrs.count_executions) * AVG(qrs.avg_logical_io_reads) as est_logical_reads, |
| 104 | + SUM(qrs.count_executions) * AVG(qrs.avg_logical_io_writes) as est_writes, |
| 105 | + MIN(qrs.last_execution_time AT TIME ZONE 'Pacific Standard Time') as min_execution_time_PST, |
| 106 | + MAX(qrs.last_execution_time AT TIME ZONE 'Pacific Standard Time') as last_execution_time_PST, |
| 107 | + SUM(qsq.count_compiles) AS sum_compiles, |
| 108 | + TRY_CONVERT(XML, (SELECT TOP 1 qsp2.query_plan from sys.query_store_plan qsp2 |
| 109 | + WHERE qsp2.query_id=qsq.query_id |
| 110 | + ORDER BY qsp2.plan_id DESC)) AS query_plan |
| 111 | +FROM sys.query_store_query qsq |
| 112 | +JOIN sys.query_store_plan qsp on qsq.query_id=qsp.query_id |
| 113 | +CROSS APPLY (SELECT TRY_CONVERT(XML, qsp.query_plan) AS query_plan_xml) AS qpx |
| 114 | +JOIN sys.query_store_runtime_stats qrs on qsp.plan_id = qrs.plan_id |
| 115 | +JOIN sys.query_store_runtime_stats_interval qsrsi on qrs.runtime_stats_interval_id=qsrsi.runtime_stats_interval_id |
| 116 | +WHERE |
| 117 | + qsp.query_plan like N'%ForcedIndex="1"%' |
| 118 | +GROUP BY |
| 119 | + qsq.query_id, qsq.query_hash |
| 120 | +ORDER BY est_logical_reads DESC |
| 121 | +OPTION (RECOMPILE); |
| 122 | +GO |
0 commit comments