技术开发 频道

如何将索引碎片数量降至最低


【IT168技术文档】

  索引碎片能增大索引树的大小,增加不必要的IO,所以每隔一段时间对索引碎片进行检查时很有必要的。
下面一个示例一起来分析如何将索引降至最低。

  新建一个表:
create table t3 ( i int primary key, xx varchar(200) not null )
  加入数据:
declare @x int set @x = 0 while @x <1000 begin insert into t3 values (@x,'qweasdqweasdqweasdqweqweasdqwe') set @x = @x+1 end
  执行动态管理视图:
SELECT index_id,index_type_desc,avg_fragmentation_in_percent,page_count FROM sys.dm_db_index_physical_stats(db_id(), OBJECT_ID('t3'), NULL, NULL , 'LIMITED');
  现在执行几个可以减少碎片的方法都不管用,不能减少碎片。
  包括:
DBCC INDEXDEFRAG (test, 'dbo.t3', PK__t3__0EA330E9) alter index PK__t3__0EA330E9 on t3 rebuild dbcc dbreindex ('t3')
  这几个方法还有删除重建索引,都不能减少碎片数量。

  后来我觉得是因为数据太少了,导致页也很少,数据库可能存在某种智能,判断是否值得去做重建索引的工作,所以加大的数据量:
declare @x int set @x = 1000 while @x <10000 begin insert into t3 values (@x,'qweasdqweasdqweasdqweqweasdqwe') set @x = @x+1 end
  再执行语句:
SELECT index_id,index_type_desc,avg_fragmentation_in_percent,page_count FROM sys.dm_db_index_physical_stats(db_id(), OBJECT_ID('t3'), NULL, NULL , 'LIMITED'); alter index t3index on t3 rebuild
0
相关文章