gpt4 book ai didi

sql-server - 如何查询SQL Server数据库恢复需要多长时间?

转载 作者:行者123 更新时间:2023-12-02 14:23:17 28 4
gpt4 key购买 nike

我正在尝试编写一个查询来告诉我在 SQL Server 2008 上恢复(完整恢复或日志恢复)花费了多少时间。

我可以运行此查询来了解备份花费了多少时间:

select  database_name, 
[uncompressed_size] = backup_size/1024/1024,
[compressed_size] = compressed_backup_size/1024/1024,
backup_start_date,
backup_finish_date,
datediff(s,backup_start_date,backup_finish_date) as [TimeTaken(s)],
from msdb..backupset b
where type = 'L' -- for log backups
order by b.backup_start_date desc

此查询将告诉我恢复了什么,但现在花费了多少时间:

select * from msdb..restorehistory

restorehistory 有一个 backup_set_id 列,它将链接到 msdb..backupset,但它保存 的开始和结束日期>备份而不是恢复。

知道在哪里查询恢复的开始和结束时间吗?

最佳答案

要查找 RESTORE DATABASE 时间,我发现您可以使用以下查询:

declare @filepath nvarchar(1000) 

SELECT @filepath = cast(value as nvarchar(1000)) FROM [fn_trace_getinfo](NULL)
WHERE [property] = 2 and traceid=1

SELECT *
FROM [fn_trace_gettable](@filepath, DEFAULT)
WHERE TextData LIKE 'RESTORE DATABASE%'
ORDER BY StartTime DESC;

缺点是,您会注意到,至少在我的测试服务器上,EndTime 始终为 NULL。

因此,我提出了第二个查询来尝试确定结束时间。首先,我很抱歉这非常丑陋并且疯狂地嵌套。

下面的查询假设如下:

  1. 运行恢复时,对于该 DatabaseID 和 ClientProcessID,下一个 EventSequence 包含我们需要的 TransactionID。
  2. 然后我去查找事务的最大 EventSequence
  3. 最后,我选择包含 RESTORE DATABASE 的记录以及与该记录关联的最大事务数。

我确信有人可能会采用我所做的并改进它,但这似乎适用于我的测试环境:

declare @filepath nvarchar(1000) 

SELECT @filepath = cast(value as nvarchar(1000)) FROM [fn_trace_getinfo](NULL)
WHERE [property] = 2 and traceid=1

SELECT *
FROM [fn_trace_gettable](@filepath, DEFAULT) F5
INNER JOIN
(
SELECT F4.EventSequence MainSequence,
MAX(F3.EventSequence) MaxEventSequence, F3.TransactionID
FROM [fn_trace_gettable](@filepath, DEFAULT) F3
INNER JOIN
(
SELECT F2.EventSequence, MIN(TransactionID) as TransactionID
FROM [fn_trace_gettable](@filepath, DEFAULT) F1
INNER JOIN
(
SELECT DatabaseID, SPID, StartTime, ClientProcessID, EventSequence
FROM [fn_trace_gettable](@filepath, DEFAULT)
WHERE TextData LIKE 'RESTORE DATABASE%'
) F2 ON F1.DatabaseID = F2.DatabaseID AND F1.SPID = F2.SPID
AND F1.ClientProcessID = F2.ClientProcessID
AND F1.StartTime > F2.StartTime
GROUP BY F2.EventSequence
) F4 ON F3.TransactionID = F4.TransactionID
GROUP BY F3.TransactionID, F4.EventSequence
) F6 ON F5.EventSequence = F6.MainSequence
OR F5.EventSequence = F6.MaxEventSequence
ORDER BY F5.StartTime

编辑

我对查询进行了一些更改,因为我使用的测试数据库之一区分大小写,并且丢失了一些记录。我还注意到从磁盘恢复时 DatabaseID 为空,所以我现在也在处理它:

SELECT * 
FROM [fn_trace_gettable](@filepath, DEFAULT) F5
INNER JOIN
(
SELECT F4.EventSequence MainSequence,
MAX(F3.EventSequence) MaxEventSequence, F3.TransactionID
FROM [fn_trace_gettable](@filepath, DEFAULT) F3
INNER JOIN
(
SELECT F2.EventSequence, MIN(TransactionID) as TransactionID
FROM [fn_trace_gettable](@filepath, DEFAULT) F1
INNER JOIN
(
SELECT DatabaseID, SPID, StartTime, ClientProcessID, EventSequence
FROM [fn_trace_gettable](@filepath, DEFAULT)
WHERE upper(convert(nvarchar(max), TextData))
LIKE 'RESTORE DATABASE%'
) F2 ON (F1.DatabaseID = F2.DatabaseID OR F2.DatabaseID IS NULL)
AND F1.SPID = F2.SPID
AND F1.ClientProcessID = F2.ClientProcessID
AND F1.StartTime > F2.StartTime
GROUP BY F2.EventSequence
) F4 ON F3.TransactionID = F4.TransactionID
GROUP BY F3.TransactionID, F4.EventSequence
) F6 ON F5.EventSequence = F6.MainSequence
OR F5.EventSequence = F6.MaxEventSequence
ORDER BY F5.StartTime

关于sql-server - 如何查询SQL Server数据库恢复需要多长时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3776783/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com