索引碎片整理需要永远运行

deu*_*uid 5 index sql-server-2005

我们有一个代理工作,每天早上 2 点运行。对前 5 个最大的表进行碎片整理。通常需要大约 20 分钟才能完成。多年来,它一直保持这种状态。但是,最近(上周左右)我注意到它花费的时间要长得多(2-7 小时)。今天早上,我不得不终止这项工作,因为它已经运行了 8 个半小时。

我查看了它的日志,看起来在我取消作业时它甚至还没有完成第一个表中的索引:

Job '02.00 am - Index defrag' : Step 1, 'run index defrag' : Began Executing 2012-06-27 02:00:05

DBCC: Defrag phase of index 'table1_pk_index' is 27% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 27% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 27% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 27% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 28% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 29% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 30% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 31% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 31% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 32% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 33% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 33% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 34% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 34% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 35% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 35% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 36% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 43% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 43% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 43% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 44% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 44% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 44% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 45% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 45% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 45% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 45% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 46% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 46% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 47% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 47% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 48% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 48% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 49% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 49% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 50% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 50% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 51% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 52% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 52% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 52% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 53% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 53% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 53% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 54% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 54% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 54% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 55% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 55% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 55% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 56% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 56% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 57% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 57% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 58% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 59% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 59% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 60% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 60% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 61% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 62% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 63% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 63% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 64% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 65% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 65% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 66% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 67% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 73% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 77% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 79% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 80% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 82% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 84% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 86% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 88% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 90% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 91% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 94% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 96% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_pk_index' is 98% complete. [SQLSTATE 01000]
Index Name                                                                                                                       Pages Scanned        Pages Moved          Pages Removed       
-------------------------------------------------------------------------------------------------------------------------------- -------------------- -------------------- --------------------
table1_pk_index                                                                                                           2145739              1546226              236
DBCC: Defrag phase of index 'table1_index1' is 66% complete. [SQLSTATE 01000]
Index Name                                                                                                                       Pages Scanned        Pages Moved          Pages Removed       
-------------------------------------------------------------------------------------------------------------------------------- -------------------- -------------------- --------------------
subscription_orders_idx1                                                                                                         434585               374095               768
DBCC: Compaction phase of index 'table1_index2' is 93% complete. [SQLSTATE 01000]
DBCC: Defrag phase of index 'table1_index2' is 38% complete. [SQLSTATE 01000]
Msg 0, Sev 0, State 1: Operation canceled [SQLSTATE HY008]

Query cancelled.
Run Code Online (Sandbox Code Playgroud)

从“DBCC:索引的碎片整理阶段...”的多行来看,我可以看出主键索引的时间比平时长得多(只是一个附带问题,有没有人知道这些消息的间隔时间?生成,因为 SQL Server 在时间戳方面非常节省。)

查看索引碎片信息,似乎所有三个索引的平均碎片百分比都非常低,这是预期的,因为它们每天都在进行碎片整理:

  • table1_pk_index:0.103168974377375%
  • table1_index1:0.100508753691454%
  • table2_index2:0.734118693782182%

这些数字对我来说看起来很正常,所以我有点难住了。是什么导致这些索引需要更长的时间进行碎片整理?我还应该注意什么?

更新:我按照 Remus 的建议深入研究了这个问题,发现磁盘队列长度在碎片整理发生的时候已经超过了屋顶。事实证明,这是在几天前我们将报告服务器上的恢复时间(使用 Litespeed 从生产服务器上的备份文件恢复)移动后开始发生的,因此它与碎片整理的计划时间一致。即使这两个作业从两个不同的逻辑驱动器读取/写入文件,两个驱动器碰巧位于同一个 LUN 上,无法处理并发 IO 请求的数量。一旦我们将碎片整理的时间移到另一个时间段,问题就消失了。

Rem*_*anu 8

您应该像处理任何性能故障排除问题一样处理索引重组性能故障排除问题:识别等待和资源,例如使用等待和队列之类的方法。一旦您进行尽职调查并收集数据(性能计数器、等待统计数据等),如果您仍然无法找到问题,我们可以提供帮助,而不是让我们掷 8 球并猜测(更重要的是,而不是猜测工作)。

接下来,有许多维护脚本比每天晚上重新组织令人作呕的内容要好得多,例如Ola HallengrenMichelle Ufford 仅举两个例子。

  • 我给你提供了帮助我解决问题的合理建议的学分。除了每晚重组索引之外,我将开始寻找其他选项。用决议更新了我的答案。 (2认同)