Skip to content

Instantly share code, notes, and snippets.

@kosperera
Created March 27, 2018 12:09
Show Gist options
  • Save kosperera/acfd80c3466a7df5bb7cc22672664741 to your computer and use it in GitHub Desktop.
Save kosperera/acfd80c3466a7df5bb7cc22672664741 to your computer and use it in GitHub Desktop.
Find out missing indexes for queries running on the database.
SELECT TOP 20
total_worker_time/execution_count AS Avg_CPU_Time
,Execution_count
,total_elapsed_time/execution_count as AVG_Run_Time
,total_elapsed_time
,(SELECT
SUBSTRING(text,statement_start_offset/2+1,statement_end_offset
) FROM sys.dm_exec_sql_text(sql_handle)
) AS Query_Text
FROM sys.dm_exec_query_stats
ORDER BY Avg_CPU_Time DESC
SET TRANSACTION ISOLATION LEVEL
READ UNCOMMITTED
SELECT TOP 20
ROUND(s.avg_total_user_cost *
s.avg_user_impact
* (s.user_seeks + s.user_scans),0)
AS [Total Cost]
,d.[statement] AS [Table Name]
,equality_columns
,inequality_columns
,included_columns
FROM sys.dm_db_missing_index_groups g
INNER JOIN sys.dm_db_missing_index_group_stats s
ON s.group_handle = g.index_group_handle
INNER JOIN sys.dm_db_missing_index_details d
ON d.index_handle = g.index_handle
ORDER BY [Total Cost] DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment