SELECT DB_NAME(database_id) AS DB_NAME ,
OBJECT_NAME(object_id, database_id) AS Table_Name ,
MisDetail.equality_columns [相等欄位] ,
MisDetail.inequality_columns [不等欄位] ,
MisDetail.included_columns [覆蓋欄位] ,
( user_seeks + user_scans ) * avg_total_user_cost * ( avg_user_impact
* 0.01 ) [建索引可降低的成本] ,
MisStatus.unique_compiles [可用此索引的已編譯執行計畫之數量] ,
MisStatus.user_seeks [使用者搜尋次數] ,
MisStatus.user_scans [使用者掃描次數] ,
MisStatus.avg_total_user_cost [平均使用者可以降低的成本] ,
MisStatus.avg_user_impact [使用後成本佔原成本的百分率] ,
'use ' + DB_NAME(database_id) + ' create index idx'
+ OBJECT_NAME(object_id, database_id) + '_missing_'
+ CONVERT(VARCHAR(10), MisDetail.index_handle) + ' on ['
+ OBJECT_NAME(object_id, database_id) + ']('
+ ISNULL(MisDetail.equality_columns, '')
+ CASE WHEN MisDetail.equality_columns IS NOT NULL
AND MisDetail.inequality_columns IS NOT NULL THEN ','
ELSE ''
END + ISNULL(MisDetail.inequality_columns, '') + ')
' + ISNULL('include (' + included_columns + ')
', '') AS SQL_Statment
FROM sys.dm_db_missing_index_group_stats MisStatus WITH ( NOLOCK )
JOIN sys.dm_db_missing_index_groups MisGroup WITH ( NOLOCK ) ON MisStatus.group_handle = MisGroup.index_group_handle
JOIN sys.dm_db_missing_index_details MisDetail WITH ( NOLOCK ) ON MisGroup.index_handle = MisDetail.index_handle
ORDER BY 1 ,
2 ASC