1.查找最重要的缺失的索引 

--=======================================================
--查找最重要的缺失的索引
SELECT TOP(20)
DB_NAME() AS DBName,
ROUND(s.avg_total_user_cost*s.avg_user_impact
*(S.user_seeks+S.user_scans),0) AS [TotalCost],
D.[statement] AS TableName,
D.equality_columns,
D.inequality_columns,
D.included_columns
INTO #TB1
FROM sys.dm_db_missing_index_groups G
INNER JOIN sys.dm_db_missing_index_group_stats S
ON S.group_handle=G.index_group_handle
INNER JOIN sys.dm_db_missing_index_details D
ON D.index_handle=G.index_handle EXEC sp_MSforeachdb '
USE [?]
INSERT INTO #TB1
SELECT TOP(20)
DB_NAME() AS DBName,
ROUND(s.avg_total_user_cost*s.avg_user_impact
*(S.user_seeks+S.user_scans),0) AS [TotalCost],
D.[statement] AS TableName,
D.equality_columns,
D.inequality_columns,
D.included_columns
FROM sys.dm_db_missing_index_groups G
INNER JOIN sys.dm_db_missing_index_group_stats S
ON S.group_handle=G.index_group_handle
INNER JOIN sys.dm_db_missing_index_details D
ON D.index_handle=G.index_handle
ORDER BY TotalCost DESC'

PS:

avg_total_user_cost:Average cost of the user queries that could be reduced by the index in the group.
avg_user_impact :Average percentage benefit that user queries could experience if this missing index group was implemented. The value means that the query cost would on average drop by this percentage if this missing index group was implemented.

user_seeks :Number of seeks caused by user queries that the recommended index in the group could have been used for.Not the number of times the index has been accessed.
user_scans :Number of scans caused by user queries that the recommended index in the group could have been used for.
参考:http://msdn.microsoft.com/zh-cn/library/ms345421.aspx

2. 查找无用索引

--==============================================================
--查找无用的索引,当索引使用次数和索引更新次数比在一个很小范围时,
--应该考虑该索引是否合理。 --在SQL SERVER DMVs IN ACTION书中判断无用索引的标注是
--S.[user_seeks]=0 AND S.[user_scans]=0 AND s.[user_lookups]=0
--该标准会将更新过于频繁但会偶尔使用的索引排出在外。 --因此还是建议使用索引使用次数和索引更新次数比,该比值可以自行调整
--============================================================== SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT
DB_NAME() AS DBName,
SCHEMA_NAME(O.[Schema_ID]) AS SchemaName,
OBJECT_NAME(O.[Object_ID]) AS TableName,
I.[name] AS IndexName,
S.[user_Updates],
(S.system_seeks+S.system_scans+s.system_lookups) AS [SystemUsage],
(S.[user_seeks]+S.[user_scans]+s.[user_lookups]) AS [UserUsage]
INTO #UnusedIndexes
FROM sys.dm_db_index_usage_stats S
INNER JOIN sys.indexes I
ON I.[object_id]=S.[object_id]
AND I.[index_id]=S.[index_id]
INNER JOIN sys.objects O
ON O.[object_id]=I.[object_id]
WHERE 1=2 EXEC sp_MSForeachDB '
use [?];
INSERT INTO #UnusedIndexes
SELECT TOP(20)
DB_NAME() AS DBName,
SCHEMA_NAME(O.[Schema_ID]) AS SchemaName,
OBJECT_NAME(O.[Object_ID]) AS TableName,
I.[name] AS IndexName,
S.[user_Updates],
(S.system_seeks+S.system_scans+s.system_lookups) AS [SystemUsage],
(S.[user_seeks]+S.[user_scans]+s.[user_lookups]) AS [UserUsage]
FROM sys.dm_db_index_usage_stats S
INNER JOIN sys.indexes I
ON I.[object_id]=S.[object_id]
AND I.[index_id]=S.[index_id]
INNER JOIN sys.objects O
ON O.[object_id]=I.[object_id]
WHERE S.[database_id]=DB_ID()
AND OBJECTPROPERTY(s.[object_id],''ISMSShipped'')=0
AND (S.[user_seeks]+S.[user_scans]+s.[user_lookups])*1.0/(S.[user_Updates]+1)<0.3
AND I.name IS NOT NULL
ORDER BY S.[user_Updates] DESC
' SELECT TOP(100)* FROM #UnusedIndexes
ORDER BY S.[user_Updates] DESC DROP TABLE #UnusedIndexes

