Quantcast
Channel: Doyensys Allappsdba Blog..
Viewing all articles
Browse latest Browse all 1640

Article 2

$
0
0

SQL SERVER – Missing Index Script



Performance Tuning is quite interesting and Index plays a vital role in it. A proper index can improve the performance and a bad index can hamper the performance. In this post we will discuss about Missing Index.

Please note, if you should not create all the missing indexes this script suggest. This is just for guidance. You should not create more than 5-10 indexes per table


-- Missing Index Script

SELECTTOP25
dm_mid.database_id ASDatabaseID,
dm_migs.avg_user_impact*(dm_migs.user_seeks+dm_migs.user_scans) Avg_Estimated_Impact,
dm_migs.last_user_seek ASLast_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
WHENdm_mid.equality_columns ISNOTNULL
ANDdm_mid.inequality_columns ISNOTNULLTHEN'_'
ELSE''
END
+ REPLACE(REPLACE(REPLACE(ISNULL(dm_mid.inequality_columns,''),', ','_'),'[',''),']','')
+ ']'
+ ' ON '+ dm_mid.statement
+ ' ('+ ISNULL(dm_mid.equality_columns,'')
+ CASEWHENdm_mid.equality_columns ISNOTNULLANDdm_mid.inequality_columns
ISNOTNULLTHEN','ELSE
''END
+ ISNULL(dm_mid.inequality_columns, '')
+ ')'
+ ISNULL(' INCLUDE ('+ dm_mid.included_columns + ')', '') ASCreate_Statement
FROMsys.dm_db_missing_index_groups dm_mig
INNERJOINsys.dm_db_missing_index_group_stats dm_migs
ONdm_migs.group_handle = dm_mig.index_group_handle
INNERJOINsys.dm_db_missing_index_details dm_mid
ONdm_mig.index_handle = dm_mid.index_handle
WHEREdm_mid.database_ID = DB_ID()
ORDERBYAvg_Estimated_Impact DESC
GO

Viewing all articles
Browse latest Browse all 1640

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>