本文主要是介绍sqlserver索引碎片过大如何处理 sqlserver索引碎片查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、概述
SQLServer提供了一个数据库命令——DBCC SHOWCONTIG——来确定一个指定的表或索引是否有碎片。
示例:
显示数据库里所有索引的碎片信息
DBCC SHOWCONTIG WITH ALL_INDEXES
显示指定表的所有索引的碎片信息
DBCC SHOWCONTIG (authors) WITH ALL_INDEXES
显示指定索引的碎片信息
DBCC SHOWCONTIG (authors,aunmind)
DBCC 执行结果:
扫描页数:如果你知道行的近似尺寸和表或索引里的行数,那么你可以估计出索引里的页数。看看扫描页数,如果明显比你估计的页数要高,说明存在内部碎片。
扫描扩展盘区数:用扫描页数除以8,四舍五入到下一个最高值。该值应该和DBCC SHOWCONTIG返回的扫描扩展盘区数一致。如果DBCC SHOWCONTIG返回的数高,说明存在外部碎片。碎片的严重程度依赖于刚才显示的值比估计值高多少。
扩展盘区开关数:该数应该等于扫描扩展盘区数减1。高了则说明有外部碎片。
每个扩展盘区上的平均页数:该数是扫描页数除以扫描扩展盘区数,一般是8。小于8说明有外部碎片。
扫描密度[最佳值:实际值]:DBCC SHOWCONTIG返回最有用的一个百分比。这是扩展盘区的最佳值和实际值的比率。该百分比应该尽可能靠近100%。低了则说明有外部碎片。
逻辑扫描碎片:无序页的百分比。该百分比应该在0%到10%之间,高了则说明有外部碎片。
扩展盘区扫描碎片:无序扩展盘区在扫描索引叶级页中所占的百分比。该百分比应该是0%,高了则说明有外部碎片。
每页上的平均可用字节数:所扫描的页上的平均可用字节数。越高说明有内部碎片,不过在你用这个数字决定是否有内部碎片之前,应该考虑fill factor(填充因子)。
平均页密度(完整):每页上的平均可用字节数的百分比的相反数。低的百分比说明有内部碎片。
查看索引碎片(常规)
--查看索引碎片
select
db_name(database_id) as '数据库名',
object_name(t.object_id) as '表名',
t.index_id as '索引id',
t1.index_name as '索引名称',
t1.type_desc as '索引类型',
t1.column_name as '索引列名',
t.partition_number as '当前索引所在分区',
t.page_count as '页统计',
t.avg_page_space_used_in_percent as '页使用率/填充因子' ,
t.record_count as '页行记录数',
t.avg_record_size_in_bytes as '平均每条记录大小(B)',
t.avg_fragmentation_in_percent as '索引碎片比率',
t.fragment_count as '索引中的碎片数量',
t.avg_fragment_size_in_pages as '一个索引中碎片的平均页数'
from sys.dm_db_index_physical_stats(db_id('db_tank'),NULL,NULL,NULL,NULL) t
join (select distinct t3.object_id,t3.index_id,object_name(t3.object_id) as 'table_name',t3.name as 'index_name',t3.type_desc, t1.name as 'column_name'
from sys.columns t1 join sys.index_columns t2 on t1.column_id = t2.column_id AND t1.object_id = t2.object_id
join sys.indexes t3 on t2.index_id = t3.index_id AND t2.object_id = t3.object_id) t1
on t1.object_id = t.object_id AND t1.index_id = t.index_id
where object_name(t.object_id) = 'sys_users_goods'--查看所有表中对应的索引名与索引列
select t3.object_id,object_name(t3.object_id) as 'table_name',t3.name as 'index_name',t3.type_desc, t1.name as 'column_name'
from sys.columns t1 join sys.index_columns t2 on t1.column_id = t2.column_id AND t1.object_id = t2.object_id
join sys.indexes t3 on t2.index_id = t3.index_id AND t2.object_id = t3.object_id
where object_name(t3.object_id) = 'sys_users_goods'--查看表中所有索引
SELECT name,type_desc FROM sys.indexes WHERE object_id=object_id('O_Orders')--根据索引名称查看对应的列
DBCC SHOW_STATISTICS(O_Orders,idx_order_status_2)
DBCC SHOW_STATISTICS(O_Orders,IX_O_OrdersUID)--查找碎片率大于40%的
SELECT object_name(object_id) ,index_type_desc,alloc_unit_type_desc,avg_fragmentation_in_percent,fragment_count,avg_fragment_size_in_pages,page_count,record_count,avg_page_space_used_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID('DBNAME'),
OBJECT_ID(''),NULL,NULL,'Sampled')
WHERE avg_fragmentation_in_percent>40
最有效的索引碎片查看方式(必看)
use gameabcAction;--查看索引碎片
select
db_name(database_id) as '数据库名',
object_name(t1.object_id) as obj_name,
t.index_id as '索引id',
t1.index_name as '索引名称',
t1.type_desc as '索引类型',
t1.column_name as '索引列名',
t.partition_number as '所在分区',
t.page_count as '页统计',
cast(t.page_count * 8.0/1024 as decimal(10,2)) as 'indexMB',
t.avg_fragmentation_in_percent as '索引碎片比率',
t2.rows as '行记录数',t.fragment_count as '索引中的碎片数量',
t.avg_fragment_size_in_pages as '一个索引中碎片的平均页数',
t.avg_page_space_used_in_percent as '页使用率/填充因子' ,
t.record_count as '页行记录数',
t.avg_record_size_in_bytes as '平均每条记录大小(B)'
from sys.dm_db_index_physical_stats(db_id('gameabcAction'),NULL,NULL,NULL,NULL) t
join
(select distinct t3.object_id,t3.index_id,t3.name as 'index_name',t3.type_desc, t1.name as 'column_name' --t4.name,t4.xtype,from (select t1.object_id,t2.index_id,stuff((select ','+name from sys.index_columns q2 join sys.columns q1 on q1.column_id = q2.column_id AND q1.object_id = q2.object_idwhere q1.object_id = t1.object_id and q2.index_id=t2.index_idfor xml path('')),1,1,'') as name from sys.index_columns t2 join sys.columns t1 on t1.column_id = t2.column_id AND t1.object_id = t2.object_idgroup by t1.object_id,t2.index_id) t1join sys.indexes t3 on t1.index_id = t3.index_id AND t1.object_id = t3.object_id--join sys.sysobjects t4 on t3.object_id = t4.id--where t3.object_id >100) t1
on t1.object_id = t.object_id AND t1.index_id = t.index_id
left join sys.sysindexes t2 on t1.object_id = t2.id and t.index_id=t2.indid
where t.avg_fragmentation_in_percent>0
order by [索引碎片比率] desc,[indexMB] desc
解决碎片问题
1. 删除并重建索引
2. 使用DROP_EXISTING子句重建索引
3. 执行DBCC DBREINDEX
4. 执行DBCC INDEXDEFRAG
dbcc showcontig('Log_FairBattlePrestige')
dbcc dbreindex('Log_FairBattlePrestige')alter table index_name on table_name rebuild with(online=off);ALTER INDEX ALL ON Employee REBUILD WITH (FILLFACTOR = 60, SORT_IN_TEMPDB = ON,STATISTICS_NORECOMPUTE = ON);-------------------------
STATISTICS_NORECOMPUTE = {ON | OFF}
指定是否重新计算分布统计信息。默认为OFF。
SORT_IN_TEMPDB = {ON | 关闭 }
适用于:SQL Server(从SQL Server 2008开始)和SQL数据库。
指定是否将排序结果存储在tempdb中。默认为OFF。
ON
用于构建索引的中间排序结果存储在tempdb中。如果tempdb与用户数据库位于不同的磁盘集上,则可能会减少创建索引所需的时间。但是,这会增加索引构建期间使用的磁盘空间量。
OFF
中间排序结果与索引存储在同一数据库中。
如果不需要排序操作,或者可以在内存中执行排序,则忽略SORT_IN_TEMPDB选项。
这篇关于sqlserver索引碎片过大如何处理 sqlserver索引碎片查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!