参考:http://msdn.microsoft.com/zh-cn/library/ms188755.aspx

3. 查找碎片较多的索引

--=================================================================
--查看索引碎片最高的索引
--运行在LIMITIED模式来减少对系统的影响
--参考连接:http://technet.microsoft.com/zh-cn/library/ms188917.aspx --=================================================================
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SELECT DB_NAME() AS DBName,
SCHEMA_NAME(O.[Schema_ID]) AS SchemaName,
OBJECT_NAME(O.[Object_id]) AS ObjectName,
I.name AS IndexName,
ROUND(S.avg_fragmentation_in_percent,2) AS [FragmentationPercent]
INTO #TempFragmentation
FROM sys.dm_db_index_physical_stats(db_id(),null,null,null,null) AS S
INNER JOIN sys.indexes I
ON I.[Object_ID]=S.[Object_id]
AND I.[Index_id]=S.[Index_id]
INNER JOIN sys.objects O
ON O.[object_Id]=I.[object_id]
WHERE S.[database_id]=DB_ID()
AND I.[name] IS NOT NULL
AND OBJECTPROPERTY(O.[object_id],'ISMSShipped')=0
AND 1=2
ORDER BY [FragmentationPercent] DESC EXEC SP_MSFOREACHDB '
USE [?];
INSERT INTO #TempFragmentation
SELECT TOP(20)
DB_NAME() AS DBName,
SCHEMA_NAME(O.[Schema_ID]) AS SchemaName,
OBJECT_NAME(O.[Object_id]) AS ObjectName,
I.name AS IndexName,
ROUND(S.avg_fragmentation_in_percent,2) AS [FragmentationPercent] FROM sys.dm_db_index_physical_stats(db_id(),null,null,null,null) AS S
INNER JOIN sys.indexes I
ON I.[Object_ID]=S.[Object_id]
AND I.[Index_id]=S.[Index_id]
INNER JOIN sys.objects O
ON O.[object_Id]=I.[object_id]
WHERE S.[database_id]=DB_ID()
AND I.[name] IS NOT NULL
AND OBJECTPROPERTY(O.[object_id],''ISMSShipped'')=0
ORDER BY [FragmentationPercent] DESC
' SELECT TOP(20)*
FROM #TempFragmentation
ORDER BY [FragmentationPercent] DESC DROP TABLE #TempFragmentation

4. 查看索引的统计信息

--==========================================================
--查看统计的状态信息
--==========================================================
SELECT
SS.[name] AS SchemaName,
ST.[name] AS TableName,
STATS_DATE(S.ID,S.IndID) AS StatisticsLastUpdated,
S.rowcnt AS [RowCount],
s.rowmodctr AS [NumberOfChanges],
ROUND(s.rowmodctr*1.0/s.rowmodctr*100,2) AS [RowChangedPercent]
FROM sys.sysindexes S
INNER JOIN sys.tables ST
ON ST.[object_id]=S.[id]
INNER JOIN sys.schemas SS
ON ss.[Schema_id]=ST.[schema_id]
WHERE S.ID>100
AND S.indid>0
AND s.rowcnt>=500

