Thursday, October 17, 2019

When a database was last accessed?

When a database was last accessed?

You might say: "- What the silly question.... I'm accessing it right now!"
And you would be absolutely right if your database is online, but what if it is offline?

How to figure out when database got offline?

One way id to try to bring you database in question online and then look at the objects/indexes/statistics etc. and you might figure it out.
However there is much easier method without bringing database online.
Just use CMD or PowerShell.

At first, you need to figure out location of your database's log file:
SELECT physical_name
FROM master.sys.master_files
WHERE type = 1 and database_id = DB_ID('sysdb');

GO

Then you can run a CMD commands like these to get Last Write and Last Access times:
dir N:\Logs\sysdb_log.ldf /TW
dir N:\Logs\sysdb_log.ldf /TA

Or you can use simple PowerShell command to get the same info, but with precision to the seconds.
ls N:\Logs\sysdb_log.ldf | Format-table -Property LastWriteTime, LastAccessTime

In my case result was horrible!

The database was sitting offline for more than a year and nobody paid attention to it.

Isn't t easy?

No comments:

Post a Comment