Performance 03: missing indexes

Indexes SQL Server thinks would help, across all databases, since start-up, with a CREATE INDEX statement for each. Treat them as hints: check for overlap with existing indexes before creating any. Read-only, changes nothing. Tested on SQL Server 2017 and 2025.

-- 03_Missing_Indexes.sql - indexes SQL Server thinks would help, across all databases, since start-up. Read-only.
-- Treat as hints: check for overlap with existing indexes before creating any (and sp_BlitzIndex gives a fuller picture).
SET NOCOUNT ON;
SELECT TOP (30) DB_NAME(d.database_id) AS [database], d.statement AS [table],
       CONVERT(decimal(18,0), s.avg_total_user_cost * s.avg_user_impact * (s.user_seeks + s.user_scans)) AS benefit,
       s.user_seeks, s.user_scans, s.last_user_seek, d.equality_columns, d.inequality_columns, d.included_columns,
       N'CREATE INDEX IX_' + REPLACE(REPLACE(REPLACE(ISNULL(d.equality_columns, d.inequality_columns), N'[', N''), N']', N''), N', ', N'_')
       + N' ON ' + d.statement + N' (' + ISNULL(d.equality_columns, N'')
       + CASE WHEN d.equality_columns IS NOT NULL AND d.inequality_columns IS NOT NULL THEN N', ' ELSE N'' END + ISNULL(d.inequality_columns, N'') + N')'
       + ISNULL(N' INCLUDE (' + d.included_columns + N')', N'') + N';' AS create_statement
FROM sys.dm_db_missing_index_details AS d
JOIN sys.dm_db_missing_index_groups AS g ON g.index_handle = d.index_handle
JOIN sys.dm_db_missing_index_group_stats AS s ON s.group_handle = g.index_group_handle
ORDER BY benefit DESC;