Created
October 14, 2016 05:46
-
-
Save PartTimeLegend/661b78460bfab3ec3beb2007b25ff6dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| print '-- query and plan hash capture --' | |
| print '-- query and plan hash capture --' | |
| print '-- top 10 CPU by query_hash --' | |
| select getdate() as runtime, * --into tbl_QueryHashByCPU | |
| from | |
| ( | |
| SELECT TOP 10 query_hash, COUNT (distinct query_plan_hash) as 'distinctquery_plan_hash count', | |
| sum(execution_count) as 'execution_count', | |
| sum(total_worker_time) as 'total_worker_time', | |
| SUM(total_elapsed_time) as 'total_elapsed_time', | |
| SUM (total_logical_reads) as 'total_logical_reads', | |
| max(REPLACE (REPLACE (SUBSTRING (st.[text], qs.statement_start_offset/2 + 1, | |
| CASE WHEN qs.statement_end_offset = -1 THEN LEN (CONVERT(nvarchar(max), st.[text])) | |
| ELSE qs.statement_end_offset/2 - qs.statement_start_offset/2 + 1 | |
| END), CHAR(13), ' '), CHAR(10), ' ')) AS sample_statement_text | |
| FROM sys.dm_exec_query_stats AS qs | |
| CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) AS st | |
| group by query_hash | |
| ORDER BY sum(total_worker_time) DESC | |
| ) t | |
| go | |
| --Find out which one is on the TOP and then run the below query to find the missing indexes: | |
| DECLARE @runtime datetime | |
| DECLARE @cpu_time_start bigint, @cpu_time bigint, @elapsed_time_start bigint, @rowcount bigint | |
| DECLARE @queryduration int, @qrydurationwarnthreshold int | |
| DECLARE @querystarttime datetime | |
| SET @runtime = GETDATE() | |
| SET @qrydurationwarnthreshold = 5000 | |
| PRINT '' | |
| PRINT '===============================================================================================' | |
| PRINT 'Missing Indexes: ' | |
| PRINT 'The "improvement_measure" column is an indicator of the (estimated) improvement that might ' | |
| PRINT 'be seen if the index was created. This is a unitless number, and has meaning only relative ' | |
| PRINT 'the same number for other indexes. The measure is a combination of the avg_total_user_cost, ' | |
| PRINT 'avg_user_impact, user_seeks, and user_scans columns in sys.dm_db_missing_index_group_stats.' | |
| PRINT '' | |
| PRINT '-- Missing Indexes --' | |
| SELECT CONVERT (varchar, @runtime, 126) AS runtime, | |
| mig.index_group_handle, mid.index_handle, | |
| CONVERT (decimal (28,1), migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans)) AS improvement_measure, | |
| 'CREATE INDEX missing_index_' + CONVERT (varchar, mig.index_group_handle) + '_' + CONVERT (varchar, mid.index_handle) | |
| + ' ON ' + mid.statement | |
| + ' (' + ISNULL (mid.equality_columns,'') | |
| + CASE WHEN mid.equality_columns IS NOT NULL AND mid.inequality_columns IS NOT NULL THEN ',' ELSE '' END + ISNULL (mid.inequality_columns, '') | |
| + ')' | |
| + ISNULL (' INCLUDE (' + mid.included_columns + ')', '') AS create_index_statement, | |
| migs.*, mid.database_id, mid.[object_id] | |
| FROM sys.dm_db_missing_index_groups mig | |
| INNER JOIN sys.dm_db_missing_index_group_stats migs ON migs.group_handle = mig.index_group_handle | |
| INNER JOIN sys.dm_db_missing_index_details mid ON mig.index_handle = mid.index_handle | |
| WHERE CONVERT (decimal (28,1), migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans)) > 10 | |
| ORDER BY migs.avg_total_user_cost * migs.avg_user_impact * (migs.user_seeks + migs.user_scans) DESC | |
| PRINT '' | |
| GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment