Scripts for Identifying Missing Indexes, Unused Indexes and Heaps

Recently, I was surfing web for some SQL Server resources and explored some gems for identified missing indexes, unused indexes and heaps. Though, sp_BlitzIndex provides comprehensive information about index optimization. However, these popular scripts can be alternatives where you cannot deploy First Responder Kits. Moreover, you can easily customize them to fit your requirements. Let's dive in!

Figure: Missing Indexes, Unused Indexes and Heaps

Missing Indexes

Indexes are crucial to improve SELECT query performances. SQL Server suggests about missing indexes. With the correct indexing strategy, performances can be improved. However, you must be diligent before implementing SQL Server's recommendation. Because, SQL Server loves to add indexes if it sees a query without index.

One of my favorite SQL Server guru, Pinal Dave wrote below scripts to identify the missing indexes. This could be a good starting point. You should consider Avg_Estimated_Impact before creating an index. You will see the index creation script in the last column.

-- Missing Index Script
-- Original Author: Pinal Dave 
SELECT TOP 25
dm_mid.database_id AS DatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek AS Last_User_Seek,
OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) AS [TableName],
'CREATE INDEX [IX_' + OBJECT_NAME(dm_mid.OBJECT_ID,dm_mid.database_id) + '_'
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.equality_columns,''),', ','_'),'[',''),']','') 
+ CASE
WHEN dm_mid.equality_columns IS NOT NULL 
AND dm_mid.inequality_columns IS NOT NULL THEN '_'
ELSE ''
END
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')
+ ']'
+ ' ON ' + dm_mid.statement
+ ' (' + ISNULL (dm_mid.equality_columns,'')
+ CASE WHEN dm_mid.equality_columns IS NOT NULL AND dm_mid.inequality_columns 
IS NOT NULL THEN ',' ELSE
'' END
+ ISNULL (dm_mid.inequality_columns, '')
+ ')'
+ ISNULL (' INCLUDE (' + dm_mid.included_columns + ')', '') AS Create_Statement
FROM sys.dm_db_missing_index_groups dm_mig
INNER JOIN sys.dm_db_missing_index_group_stats dm_migs
ON dm_migs.group_handle = dm_mig.index_group_handle
INNER JOIN sys.dm_db_missing_index_details dm_mid
ON dm_mig.index_handle = dm_mid.index_handle
WHERE dm_mid.database_ID = DB_ID()
ORDER BY Avg_Estimated_Impact DESC
GO

Unused Indexes

SQL Server is putting labor to maintain the indexes however, none is using it! So, this is total waste of energy and resources. You should consider it to remove. As it will improve your INSERT/UPDATE/DELETE performances significantly.

Again, Pinal Dave created below cool script using sys.dm_db_index_usage_stats DMV to find out unused indexes.

-- Unused Index Script
-- Original Author: Pinal Dave 
SELECT TOP 25
o.name AS ObjectName
, i.name AS IndexName
, i.index_id AS IndexID
, dm_ius.user_seeks AS UserSeek
, dm_ius.user_scans AS UserScans
, dm_ius.user_lookups AS UserLookups
, dm_ius.user_updates AS UserUpdates
, p.TableRows
, 'DROP INDEX ' + QUOTENAME(i.name)
+ ' ON ' + QUOTENAME(s.name) + '.' 
+ QUOTENAME(OBJECT_NAME(dm_ius.OBJECT_ID)) AS 'drop statement'
FROM sys.dm_db_index_usage_stats dm_ius
INNER JOIN sys.indexes i ON i.index_id = dm_ius.index_id 
AND dm_ius.OBJECT_ID = i.OBJECT_ID
INNER JOIN sys.objects o ON dm_ius.OBJECT_ID = o.OBJECT_ID
INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
INNER JOIN (SELECT SUM(p.rows) TableRows, p.index_id, p.OBJECT_ID
FROM sys.partitions p GROUP BY p.index_id, p.OBJECT_ID) p
ON p.index_id = dm_ius.index_id AND dm_ius.OBJECT_ID = p.OBJECT_ID
WHERE OBJECTPROPERTY(dm_ius.OBJECT_ID,'IsUserTable') = 1
AND dm_ius.database_id = DB_ID()
AND i.type_desc = 'nonclustered'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
ORDER BY (dm_ius.user_seeks + dm_ius.user_scans + dm_ius.user_lookups) ASC
GO

Heaps

Heaps are good if you want to improve your INSERT performances. However, it becomes messy for SELECT/UPDATE/DELETE operations. In general, no heaps = best practice. If you want to find out the heaps list of your database. Use below query.

SELECT OBJECT_NAME(object_id) as TableName, *
FROM sys.indexes WHERE index_id = 0

Final Words

Indexing can make or break query performance. Choosing the right index can drastically speed up queries, while the wrong one can drag things down. So, carefully analyze and examine your indexes and then make the decision.

Going Further

If SQL Server is your thing and you enjoy learning real-world tips, tricks, and performance hacks—you are going to love my training sessions too! 

Need results fast? I am also available for 1-on-1 consultancy to help you troubleshoot and fix your database performance issues.

Let’s make your SQL Server to take your business Challenge!

For any queries, mail to mamehedi.hasan[at]gmail.com.

 

 

Add comment