sqlserver查看索引使用情况以及建立丢失的索引

参考:https://www.cnblogs.com/firtree/p/3918844.html

--查看表的索引使用情况
SELECT TOP 1000
o.name AS 表名
, i.name AS 索引名
, i.index_id AS 索引id
, dm_ius.user_seeks AS 搜索次数
, dm_ius.user_scans AS 扫描次数
, dm_ius.user_lookups AS 查找次数
, dm_ius.user_updates AS 更新次数
, p.TableRows as 表行数
, 'DROP INDEX ' + QUOTENAME(i.name)
+ ' ON ' + QUOTENAME(s.name) + '.' + QUOTENAME(OBJECT_NAME(dm_ius.OBJECT_ID)) AS '删除语句'
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
and o.name='testtable'   --需要查找的表名
ORDER BY (dm_ius.user_seeks + dm_ius.user_scans + dm_ius.user_lookups) ASC



--查看数据库里表丢失的索引并生成建立索引的语句
SELECT t4.name,t1.[statement],t1.object_id, t2.user_seeks, t2.user_scans,
       t1.equality_columns, t1.inequality_columns,t1.included_columns,
    case 
       --when t1.equality_columns is null and charindex(',',t1.inequality_columns)=0 and t1.included_columns is null
       --    then   'create UNIQUE NONCLUSTERED INDEX IX_' + replace((replace((replace(t1.[statement],'[','_')),']','_')),'.','_') +'_'+ replace((replace((replace(isnull(t1.equality_columns,'1'),'[','_')),']','_')),'.','_') +'_' 
       --           +replace((replace((replace(isnull(t1.inequality_columns,'_2'),'[','_')),']','_')),'.','_') + ' ON '+ t1.[statement] + ' (' + t1.inequality_columns + ' ASC )'  
       when --t1.equality_columns is null and charindex(',',t1.inequality_columns)>0 and
       t1.included_columns is null
           then   'create  NONCLUSTERED INDEX IX_' + replace((replace((replace((replace(t1.[statement],'[','_')),']','_')),'.','_')),',','_') +'_'  
                  +replace(replace(replace(replace(replace(isnull(t1.equality_columns,'2'),' [',''),'[',''),'.',''),',',''),']','')
  +replace((replace((replace((replace(isnull(t1.inequality_columns,'2'),'[','')),']','')),'.','')),',','_') + ' ON '+ t1.[statement] + ' (' + 
    case  when t1.equality_columns is null then ' '
          when charindex(',',t1.equality_columns)=0 then t1.equality_columns +' ASC '
  when charindex(',',t1.equality_columns)>0 then replace(t1.equality_columns,',',' ASC,') + ' ASC ' 
 end
       +   
    case  when t1.equality_columns is not null and charindex(',',t1.inequality_columns)=0 then  ' ,'+t1.inequality_columns + ' ASC )'
when t1.equality_columns is null and charindex(',',t1.inequality_columns)=0 then  ' '+t1.inequality_columns + ' ASC )'
  when  t1.inequality_columns is null then ' )'
  when charindex(',',t1.inequality_columns) > 0 then ' ,'+ replace(t1.inequality_columns,',',' ASC,') + ' ASC )' 
  when  t1.equality_columns is null and charindex(',',t1.inequality_columns) > 0 then ' '+ replace(t1.inequality_columns,',',' ASC,') + ' ASC )'
     end
   when t1.included_columns is not null
        then   'create NONCLUSTERED INDEX IX_' + replace((replace((replace((replace(t1.[statement],'[','_')),']','_')),'.','_')),',','_') +'_'  
                  +replace(replace(replace(replace(replace(isnull(t1.equality_columns,'2'),' [',''),'[',''),'.',''),',',''),']','')
  +replace((replace((replace((replace(replace(isnull(t1.inequality_columns,'2'),' [',''),'[','')),']','')),'.','')),',','_') + ' ON '+ t1.[statement] + ' (' + 
    case  when t1.equality_columns is null then ' '
          when charindex(',',t1.equality_columns) = 0 then t1.equality_columns +' ASC '
  when charindex(',',t1.equality_columns) > 0 then replace(t1.equality_columns,',',' ASC,') + ' ASC ' 
 end
       +   
    case  when  t1.equality_columns is not null and charindex(',',t1.inequality_columns)=0 then ' ,'+t1.inequality_columns + ' ASC )'
when  t1.equality_columns is null and charindex(',',t1.inequality_columns)=0 then ' '+t1.inequality_columns + ' ASC )'
  when  t1.inequality_columns is null then ' )'
  when  t1.equality_columns is not null and charindex(',',t1.inequality_columns) > 0 then ' ,'+ replace(t1.inequality_columns,',',' ASC,') + ' ASC )' 
  when  t1.equality_columns is null and charindex(',',t1.inequality_columns) > 0 then ' '+ replace(t1.inequality_columns,',',' ASC,') + ' ASC )' 
     end
  + ' INCLUDE ( ' + t1.included_columns + ' )'
   
    end  as  '建立索引的语句'


      FROM sys.dm_db_missing_index_groups AS t3
      join sys.dm_db_missing_index_details AS t1
       on  t1.index_handle = t3.index_handle
          join sys.dm_db_missing_index_group_stats AS t2
            on t2.group_handle = t3.index_group_handle
              join sys.databases AS t4 
                on t1.database_id = t4.database_id
      WHERE t1.database_id = DB_ID() --AND object_id = OBJECT_ID('interface.商户设备表')
      order by t2.user_seeks desc 
      
      --t4.name,t1.object_id
posted @ 2022-05-22 19:23  朕在coding  阅读(460)  评论(0编辑  收藏  举报