SQLServer 维护脚本分享(10)索引
--可添加索引的字段
SELECT top 100 migs.user_seeks,migs.avg_total_user_cost,migs.avg_user_impact,migs.last_user_seek
,mid.statement ,mid.equality_columns ,mid.included_columns,mid.inequality_columns,migs.unique_compiles
FROM sys.dm_db_missing_index_group_stats migs (nolock)
inner join sys.dm_db_missing_index_groups mig (nolock) on migs.group_handle=mig.index_group_handle
inner join sys.dm_db_missing_index_details mid (nolock) on mig.index_handle=mid.index_handle
inner join sys.objects so (nolock) on mid.object_id=so.object_id
inner join sys.databases sd (nolock) on mid.database_id=sd.database_id
where so.is_ms_shipped=0
and sd.name = DB_NAME()
order by migs.avg_total_user_cost desc --查看无用索引
SELECT ind.index_id ,
obj.name AS TableName ,
ind.name AS IndexName ,
ind.type_desc ,
indUsage.user_seeks ,
indUsage.user_scans ,
indUsage.user_lookups ,
indUsage.user_updates ,
indUsage.last_system_seek ,
indUsage.last_user_scan ,
'drop index [' + ind.name + '] ON [' + obj.name + ']' AS DropIndexCommand
FROM sys.indexes AS ind (nolock)
INNER JOIN sys.objects AS obj(nolock) ON ind.object_id = obj.object_id
LEFT JOIN sys.dm_db_index_usage_stats indUsage(nolock)
ON ind.object_id = indUsage.object_id AND ind.index_id = indUsage.index_id
WHERE ind.type_desc <> 'HEAP'
AND obj.type <> 'S'
AND OBJECTPROPERTY(obj.object_id, 'isusertable') = 1
AND ( ISNULL(indUsage.user_seeks, 0) = 0
AND ISNULL(indUsage.user_scans, 0) = 0
AND ISNULL(indUsage.user_lookups, 0) = 0
)
ORDER BY obj.name ,ind.name SELECT
o.name
, indexname=i.name
, i.index_id
, reads=user_seeks + user_scans + user_lookups
, writes = user_updates
, rows = (SELECT SUM(p.rows) FROM sys.partitions p WHERE p.index_id = s.index_id AND s.object_id = p.object_id)
, CASE
WHEN s.user_updates < 1 THEN 100
ELSE 1.00 * (s.user_seeks + s.user_scans + s.user_lookups) / s.user_updates
END AS reads_per_write
, 'DROP INDEX ' + QUOTENAME(i.name)
+ ' ON ' + QUOTENAME(c.name) + '.' + QUOTENAME(OBJECT_NAME(s.object_id)) as 'drop statement'
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON i.index_id = s.index_id AND s.object_id = i.object_id
INNER JOIN sys.objects o on s.object_id = o.object_id
INNER JOIN sys.schemas c on o.schema_id = c.schema_id
WHERE OBJECTPROPERTY(s.object_id,'IsUserTable') = 1
AND s.database_id = DB_ID()
AND i.type_desc = 'nonclustered'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
AND (SELECT SUM(p.rows) FROM sys.partitions p WHERE p.index_id = s.index_id AND s.object_id = p.object_id) > 10000
ORDER BY reads -- 返回最经常被修改的20个索引
SELECT top 20 *
FROM sys.dm_db_index_operational_stats(NULL, NULL, NULL, NULL)
order by leaf_insert_count+leaf_delete_count+leaf_update_count desc
GO -- 返回当前数据库所有碎片率大于25%的索引
-- 运行本语句会扫描很多数据页面
-- 避免在系统负载比较高时运行
SELECT
DB_NAME() as DB_NAME
,OBJECT_NAME(s.object_id) as OBJECT_NAME
,i.name index_name
,i.type_desc
,s.index_type_desc
,s.alloc_unit_type_desc
,s.page_count
,s.fragment_count
,s.avg_fragment_size_in_pages
,s.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (db_id(), NULL, NULL, NULL, NULL) s,sys.indexes i
WHERE s.object_id=i.object_id and s.index_id=i.index_id
AND avg_fragmentation_in_percent>25
order by avg_fragmentation_in_percent desc --索引页类型及使用情况
SELECT
OBJECT_NAME(OBJECT_ID) AS 表名
,OBJECT_ID AS 对象ID
,SUM(reserved_page_count) AS 已分配页数
,SUM(used_page_count) AS 使用页数
,SUM(CASE WHEN index_id < 2
THEN in_row_data_page_count+lob_used_page_count+row_overflow_used_page_count
ELSE lob_used_page_count+row_overflow_used_page_count END) AS 数据页数
,SUM(CASE WHEN index_id < 2 THEN row_count ELSE 0 END) AS 行数
FROM sys.dm_db_partition_stats
WHERE OBJECT_ID IN(SELECT OBJECT_ID FROM sys.objects where type = 'U')
GROUP BY OBJECT_ID SELECT object_name(object_id) AS 表名
,object_id AS 对象ID
,partition_number,filegroup_id,type_desc
,CASE WHEN index_id=0 THEN '堆索引(无聚集索引)'
WHEN index_id=1 THEN '聚集索引'
WHEN index_id BETWEEN 2 AND 250 THEN '非聚集索引'
ELSE 'text/image' END AS 存储方式
,total_pages AS 已分配页数
,used_pages AS 使用页数
,data_pages AS 数据页数
,rows AS 行数
,first_page,root_page,first_iam_page
FROM sys.partitions p
inner join sys.system_internals_allocation_units s on p.hobt_id=s.container_id
WHERE OBJECT_ID IN(SELECT OBJECT_ID FROM sys.objects where type = 'U')
-- AND index_id in (0,1) --各表索引页的记录情况(指定表,否则太久)
SELECT o.name,
ips.partition_number,
ips.index_type_desc,
ips.index_level,
ips.page_count,
ips.record_count,
ips.compressed_page_count
FROM sys.dm_db_index_physical_stats ( DB_ID(), NULL, NULL, NULL, 'DETAILED') ips
INNER JOIN sys.objects o on o.object_id = ips.object_id
WHERE o.name ='table'
ORDER BY o.name,ips.partition_number DESC;
/*
sys.dm_db_index_physical_stats (
{ database_id | NULL | 0 | DEFAULT }
, { object_id | NULL | 0 | DEFAULT }
, { index_id | NULL | 0 | -1 | DEFAULT }
, { partition_number | NULL | 0 | DEFAULT }
, { mode | NULL | DEFAULT }
)
*/ --各表索引的页页操作情况
select DB_NAME(database_id) DBName
,object_name(s.object_id) tabletName
,i.name indexName
,partition_number
,leaf_insert_count
,leaf_delete_count
,leaf_update_count
,range_scan_count
,singleton_lookup_count
,row_lock_count
,row_lock_wait_count
,page_lock_count
,page_lock_wait_count
from sys.dm_db_index_operational_stats (db_id(), NULL, NULL, NULL) s
inner join sys.indexes i on i.object_id = s.object_id and i.index_id = s.index_id
where objectproperty(s.object_id,'IsUserTable') = 1
order by tabletName,indexName,partition_number --(两次执行结果间)表的访问次数统计
select object_name(i.object_id) tabletName,isnull(sum(range_scan_count+singleton_lookup_count),0) as [read]
from sys.dm_db_index_operational_stats (db_id(), NULL, NULL, NULL) s
right join sys.indexes i on i.object_id = s.object_id and i.index_id = s.index_id
where objectproperty(i.object_id,'IsUserTable') = 1
and object_name(i.object_id) not like 'conflict%'
and exists(SELECT 1 FROM sys.tables t(nolock) WHERE i.object_id=t.object_id and is_ms_shipped=0)
group by object_name(i.object_id)
order by tabletName
SQLServer 维护脚本分享(10)索引的更多相关文章
- SQLServer 维护脚本分享(05)内存(Memory)
--查看设置的最大最小每次 exec sp_configure 'max server memory (MB)' exec sp_configure 'min server memory (MB)' ...
- SQLServer 维护脚本分享(06)CPU
--CPU相关视图 SELECT * FROM sys.dm_os_sys_info SELECT * FROM sys.dm_exec_sessions SELECT * FROM sys.sysp ...
- SQLServer 维护脚本分享(07)IO
sp_helpfile --当前数据库文件分配情况 sp_spaceused --当前db空间大小(有时不准) sp_spaceused 'dbo.user' --指定表的空间大小(有时不准) sp_ ...
- SQLServer 维护脚本分享(09)相关文件读取
/********************[读取跟踪文件(trc)]********************/ --查看事件类型描述 SELECT tc.name,te.trace_event_id, ...
- SQLServer 维护脚本分享(11)部分DBCC及系统存储过程
--DBCC命令与用法 DBCC HELP('?') DBCC HELP('useroptions') DBCC USEROPTIONS WITH NO_INFOMSGS --当前DB的区及文件 DB ...
- SQLServer 维护脚本分享(04)服务器角色和数据库角色相关操作
/*------------------------------------------------------------------------------------ [服务器级别-服务器角色] ...
- SQLServer 维护脚本分享(08)临时数据库(tempdb)
dbcc sqlperf(logspace) --各数据库日志大小及使用百分比 dbcc loginfo --查看当前数据库的虚拟日志文件 --临时表'Tempdb'最近使用情况 SELECT t1. ...
- sqlserver 生成脚本执行创建索引
create or alter proc SP_CreateIndex as begin if exists(select * from sys.objects where name='execsql ...
- SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享
SQL Server定时自动抓取耗时SQL并归档数据发邮件脚本分享 第一步建库和建表 USE [master] GO CREATE DATABASE [MonitorElapsedHighSQL] G ...
随机推荐
- 如何为自己的windows 8系统的电脑更换锁屏壁纸
现在的人都喜欢个性,今天教大家如何设置自己想要的锁屏壁纸 工具/原料 Windows 8系统的笔记本电脑 方法/步骤 将鼠标移到电脑的右下方,点击设置按钮进入设置页面 找到更改电脑设置并点击进 ...
- ACM/ICPC 之 四道MST-Prim解法(POJ1258-POJ1751-POJ2349-POJ3026)
四道MST,适合Prim解法,也可以作为MST练习题. 题意包括在代码中. POJ1258-Agri Net 水题 //Prim-没什么好说的 //接受一个邻接矩阵,求MST //Time:0Ms M ...
- Effective C++ -----条款21:必须返回对象时,别妄想返回其reference
绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...
- HDU 4873 ZCC Loves Intersection(JAVA、大数、推公式)
在一个D维空间,只有整点,点的每个维度的值是0~n-1 .现每秒生成D条线段,第i条线段与第i维度的轴平行.问D条线段的相交期望. 生成线段[a1,a2]的方法(假设该线段为第i条,即与第i维度的轴平 ...
- Log4j的使用
一,Log4j简介 日志的三个目的: 1. 监视代码中变量的变化情况,周期性的记录到文件中供其他应用进行统计分析工作 2.跟踪代码运行时轨迹,作为日后审计的依 ...
- [Android] 查看Android中的AlarmManager事件
reference to : https://segmentfault.com/a/1190000000404684 有时候我们需要设置一个alarmmanager事件 但是如果这个事件的时间是凌晨三 ...
- cmd for备忘
=============参数 /d (参数只能显示当前目录下的目录名字)=================== rem 显示d:\work\目录下与*.txt相匹配的文件(如果报错就%i-> ...
- solrconfig.xml和schema.xml说明
1. solrconfig.xml solrconfig.xml配置文件主要定义了SOLR的一些处理规则,包括索引数据的存放位置,更新,删除,查询的一些规则配置. 1.1. datadir节点 ...
- Mysql之复制选项与监控
1.Slave 筛选选项: --replicate-do-db ,同步复制哪些库 --replicate-ignore-db,忽略哪些库 --replicate-do-table=db_name.t ...
- 查询Oracle中字段名带"."的数据
SDE中的TT_L线层会有SHAPE.LEN这样的字段,使用: SQL>select shape.len from tt_l; 或 SQL>select t.shape.len from ...