Sql Server 索引使用情况及优化的相关Sql语句分享
程序员文章站
2023-12-02 21:36:46
复制代码 代码如下: --begin index(索引) 分析优化的相关 sql -- 返回当前数据库所有碎片率大于25%的索引 -- 运行本语句会扫描很多数据页面 --...
复制代码 代码如下:
--begin index(索引) 分析优化的相关 sql
-- 返回当前数据库所有碎片率大于25%的索引
-- 运行本语句会扫描很多数据页面
-- 避免在系统负载比较高时运行
-- 避免在系统负载比较高时运行
declare @dbid int
select @dbid = db_id()
select o.name as tablename,s.* from sys.dm_db_index_physical_stats (@dbid, null, null, null, null) s,sys.objects o
where avg_fragmentation_in_percent>25 and o.object_id =s.object_id
order by avg_fragmentation_in_percent desc
go
-- 当前数据库可能缺少的索引
-- 非常好用的 sql 语句
select d.*
, s.avg_total_user_cost
, s.avg_user_impact
, s.last_user_seek
,s.unique_compiles
from sys.dm_db_missing_index_group_stats s
,sys.dm_db_missing_index_groups g
,sys.dm_db_missing_index_details d
where s.group_handle = g.index_group_handle
and d.index_handle = g.index_handle
order by s.avg_user_impact desc
go
-- 自动重建或重新组织索引
-- 比较好用,慎用,特别是对于在线 db
-- ensure a use <databasename> statement has been executed first.
set nocount on;
declare @objectid int;
declare @indexid int;
declare @partitioncount bigint;
declare @schemaname nvarchar(130);
declare @objectname nvarchar(130);
declare @indexname nvarchar(130);
declare @partitionnum bigint;
declare @partitions bigint;
declare @frag float;
declare @command nvarchar(4000);
-- conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
-- and convert object and index ids to names.
select
object_id as objectid,
index_id as indexid,
partition_number as partitionnum,
avg_fragmentation_in_percent as frag
into #work_to_do
from sys.dm_db_index_physical_stats (db_id(), null, null , null, 'limited')
where avg_fragmentation_in_percent > 10.0 and index_id > 0;
-- declare the cursor for the list of partitions to be processed.
declare partitions cursor for select * from #work_to_do;
-- open the cursor.
open partitions;
-- loop through the partitions.
while (1=1)
begin;
fetch next
from partitions
into @objectid, @indexid, @partitionnum, @frag;
if @@fetch_status < 0 break;
select @objectname = quotename(o.name), @schemaname = quotename(s.name)
from sys.objects as o
join sys.schemas as s on s.schema_id = o.schema_id
where o.object_id = @objectid;
select @indexname = quotename(name)
from sys.indexes
where object_id = @objectid and index_id = @indexid;
select @partitioncount = count (*)
from sys.partitions
where object_id = @objectid and index_id = @indexid;
-- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
if @frag < 30.0
set @command = n'alter index ' + @indexname + n' on ' + @schemaname + n'.' + @objectname + n' reorganize';
if @frag >= 30.0
set @command = n'alter index ' + @indexname + n' on ' + @schemaname + n'.' + @objectname + n' rebuild';
if @partitioncount > 1
set @command = @command + n' partition=' + cast(@partitionnum as nvarchar(10));
exec (@command);
print n'executed: ' + @command;
end;
-- close and deallocate the cursor.
close partitions;
deallocate partitions;
-- drop the temporary table.
drop table #work_to_do;
go
-- 查看当前数据库索引的使用率
-- 非常的有用
select
object_name(object_id) as table_name,
(
select name
from sys.indexes
where object_id = stats.object_id and index_id = stats.index_id
) as index_name,
*
from sys.dm_db_index_usage_stats as stats
where database_id = db_id()
order by table_name
-- 指定表的索引使用情况
declare @table as nvarchar(100)
set @table = 't_name';
select
(
select name
from sys.indexes
where object_id = stats.object_id and index_id = stats.index_id
) as index_name,
*
from sys.dm_db_index_usage_stats as stats
where object_id = object_id(@table)
order by user_seeks, user_scans, user_lookups asc
--end index 分析优化的相关 sql