官方文档
https://docs.microsoft.com/zh-cn/sql/t-sql/database-console-commands/dbcc-showcontig-transact-sql?view=sql-server-ver15
https://docs.microsoft.com/zh-cn/sql/relational-databases/system-dynamic-management-views/sys-dm-db-index-physical-stats-transact-sql?view=sql-server-ver15
1、虽然官方推荐使用sys.dm_db_index_physical_stats来代替DBCC SHOWCONTIG查看碎片信息,但是DBCC SHOWCONTIG显示出来的结果还是更直观些
2、如果表有聚集索引,表就是索引组织表,对表进行碎片整理,使用ALTER INDEX ALL ON Table_Name REORGANIZE就行
3、如果表没有聚集索引,表就是堆表,对表进行碎片整理,如果ALTER INDEX ALL ON Table_Name REORGANIZE没有达到效果,就再使用alter table table_name REBUILD WITH ( <rebuild_option> )
查询wondb库下dbo.RSMsi1的碎片信息
方法1:使用DBCC SHOWCONTIG ,Avg. Page Density (full)越小,碎片越大
DBCC SHOWCONTIG ('wondb.dbo.RSMsi1');
DBCC SHOWCONTIG scanning 'RsmSi1' table...
Table: 'RsmSi1' (1419152101); index ID: 1, database ID: 24
TABLE level scan performed.
- Pages Scanned................................: 3728
- Extents Scanned..............................: 452
- Extent Switches..............................: 351
- Avg. Pages per Extent........................: 7.8
- Scan Density [Best Count:Actual Count].......: 98.41% [248:252]
- Logical Scan Fragmentation ..................: 0.40%
- Extent Scan Fragmentation ...................: 4.37%
- Avg. Bytes Free per Page.....................: 800.4
- Avg. Page Density (full).....................: 60.11%
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
方法2:使用sys.dm_db_index_physical_stats
use wondb;--以下语句需要在当前数据库下执行
select index_type_desc,avg_fragment_size_in_pages,page_count,avg_fragmentation_in_percent from sys.dm_db_index_physical_stats (db_id(), object_id ('RSMsi1'), default, default, default)
index_type_desc avg_fragment_size_in_pages page_count avg_fragmentation_in_percent
CLUSTERED INDEX 164.833333333333 3728 0.404448938321537
NONCLUSTERED INDEX 159.818181818182 3721 0.341296928327645
整理wondb库下dbo.RSMsi1的碎片
方法1:重新组织索引而非重建索引,REORGANIZE重新组织索引不会引发堵塞,再收集统计信息
USE WONDB;
GO
ALTER INDEX ALL ON [dbo].[RSMsi1] REORGANIZE ;
GO
UPDATE STATISTICS [dbo].[RSMsi1];
GO
再查看碎片信息
DBCC SHOWCONTIG (RSMsi1);
DBCC SHOWCONTIG scanning 'RsmSi1' table...
Table: 'RsmSi1' (1419152101); index ID: 1, database ID: 24
TABLE level scan performed.
- Pages Scanned................................: 1978
- Extents Scanned..............................: 249
- Extent Switches..............................: 248
- Avg. Pages per Extent........................: 7.9
- Scan Density [Best Count:Actual Count].......: 99.60% [248:249]
- Logical Scan Fragmentation ..................: 0.35%
- Extent Scan Fragmentation ...................: 6.83%
- Avg. Bytes Free per Page.....................: 800.4
- Avg. Page Density (full).....................: 90.11%
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
select index_type_desc,avg_fragment_size_in_pages,page_count,avg_fragmentation_in_percent from sys.dm_db_index_physical_stats (db_id(), object_id (‘RSMsi1’), default, default, default)
index_type_desc avg_fragment_size_in_pages page_count avg_fragmentation_in_percent
CLUSTERED INDEX 104.105263157895 1978 0.353892821031345
NONCLUSTERED INDEX 103.411764705882 1758 0.341296928327645
方法2:再对表执行rebuild操作
alter table RSMsi1 rebuild with (ONLINE = ON)
REBUILD WITH ( <rebuild_option> )
如果表具有聚集索引,则 REBUILD 选项将重新生成该聚集索引。 REBUILD 可以为 ONLINE 操作。
如果表没有聚集索引,堆结构只受部分选项影响。
再查看碎片信息
DBCC SHOWCONTIG (RSMsi1);
DBCC SHOWCONTIG scanning 'RsmSi1' table...
Table: 'RsmSi1' (1419152101); index ID: 1, database ID: 24
TABLE level scan performed.
- Pages Scanned................................: 1978
- Extents Scanned..............................: 252
- Extent Switches..............................: 251
- Avg. Pages per Extent........................: 7.8
- Scan Density [Best Count:Actual Count].......: 98.41% [248:252]
- Logical Scan Fragmentation ..................: 0.40%
- Extent Scan Fragmentation ...................: 4.37%
- Avg. Bytes Free per Page.....................: 800.4
- Avg. Page Density (full).....................: 90.11%
DBCC execution completed. If DBCC printed error messages, contact your system administrator.
select index_type_desc,avg_fragment_size_in_pages,page_count,avg_fragmentation_in_percent from sys.dm_db_index_physical_stats (db_id(), object_id (‘RSMsi’), default, default, default)
index_type_desc avg_fragment_size_in_pages page_count avg_fragmentation_in_percent
CLUSTERED INDEX 164.833333333333 1978 0.404448938321537
NONCLUSTERED INDEX 103.411764705882 1758 0.341296928327645