当前位置:首页 > PHP教程 > PHP总结归纳

解决SQLServer表或索引的碎片问题

  对表进行长期的修改或删除会产生大量的碎片,影响数据库性能。解决办法就是把表或索引重建,消除碎片,达到优化的目的。

  直接上代码:

  /*查询碎片,avg_fragmentation_in_percent就是索引占的百分比,大于30都是不正常的,,需要重建*/

  declare @db_id int;

  declare @object_id int;

  set @db_id = db_id(n'adventureworks2008r2');

  set @object_id = object_id(n'adventureworks2008r2.person.address');

  if @db_id is null

  begin;

  print n'invalid database';

  end;

  else if @object_id is null

  begin;

  print n'invalid object';

  end;

  else

  begin;

  select * from sys.dm_db_index_operational_stats(@db_id, @object_id, null, null);

  end;

  go

  /*重建表*/

  alter table <表名> rebuild

  /*重建索引*/

  alter index <索引名> on <表名> rebuild


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!