When Was SQL Server Last Restarted? – Interview Question of the Week #225

Question: When was SQL Server last restarted? For the Database Engine, read sqlserver_start_time from sys.dm_os_sys_info.

A newly lit pendant lamp illuminates an extinguished candle

This is one of my first questions during a Comprehensive Database Performance Health Check. Many useful DMVs lose accumulated history when the engine restarts. A quiet counter just after startup tells a different story from the same counter after a week of work.

SELECT sqlserver_start_time
FROM sys.dm_os_sys_info;

SELECT create_date AS tempdb_create_date
FROM sys.databases
WHERE name = N'tempdb';

EXEC sys.sp_readerrorlog 0, 1, N'SQL Server is starting';

The first query reports the local date and time the current Database Engine started. It does not report Windows boot time or SQL Server Agent’s start time.

Tempdb is recreated during engine startup, so its creation time is another clue. I’ve observed a small difference between that timestamp and the DMV timestamp because they identify different points in startup. I prefer the DMV for the engine’s actual start time.

The third command searches the current SQL Server error log. If the log has cycled since startup, the relevant entry can be in an archived log. Change the log number only after identifying the appropriate archive; an empty search in log 0 doesn’t prove the engine never restarted.

I prefer to investigate an instance that has run long enough to collect representative workload history. Frequent restarts make that harder. Save diagnostic history outside transient DMVs if you need to compare activity across restarts.

Permissions differ by method. The engine DMV requires VIEW SERVER PERFORMANCE STATE on SQL Server 2022 and later, and VIEW SERVER STATE on earlier versions. Error-log access has its own version-dependent permission requirement.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

SQL DMV, SQL Scripts, SQL Server, SQL TempDB, Starting SQL
Previous Post
How to Execute Query Without Using F5 or Query Toolbar in SSMS? – Interview Question of the Week #224
Next Post
When was Table Accessed Last By User? – Interview Question of the Week #226

Related Posts

2 Comments. Leave new

  • Jorge Flores
    June 1, 2019 4:37 am

    I use this one too:

    SELECT @@SERVERNAME as Instance, login_time AS ‘Started’,
    DATEDIFF(DAY, login_time, CURRENT_TIMESTAMP)
    AS ‘Uptime in days’
    FROM
    sys.sysprocesses
    WHERE spid = 1;
    Go

    Reply
  • maheswarareddy
    July 6, 2019 5:16 pm

    what is the use index seek in how does the architecture can be defined

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.