Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1099407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:43:56+00:00 2026-05-17T00:43:56+00:00

Im trying to write a query that will tell me how much time a

  • 0

Im trying to write a query that will tell me how much time a restore (full or log) has taken on SQL server 2008.

I can run this query to find out how much time the backup took:

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

This query will tell me what is restored but now how much time it took:

select * from msdb..restorehistory

restorehistory has a column backup_set_id which will link to msdb..backupset, but that hold the start and end date for the backup not the restore.

Any idea where to query the start and end time for restores?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:43:57+00:00Added an answer on May 17, 2026 at 12:43 am

    To find the RESTORE DATABASE time, I have found that you can use this query:

    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; 
    

    The downside is, you’ll notice that, at least on my test server, the EndTime is always NULL.

    So, I came up with a second query to try and determine the end time. First of all, I apologize that this is pretty ugly and nested like crazy.

    The query below assumes the following:

    1. When a restore is run, for that DatabaseID and ClientProcessID, the next EventSequence contains the TransactionID we need.
    2. I then go and find the max EventSequence for the Transaction
    3. Finally, I select the record that contains RESTORE DATABASE and the maximum transaction associated with that record.

    I’m sure someone can probably take what I’ve done and refine it, but this appears to work on my test environment:

    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
    

    EDIT

    I made some changes to the query, since one of the test databases I used is case-sensitive and it was losing some records. I also noticed when restoring from disk that the DatabaseID is null, so I’m handling that now as well:

    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 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a query that will pull back the two most recent
I am trying to write a query for SQL Server 2005 but I can't
I am trying to write a query that will search for orders that contain
I'm trying to write a query that will return ... Each distinct user id
I am trying to write a query that will capture calls completed within 24
I'm trying to write a query that will display the minimum value (lowest score)
I'm trying to write a query that will return all QUERY_ID values alongside all
I'm trying to write a MySQL query (from PHP) that will find the newest
I am trying to write a Java application that will query the WMI on
I'm trying to write a query that extracts and transforms data from a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.