用于重建和重新索引碎片索引的脚本?
当“avg_fragmentation_in_percent”超过一定限制时(如果不使用游标更好),任何人都可以提供用于重建和重新索引碎片索引的脚本吗?
Can anyone provide the script for rebuilding and re-indexing the fragmented index when 'avg_fragmentation_in_percent' exceeds certain limits (better if cursor is not used)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
重建使用:
或重新组织使用:
应在较低 (<30%) 碎片时使用重组,但只有重建(对数据库来说较重)才能将碎片减少到 0%。
有关详细信息,请参阅 https://msdn.microsoft.com/en-us/library /ms189858.aspx
To rebuild use:
or to reorganize use:
Reorganizing should be used at lower (<30%) fragmentations but only rebuilding (which is heavier to the database) cuts the fragmentation down to 0%.
For further information see https://msdn.microsoft.com/en-us/library/ms189858.aspx
简介
根据问题的严重性,您可以使用两种解决方案
替换为您自己的值,如下所示:
XXXMYINDEXXXX
替换为索引名称。XXXMYTABLEXXX
替换为表的名称。XXXDATABASENAMEXXX
替换为数据库的名称。解决方案 1. 索引
在离线模式下为表重建所有索引
在离线模式下为表重建一个指定索引
解决方案 2. 碎片
碎片是经常出现问题的表的一个问题添加和删除的条目。
检查碎片百分比
碎片 5..30%
如果碎片值大于 5%,但小于 30%,则值得重新组织索引。
重新整理一张表的所有索引
重新整理一张表的某个指定索引
碎片30%+
如果碎片值在30%以上,那么就值得重建在线模式下的索引。
在线模式重建表的所有索引
在线模式重建表的某一指定索引
Introduction
There are two solutions available to you depending on the severity of your issue
Replace with your own values, as follows:
XXXMYINDEXXXX
with the name of an index.XXXMYTABLEXXX
with the name of a table.XXXDATABASENAMEXXX
with the name of a database.Solution 1. Indexing
Rebuild all indexes for a table in offline mode
Rebuild one specified index for a table in offline mode
Solution 2. Fragmentation
Fragmentation is an issue in tables that regularly have entries both added and removed.
Check fragmentation percentage
Fragmentation 5..30%
If the fragmentation value is greater than 5%, but less than 30% then it is worth reorganising indexes.
Reorganise all indexes for a table
Reorganise one specified index for a table
Fragmentation 30%+
If the fragmentation value is 30% or greater then it is worth rebuilding then indexes in online mode.
Rebuild all indexes in online mode for a table
Rebuild one specified index in online mode for a table
REBUILD/REORGANIZE 索引查询
操作 查询:
输出
Query for REBUILD/REORGANIZE Indexes
Query:
Output
这是我从 http://www. foliotek.com/devblog/sql-server-optimization-with-index-rebuilding 我发现在这里发布很有用。
尽管它使用游标,并且我知道游标的主要问题是什么,但它可以轻松转换为无游标版本。
它有详细的文档记录,您可以轻松阅读并根据您的需要进行修改。
Here is the modified script which i took from http://www.foliotek.com/devblog/sql-server-optimization-with-index-rebuilding which i found useful to post here.
Although it uses a cursor and i know what is the main problem with cursors it can be easily converted to a cursor-less version.
It is well-documented and you can easily read through it and modify to your needs.
2016 年和 2017 年的真正答案是:使用 Ola Hallengren 的脚本:
https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html
在我们共同进化的这一点上,这就是我们任何人都需要知道或烦恼的事情。
The real answer, in 2016 and 2017, is: Use Ola Hallengren's scripts:
https://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html
That is all any of us need to know or bother with, at this point in our mutual evolution.
我发现以下脚本非常适合维护索引,您可以将其安排为每晚运行或您希望的任何其他时间范围运行。
http://sqlfool.com/2011/06/index-defrag -脚本-v4-1/
I have found the following script is very good at maintaining indexes, you can have this scheduled to run nightly or whatever other timeframe you wish.
http://sqlfool.com/2011/06/index-defrag-script-v4-1/