标签:processes cos tables case process base 执行计划 dex 表示
;WITH T AS (
SELECT
DB_NAME(a.database_id) DBNAME,
SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11) TableName,
avg_user_impact,
avg_total_user_cost,
‘use [‘+DB_NAME(a.database_id)+‘];if not exists(select 1 from sysindexes where name=‘‘ix_‘ + SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(equality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(inequality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) + ‘‘‘)CREATE INDEX [ix_‘ + SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(equality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(inequality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) + ‘] ON ‘ + SUBSTRING([statement],LEN(DB_NAME(a.database_id))+4,LEN([statement])-LEN(DB_NAME(a.database_id))-3) +
‘ (‘ + ISNULL(equality_columns, ‘ ‘) + CASE WHEN equality_columns IS NOT NULL AND inequality_columns IS NOT NULL THEN ‘,‘ ELSE ‘‘ end + ISNULL(inequality_columns, ‘ ‘) + ‘)‘ +
ISNULL(‘ INCLUDE (‘ + included_columns + ‘)‘, ‘‘) +‘;‘
AS MissIndex
,‘use [‘+DB_NAME(a.database_id)+‘];if exists(select 1 from sysindexes where name=‘‘ix_‘ + SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(equality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(inequality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) + ‘‘‘)drop index [ix_‘ + SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(equality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(inequality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) + ‘] on [‘+SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11)+‘];‘ DropIndex
,‘ix_‘ + SUBSTRING([statement],LEN(DB_NAME(a.database_id))+11,LEN([statement])-LEN(DB_NAME(a.database_id))-11) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(equality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) +
ISNULL(REPLACE(REPLACE(REPLACE(REPLACE(inequality_columns,‘[‘,‘_‘),‘]‘,‘‘),‘,‘,‘‘),‘ ‘,‘‘), ‘‘) MissIndexName
FROM sys.dm_db_missing_index_details a INNER JOIN
sys.dm_db_missing_index_groups b ON a.index_handle = b.index_handle
INNER JOIN sys.dm_db_missing_index_group_stats c ON
b.index_group_handle = c.group_handle
WHERE avg_user_impact > = 10 --在执行计划中的占比
)
SELECT
t1.DBNAME [数据库名称]
,t1.TableName [表名]
,t1.MissIndex [缺失的索引的创建]
,t1.avg_user_impact AS [实现此缺失索引组后,用户查询可能获得的平均百分比收益。 该值表示如果实现此缺失索引组,则查询成本将按此百分比平均下降。]
,t1.avg_total_user_cost AS [实现此缺失索引组后,减少的用户查询的平均成本]
,t1.DropIndex [缺失索引的删除]
,t1.MissIndexName [缺失索引名称]
FROM T t1
WHERE 1=1
AND NOT EXISTS(SELECT 1 FROM T t2 WHERE t1.MissIndexName!=t2.MissIndexName and SUBSTRING(t2.MissIndexName,1,LEN(t1.MissIndexName))=t1.MissIndexName)
AND t1.MissIndexName NOT IN (SELECT name from sys.indexes WHERE object_id IN (SELECT object_id FROM sys.tables WHERE type=‘U‘) AND type=2)
and t1.DBNAME in (Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = @@spid))
ORDER BY t1.DBNAME ASC,t1.avg_user_impact DESC,t1.TableName ASC
go
标签:processes cos tables case process base 执行计划 dex 表示
原文地址:https://www.cnblogs.com/xwma/p/11608584.html