Sql Server维护计划中的重组索引与重建索引
在 SSW 更好的 SQL Server 数据库规则中,有一个完整数据库维护计划的示例:SSW。 在示例中,他们运行重新组织索引,然后运行重建索引,然后运行更新统计信息。 这有什么意义吗? 我认为重组索引是重建索引的快速但效率较低的版本? 并且索引重建也会自动更新统计信息(至少在聚集索引上)。
In the SSW rules to better SQL Server Database there is an example of a full database maintenance plan: SSW. In the example they run both a Reorganize Index and then a Rebuild Index and then Update Statistics. Is there any point to this? I thought Reorganize Index was a fast but less effective version of Rebuild Index? and that an index rebuild would also update the statistics automatically (on the clustered index at least).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更好的是:
或者
Even better is:
or
我在网上进行了研究,发现了一些不错的文章。 我在下面编写了函数和脚本,用于重新组织、重新创建或重建数据库中的所有索引。
首先,您可能需要阅读 这篇文章来了解为什么我们不只是重新创建所有索引。
其次,我们需要一个函数来构建索引的创建脚本。 因此,本文可能会有所帮助。 我还在下面分享工作功能。
最后一步创建一个 while 循环来查找和组织数据库中的所有索引。 此视频就是实现这一点的很好的例子。
功能:
SQL for while:
I researched on web and found some of good articles. At the and i wrote the function and script below which is reorganize, recreate or rebuild all the indexes in a database.
First you may need to read this article to understand why we're not just recreate all indexes.
Second we need a function to build create script for index. So this article may help. Also I'm sharing working function below.
Last step making a while loop to find and organize all indexes in the database. This video is grate example to make this.
Function:
Sql for while:
到底是什么Biri 说。 以下是我如何重新索引整个数据库:
Exactly what Biri said. Here is how I would reindex an entire database:
我使用此 SP
并创建一个每周执行此 SP 的作业。
I Use this SP
and create One Job that execute this SP every week.
重组索引时,如果索引分布在两个或多个物理文件中,则只会在数据文件内对数据进行碎片整理。 页面不会从一个数据文件移动到另一数据文件。
当索引位于单个文件中时,重组和重新索引将具有相同的最终结果。
有时重组会更快,有时重新索引会更快,具体取决于索引的碎片程度。 索引碎片越少,重组就会越快,碎片越多,重组就会越慢,但重新索引就会越快。
When doing a reorg of an index, if the index is spread across two or more physical files the data will only be defragged within the data file. Pages are not moved from one data file to another.
When the index is in a single file the reorg and reindex will have the same end result.
Some times the reorg will be faster, and some times the reindex will be faster depending on how fragmented the index is. The less fragmented the index then a reorg will be faster, the more fragmented the slower the reorg will be, but the faster a reindex will be.
我的两分钱...此方法遵循技术网上概述的规范: http://technet.microsoft.com/en-us/library/ms189858(v=sql.105).aspx
My two cents... This method follows the spec outlined on tech net: http://technet.microsoft.com/en-us/library/ms189858(v=sql.105).aspx
对相同索引执行
REORGANIZE
然后执行REBUILD
是没有意义的,因为执行REORGANIZE
所做的任何更改都会丢失重建。更糟糕的是,在 SSW 的维护计划图中,它首先执行
SHRINK
,这会导致索引碎片化,这是释放空间方式的副作用。 然后,在REBUILD
操作期间,REBUILD
再次为数据库文件分配更多空间作为工作空间。REORGANIZE
是一种在线操作,它使用很少的额外工作空间逐页对聚集或非聚集索引中的叶页进行碎片整理。REBUILD
在企业版中是在线操作,在其他版本中是离线操作,并且再次使用与索引大小一样多的额外工作空间。 它创建索引的新副本,然后删除旧的副本,从而消除碎片。 默认情况下,统计信息将作为此操作的一部分重新计算,但可以禁用。有关详细信息,请参阅重新组织和重建索引。
不要使用
SHRINK
除非使用TRUNCATEONLY
选项,即使这样,如果文件会再次增长,那么您应该认真考虑是否有必要:sqlservercentral_SHRINKFILE
Doing a
REORGANIZE
and then aREBUILD
on the same indexes is pointless, as any changes by theREORGANIZE
would be lost by doing theREBUILD
.Worse than that is that in the maintenance plan diagram from SSW, it performs a
SHRINK
first, which fragments the indexes as a side effect of the way it releases space. Then theREBUILD
allocates more space to the database files again as working space during theREBUILD
operation.REORGANIZE
is an online operation that defragments leaf pages in a clustered or non-clustered index page by page using little extra working space.REBUILD
is an online operation in Enterprise editions, offline in other editions, and uses as much extra working space again as the index size. It creates a new copy of the index and then drops the old one, thus getting rid of fragmentation. Statistics are recomputed by default as part of this operation, but that can be disabled.See Reorganizing and Rebuilding Indexes for more information.
Don't use
SHRINK
except with theTRUNCATEONLY
option and even then if the file will grow again then you should think hard as to whether it's necessary:sqlservercentral_SHRINKFILE
在考虑维护索引之前,重要的是要回答两个主要问题:
如本文所述 http ://solutioncenter.apexsql.com/why-when-and-how-to-rebuild-and-reorganize-sql-server-indexes/,并帮助您确定是否应该执行索引重建或索引重组,请理解以下内容:
索引重组是 SQL Server 遍历现有索引并将其清理的过程。 索引重建是一个繁重的过程,其中索引被删除,然后以全新的结构从头开始重新创建,没有所有堆积的碎片和空白页面。
虽然索引重组是一种纯粹的清理操作,它会保持系统状态不变,而不会锁定受影响的表和视图,但重建过程会在整个重建期间锁定受影响的表,这可能会导致长时间的停机在某些环境下是可以接受的。
考虑到这一点,很明显,索引重建是一个具有“更强”解决方案的过程,但它是有代价的——可能会对受影响的索引表进行长时间锁定。
另一方面,索引重组是一个“轻量级”过程,它将以一种不太有效的方式解决碎片问题——因为清理后的索引总是仅次于完全从头开始创建的新索引。 但从效率的角度来看,重组索引要好得多,因为它在操作过程中不会锁定受影响的索引表。
上述文章还解释了如何使用 SSMS、T-SQL(在表中重新组织/重建索引)和名为 ApexSQL Backup 的第三方工具来重新组织和重建索引。
Before considering maintenance of indexes, it is important to answer two main questions:
As described in this article http://solutioncenter.apexsql.com/why-when-and-how-to-rebuild-and-reorganize-sql-server-indexes/, and to help you determine if you should perform index rebuild or index reorganization, please understand the following:
Index reorganization is a process where the SQL Server goes through existing index, and cleans it up. Index rebuild is a heavy-duty process where index is deleted and then recreated from scratch with entirely new structure, free from all piled up fragments and empty-space pages.
While index reorganization is a pure cleanup operation which leaves system state as it is without locking-out affected tables and views, the rebuild process locks affected table for the whole rebuild period, which may result in long down-times that could not be acceptable in some environments.
With this in mind, it is clear that the index rebuild is a process with ‘stronger’ solution, but it comes with a price – possible long locks on affected indexed tables.
On the other side, index reorganization is a ‘lightweight’ process that will solve the fragmentation in a less effective way – since cleaned index will always be second to the new one fully made from scratch. But reorganizing index is much better from the efficiency standpoint, since it does not lock affected indexed table during the course of operation.
The above mentioned article also explains how to reorganize and rebuild indexes using SSMS, T-SQL (to reorganize/rebuild indexes in a table) and a 3rd party tool called ApexSQL Backup.
重组和重建是不同的事情。
重组:这是索引的碎片整理。 获取现有索引并对现有页面进行碎片整理。 但是,如果页面不连续,它们将保持像以前一样。 仅页面内容发生变化。
重建:实际上它会删除索引并从头开始重建。 这意味着您将获得一个全新的索引,其中包含经过碎片整理的连续页面。
此外,通过重建,您可以更改分区或文件组,但通过重新组织,您不仅可以对整个索引进行碎片整理,还可以仅对索引的一个分区进行碎片整理。
更新统计信息在聚集索引上是自动的,但在非聚集索引上则不是。
The reorganize and rebuild are different things.
Reorganize: it's a defrag for indexes. Takes the existing index(es) and defragments the existing pages. However if the pages are not in a contiguous manner, they stays like before. Only the content of the pages are changing.
Rebuild: actually it drops the index and rebuilds it from scratch. It means that you will get a completely new index, with defragmented and contiguous pages.
Moreover with rebuild you can change partitioning or file groups, but with reorganize you can defrag not only the whole index, but also only one partition of the index.
The update statistics is automatic on clustered indexes, but not on the non-clustered ones.