By using this site, you agree to the Privacy Policy and Terms of Use.
Forums › SQL Server DBA Discussion › SQL Server Scripts › SQL Query for Monitoring Backup and Restore Progress
The SQL query you’ve provided is designed to track the progress of backup and restore operations in SQL Server. It’s particularly useful for database administrators who need to monitor the status of these long-running operations. Here’s an explanation of what this query does:
This SQL query is used for monitoring the progress of backup and restore operations in SQL Server:
SELECT
session_id as SPID,
command,
a.text AS Query,
start_time,
percent_complete,
dateadd(second, estimated_completion_time/1000, getdate()) as estimated_completion_time
FROM
sys.dm_exec_requests r
CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) a
WHERE
r.command in ('BACKUP DATABASE', 'RESTORE DATABASE')
session_id as SPID
: Identifies the session ID of the process running the backup or restore operation. ‘SPID’ is the common term used for session ID in SQL Server.command
: Displays the type of command being executed, specifically if it’s a ‘BACKUP DATABASE’ or ‘RESTORE DATABASE’ operation.a.text AS Query
: Shows the actual SQL query being executed. This is retrieved by applying the sys.dm_exec_sql_text
dynamic management function to the sql_handle
of the executing command.start_time
: Indicates when the backup or restore operation started.percent_complete
: Shows the progress of the backup or restore operation as a percentage. This is particularly useful to estimate how much of the operation is completed and how much is remaining.dateadd(second, estimated_completion_time/1000, getdate()) as estimated_completion_time
: Calculates the estimated completion time of the operation. estimated_completion_time
is given in milliseconds, so it’s divided by 1000 to convert it to seconds, and then added to the current time (getdate()
).Execute this query in SQL Server Management Studio (SSMS) or any other SQL Server query tool. It’s especially useful to run this query during larger backup or restore operations to keep track of their progress.
Remember, this query only provides information for active backup and restore operations. Once an operation completes, it will no longer appear in the results of this query.