您好,欢迎访问一九零五行业门户网

SQL Server 重建索引 Rebuild Index

重建索引是为了减少数据碎片。数据碎片会导致 sql server 进行不必要的数据读,降低 sql server 的性能。重建索引也会同时更新列统计,而如果查询所使用的列缺少或遗漏统计信息,这可能导致 sql server 内部的优化器选择比预期效率低的查询计划。 如果您重建
重建索引是为了减少数据碎片。数据碎片会导致sql server进行不必要的数据读,降低sql server的性能。重建索引也会同时更新列统计,而如果查询所使用的列缺少或遗漏统计信息,这可能导致sql server内部的优化器选择比预期效率低的查询计划。
如果您重建了某张表上的聚集索引,该表上的非聚集索引也同时会被更新。
要更新索引,您可以使用maintenance wizard(相关内容您可以参考http://msdn.microsoft.com/en-us/library/ms180074.aspx),或在sql server代理(agent)中运行如下的自定义代码来更新某个数据库中所有表上的索引:
您可以根据您的需求修改dbreindex的参数。
需要注意的是,重建非聚集索引时该表会暂时加上共享锁,对用户不可进行select以外的操作;重建聚集索引时该表会暂时加上排外锁,不允许任何用户访问。因此需要制定好计划来预防可能的访问问题。
rebuild有一个fill factor参数,如果fill factor设置为100%,这意味着每一个索引页都是完全满的,如果fill factor设置为50%意味着每个索引页都是半满的。对于fill factor 100%,每次新插入或更新一个记录,由于当前页没有空间可用,可能有分页情况产生。过多的分页会降低sql server的性能。下面具体举个例子:
假设您在一张表上建立了一个使用默认fill factor的新索引。当sql server创建索引时,它会把索引放置在连续的物理页上,以使数据顺序地被读,i/o访问最优化。但当表因insert,update,delete等操作增长改变时,分页发生,sql server在磁盘的其他地方分配新的页,导致新的页与原物理页不连续,增加了随机i/o,访问索引页变慢。
那么fill factor的合适值应该为多少?这取决于表的读/写比:
低更新表(读/写比:100比1):100% fill factor
高更新表(写超过读):50%-70% fill factor
居中:80%-90% fill factor
过低的fill factor会增加页的数量,也会导致更多的页需要被移至缓存,缓存中有用的数据减少。默认的fill factor为0(即100% fill factor),通常这不是个好的选择,特别是对于聚集索引。
如果您无法判断设置什么fill factor,您首先需要确定磁盘的读/写比.方法就是使用如下两个计数器:
physical disk object: % disk read time 和 physical disk object: % write time。另外一个可能有用的计数器就是:sql server access methods: pages splits/sec。这个计数器测量sql server内每秒分页的次数。如果该数值过高,您需要降低fill factor防止新的分页。
如果您想确认您的索引因分页产生的碎片程度,您可以运行dbcc showcontig命令。如果看特定表和特定索引,您可以运行如下代码:
结果集中最重要的参数是scan density,越接近100%越好。如果scan density小于75%,那么您可能需要重建表中的索引。
对于小于100数据页,重建索引并不会有明显的性能改善。这是因为物理硬件缓存,sql server缓存和sql server预读机制隐藏了碎片的负面作用。但对于非常大的表,重建索引会使它受益匪浅,因为涉及大量磁盘i/o操作。
alter index idx_name on tblcompany rebuild
use databasename --enter the name of the database you want to reindexdeclare @tablename varchar(255)declare tablecursor cursor for select table_name from information_schema.tables where table_type = 'base table'open tablecursorfetch next from tablecursor into @tablenamewhile @@fetch_status = 0 begin dbcc dbreindex(@tablename, '', 90) fetch next from tablecursor into @tablename endclose tablecursordeallocate tablecursor
--script to identify table fragmentation--declare variablesdeclare @id int, @indexid int, @indexname varchar(128)--set the table and index to be examinedselect @indexname = 'index_name' --enter name of indexset @id = object_id('table_name') --enter name of table--get the index valuesselect @indexid = indidfrom sysindexeswhere id = @id and name = @indexname--display the fragmentationdbcc showcontig (@id, @indexid)
--方法一: use adventureworksgoexec sp_msforeachtable @command1=print '?' dbcc dbreindex ('?', '', 90)goexec sp_updatestatsgo --方法二:use adventureworksgocreate procedure usp_reindex_updatestatsas declare @mytable varchar(255) declare mycursor cursor for select table_name from information_schema.tables where table_type = 'base table' and table_name not like 'sys%' open mycursor fetch next from mycursor into @mytable while @@fetch_status = 0 begin print 'reindexing table: ' + @mytable dbcc dbreindex(@mytable, '', 90) fetch next from mycursor into @mytable end close mycursor deallocate mycursor exec sp_updatestatsgo
其它类似信息

推荐信息