码迷,mamicode.com
首页 > 其他好文 > 详细

查询需要创建的索引(含创建语句)

时间:2019-09-29 16:54:25      阅读:111      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!