Answers:
很难说您要花多长时间重建,因为SQL本身并不真正预先知道,也无法给您一个估计。
您可以使用以下查询来使用dm_exec_requests dmv来查看索引重建进行了多长时间,并验证SQL确实没有估算值:
SELECT r.session_id,r.command,CONVERT(NUMERIC(6,2),r.percent_complete)
AS [Percent Complete],CONVERT(VARCHAR(20),DATEADD(ms,r.estimated_completion_time,GetDate()),20) AS [ETA Completion Time],
CONVERT(NUMERIC(10,2),r.total_elapsed_time/1000.0/60.0) AS [Elapsed Min],
CONVERT(NUMERIC(10,2),r.estimated_completion_time/1000.0/60.0) AS [ETA Min],
CONVERT(NUMERIC(10,2),r.estimated_completion_time/1000.0/60.0/60.0) AS [ETA Hours],
CONVERT(VARCHAR(1000),(SELECT SUBSTRING(text,r.statement_start_offset/2,
CASE WHEN r.statement_end_offset = -1 THEN 1000 ELSE (r.statement_end_offset-r.statement_start_offset)/2 END)
FROM sys.dm_exec_sql_text(sql_handle)))
FROM sys.dm_exec_requests r WHERE command IN ('Alter Index')
但是,如果要真正估算所需的时间,则可以阅读sqlmunkee上的一篇不错的博客文章,该文章通过说“ ..it取决于”来进行总结:
而且由于我们并非都使用相同的硬件,使用相同的软件或查看相同的数据,因此答案必须是……..这取决于
令人沮丧,但事实如此,令人遗憾。
我设法找到了一个带有magick脚本的博客文章,据称该脚本完成了该任务,无法检查,因为这似乎不适用于我正在运行的SQL Server 2014,查询阻止了共享锁。也许有人会觉得它有用,所以我就把它留在这里。
;WITH cte AS
(
SELECT
object_id,
index_id,
partition_number,
rows,
ROW_NUMBER() OVER(PARTITION BY object_id, index_id, partition_number ORDER BY partition_id) as rn
FROM sys.partitions
)
SELECT
object_name(cur.object_id) as TableName,
cur.index_id,
cur.partition_number,
PrecentDone =
CASE
WHEN pre.rows = 0 THEN 0
ELSE
((cur.rows * 100.0) / pre.rows)
END,
pre.rows - cur.rows as MissingRows
FROM cte as cur
INNER JOIN cte as pre on (cur.object_id = pre.object_id) AND (cur.index_id = pre.index_id) AND (cur.partition_number = pre.partition_number) AND (cur.rn = pre.rn +1)
ORDER BY 4