sqlserver查看索引使用情况以及建立丢失的索引
--查看表的索引使用情况
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
sqlserver查看索引使用情况以及建立丢失的索引的更多相关文章
- SQLserver查看索引使用情况
查索引使用情况: https://www.cnblogs.com/sunliyuan/p/6559354.html select db_name(database_id) as N'TOPK_T ...
- mysql如何查看索引使用情况以及优化 - guols0612
mysql中支持hash和btree索引.innodb和myisam只支持btree索引,而memory和heap存储引擎可以支持hash和btree索引 我们可以通过下面语句查询当前索引使用情况: ...
- MySQL 5.7以后怎么查看索引使用情况?
MySQL 5.7以后怎么查看索引使用情况? 0.在sys库中查看没用的索引 root@localhost [sys]>select * from schema_unused_indexes; ...
- SQL 数据优化之不建立索引的情况
索引可以提高数据的检索效率,也可以降低数据库的IO成本,并且索引还可以降低数据库的排序成本.排序分组操作主要消耗的就是CPU资源和内存,所以能够在排序分组操作中好好的利用索引将会极大地降低CPU资源的 ...
- SqlServer之like、charindex、patindex 在有无索引的情况下分析
1.环境介绍 测试环境 SQL2005 测试数据 200W条 2.环境准备 2.1建表 CREATE TABLE [dbo].[Depratments]( [Dep_id] [int] ...
- mysql 查看索引使用情况
show status like 'Handler_read%'; Handler_read_key 代表着一个行被索引值读取的次数,值很低表明索引不经常用到,增加索引对性能改善不高. Handle ...
- MSSQL优化之——查看语句执行情况
MSSQL优化之——查看语句执行情况 在写SQL语句时,必须知道语句的执行情况才能对此作出优化.了解SQL语句的执行情况是每个写程序的人必不可少缺的能力.下面是对查询语句执行情况的方法介绍. 一.设置 ...
- SQL Server 索引维护(1)——如何获取索引使用情况
前言: 在前面一文中,已经提到了三类常见的索引问题,那么问题来了,当系统出现这些问题时,该如何应对? 简单而言,需要分析现有系统的行为,然后针对性地对索引进行处理: 对于索引不足的情况:检查缺少索引的 ...
- SQLServer查看和解决死锁的方法
http://luohonghong.blog.163.com/blog/static/78312058201142411533316/ SQLServer查看和解决死锁的方法 2011-05-24 ...
随机推荐
- VBA取得EXCEL表格中的行数和列数
VBA取得EXCEL表格中的行数和列数 初学EXCEL宏的童鞋,总是很想知道表格中含有数据的行数和列数,尤其是行数和列数不确定的情况下.这样可以避免很多的错误,并且可以提高效率.但每次用到的时候到网上 ...
- (多对象)Json转换成List
写的不好,请大家见谅. 1.Json 格式{"packages":[{“type”:”aaa”}],"zone_packages":[{"ticket ...
- OUTPUT 在insnert delete update 的神奇功效
Inserted deleted 个人理解 应该是两个 临时表 分别存储 变动后的数据集 和 变动前的数据集 使用例子: 1.对于INSERT,可以引用inserted表以查询新行的属性 ...
- eclipse/myeclipse 变量名自动补全问题
原理是:在输入变量名后,去掉按下空格或=后,代码上屏 以前只知道alt+/调出assist,后来发现可以所有字母都激活content assist(8.1里有写).用起来果然很爽,但是eclipse ...
- plist解析, 简易实现.
源码 class Xml { public: typedef std::pair<std::wstring, std::wstring> NodeT; static std::vector ...
- VBox UUID already exists 问题处理
问题说明: 在win7系统下使用vbox时,有时候需要多台相同操作系统和开发环境的虚拟电脑时,如果重复安装,会比较麻烦.那么可以在vbox中创建一个新的虚拟电脑B,但不创建虚拟硬盘,然后拷贝虚拟电脑A ...
- jQuery实现checkbox全选反选及删除等操作
1.list.html 说明:用checkbox数组Check[]存放每一行的ID值 <div id="con"> <table width="100% ...
- Gson解析json繁杂数据
碰到json数据.里面格式众多.list+string[]+等等.具体json参数如下: eg:以下为接口参数: "responseData":{ "brandCode& ...
- extjs combobox 设置下拉时显示滚动条 设置显示条数
extjs在点击下拉时如果没有限制它的高度,那么它的默认最大高度是300,显示的时候就会显示300的高度,知道选项内容超过这个高度时才会自动显示滚动条,往往在有些时候我们希望让combobox显示一个 ...
- Java高精度学习第一弹
为了快速解决高精度问题,总算是要来接触java了,算上这学期要开java的课了,好好学习吧! 拿来练手的是hdu的1002,高精度加法. import java.util.*; import java ...