CHARPTER 3--INDEX DMVs的更多相关文章

  1. 译:Missing index DMV的 bug可能会使你失去理智---慎重看待缺失索引DMV中的信息

    注: 本文译自https://www.sqlskills.com/blogs/paul/missing-index-dmvs-bug-that-could-cost-your-sanity/ 原文作者 ...

  2. 译:SQL Server的Missing index DMV的 bug可能会使你失去理智---慎重看待缺失索引DMV中的信息

    注: 本文译自https://www.sqlskills.com/blogs/paul/missing-index-dmvs-bug-that-could-cost-your-sanity/ 原文作者 ...

  3. The Accidental DBA

    The Accidental DBA (Day 1 of 30): Hardware Selection: CPU and Memory Considerations 本文大意:      全篇主要讲 ...

  4. 【译】The Accidental DBA:Troubleshooting Performance

    最近重新翻看The Accidental DBA,将Troubleshooting Performance部分稍作整理,方便以后查阅.此篇是Part 2Part 1:The Accidental DB ...

  5. 第七章——DMVs和DMFs(2)——用DMV和DMF监控索引性能

    原文:第七章--DMVs和DMFs(2)--用DMV和DMF监控索引性能 本文继续介绍使用DMO来监控,这次讲述的是监控索引性能.索引是提高查询性能的关键性手段.即使你的表上有合适的索引,你也要时时刻 ...

  6. MySQL 优化之 ICP (index condition pushdown:索引条件下推)

    ICP技术是在MySQL5.6中引入的一种索引优化技术.它能减少在使用 二级索引 过滤where条件时的回表次数 和 减少MySQL server层和引擎层的交互次数.在索引组织表中,使用二级索引进行 ...

  7. 在v-for中利用index来对第一项添加class(vue2.0)

    <li v-for="(el,index) in event" v-bind:class="{ 'm-swipe-active': !index}"> ...

  8. Ubuntu-server 下Apache2 配置.htaccess 隐藏thinkPHP项目index.php

    需要开启Apache2的rewrite模块 1.打开/etc/apache2/apache2.conf 将文件中的AllowOverride None改为AllowOverride All 2.修改m ...

  9. SQL Server-聚焦强制索引查询条件和Columnstore Index(九)

    前言 本节我们再来穿插讲讲索引知识,后续再讲数据类型中的日期类型,简短的内容,深入的理解,Always to review the basics. 强制索引查询条件 前面我们也讲了一点强制索引查询的知 ...

随机推荐

  1. C++11新特性介绍 01

    阅读目录 1. 概述 2. long long 类型 3. 列表初始化 4. nullptr 空指针 5. constexpr变量 6. constexpr函数 7. using类型别名 8. aut ...

  2. user_add示例

    #!/usr/bin/python3# -*- coding: utf-8 -*-# @Time    : 2018/5/28 16:51# @File    : use_test_add.py 数据 ...

  3. win10 svn commit无响应

    只是发现其中的一个原因,发现.cs代码文件图标变红了,默认是用Code Writer打开,和SVN可能是冲突了,解决方式是用Code Writer打开一下.cs文件就可以了,原因可能是不打开一次Cod ...

  4. 蚂蚁社招Java-第四轮电话面试【技术终面】

    作者:听着歌过面试链接:https://www.nowcoder.com/discuss/64708来源:牛客网 蚂蚁社招Java-第四轮电话面试[技术终面] 转载   (耗时22分钟,其实聊得东西挺 ...

  5. 20165233 Java第八、十五章学习总结

    20165233 2017-2018-2 <Java程序设计>第六周学习总结 教材学习内容总结 ch08 基础:String类 重点:StringTokenizer类.Scanner类:获 ...

  6. Spring Boot实践——基础和常用配置

    借鉴:https://blog.csdn.net/j903829182/article/details/74906948 一.Spring Boot 启动注解说明 @SpringBootApplica ...

  7. Visual Studio 使用Web Deploy发布项目

    工具:Web Deploy 3.6 点击下载 (强烈推荐使用独立的Web Deploy 安装包安装) 使用 Web Platform Installer 安装 Web Deploy(3.5,3.6都安 ...

  8. Openstack 组件简介

    1. Nova 计算服务: 负责承载和管理云计算系统 其中nova-compute service 通过调用Hypervisor APIs创建和终止虚拟机实例. 虚拟化技术: KVM和Xen 2. N ...

  9. Vertex color blending & UV tiling

    [Vertex color blending & UV tiling] 1.GemotryData控件用于代码顶点数据,如网格中的Vertex Color(下左图),UV Coord(下右图) ...

  10. Kafka学习整理五(Consumer配置)

    Property Default Description group.id   用来唯一标识consumer进程所在组的字符串,如果设置同样的group id,表示这些processes都是属于同一个 ...