28

I saw a similar question which asked how to monitor the progress of a backup/restore operation: Is there a SQL script that I can use to determine the progress of a SQL Server backup or restore process?

I would like to know if there's a similar query/way to see how much time the query has left until it will end. For example, one query usually has an elapsed time of 5 minutes. I would like to know how much time is left until it will end DURING the query's execution.

1
  • Hi Roni, to clarify, are you looking to monitor the progress of a query that is performing a backup / restore? Or a query in general? Jan 3, 2013 at 11:47

4 Answers 4

29

What you want are Live Query Statistics.

You can activate it in the most recent version of SSMS with a button next to the one that gives you the normal query plan:

enter image description here

This then gives you a live query plan:

enter image description here

At the bottom you see the total progress:

enter image description here

1
  • 4
    I feel like this may negatively impact query performance somehow. I mean - why would it otherwise be impossible to obtain the progress information without enabling live query plans? EDIT: Turns out there's a big fat warning label on the live query stats documentation. Indeed, it does negatively impact performance. Use with caution!
    – Dan
    Nov 22, 2018 at 19:06
20

There is no way to know how much time is left. A query's runtime depends on many things beyond the actual query itself: locking/blocking of other queries, other processes consuming resources (CPU/disk usage), the operating system, network, etc. What if your 5-minute query is running, yet someone else kicks off a large report, your query may run 5:30 now. What if the someone starts to download a large file and hogs all the network bandwidth? What if the OS decides to do something in the background, etc. Until all the rows are returned, the query isn't done, but it can run in a variable time frame.

5
  • Indeed. On some (badly configured/overloaded) systems, a single intensive query can lock a crucial table - and all of a sudden, queries that usually finish under a second run for tens of minutes. You could watch the query times and provide a guess - "on average, this query completes in 5:41, it's been running for 3:15 now", but it's just that: a guess (not even an estimate). Jul 20, 2010 at 12:14
  • 1
    I've seen 3D rendering screen savers running on production database boxes!
    – KM.
    Jul 20, 2010 at 12:16
  • In Oracle for example, we have the view v$session_longops, which displays the status of operations. download.oracle.com/docs/cd/B19306_01/server.102/b14237/… I you sure there's no similar thing in SQL SERVER ? Roni.
    – Roni Vered
    Jul 20, 2010 at 12:22
  • @Roni Vered, there are numerous ways to get info from SQL Server about running queries, but no "TIME_RAMINING" value. You may be able to calculate an average value based on previous runs, see: stackoverflow.com/questions/617170/…
    – KM.
    Jul 20, 2010 at 13:11
  • 3
    The only people that don't already expect time estimates to fluctuate based on server load are very new to computers indeed. Accepting that a time estimate is an estimate then try SELECT percent_complete FROM sys.dm_exec_requests, which can then be used to estimate how much time the query should have left to complete. Aug 30, 2018 at 13:13
20

sys.dm_exec_requests has that info, so something like that will give you the progress:

SELECT 
percent_complete
FROM sys.dm_exec_requests
--where session_id=51 or command like 'restore%'
2
  • 4
    This query is only useful if you have VIEW SERVER STATE permission. If you do not you will only see the results for the SELECT statement you just submitted, which is a bit useless! To establish if you have permissions run the following "SELECT * FROM fn_my_permissions(NULL, 'SERVER');" - If you don't see an entry with permission_name 'VIEW SERVER STATE' then you don't have the permission.
    – samaspin
    Mar 13, 2013 at 12:25
  • 11
    Also, this information is provided only when specific commands are being executed. These are for example ROLLBACK, RECOVERY or BACKUP DATABASE.
    – user35443
    Nov 9, 2013 at 17:11
6

Yes you can know the estimated elapsed time unless there would be some unexpected situation affecting the execution of the process.

Select total_elapsed_time,
 * from sys.dm_exec_sessions where session_id="your Id here" 
2
  • sys.dm_exec_sessions.total_elapsed_time tells you the time since a session was established. This has nothing to do with the (estimated) remaining time of a SQL statement, which was what the OP asked about. Aug 23, 2022 at 0:06
  • iif(req.percent_complete>0, (((100/req.percent_complete)-1)*(req.total_elapsed_time/1000))/60/60,0) HoursLeft
    – IvoTops
    Aug 15 at 8:36

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.