Get row count of table – SQL Server

There are times when as a DBA or developer you want to get the count of a table. To get the current count you can simply do:
SELECT COUNT(*) FROM [table_name]

Which works fine, when tables are small or medium size, but for large table (10 millions rows or above) this can take a while to return. If the count is for getting a “close enough” number, then sys.partitions can be used for getting this count quickly. sys.partitions keeps track of indexes for each table and rows contain within each index. The row count may be little off, depending on when the query is executed (pending commits etc), but for a “good enough” number count using sys.partitions is a faster option.

SELECT OBJECT_NAME(object_id), rows FROM sys.partitions WHERE index_id = 1

OBJECT_NAME will return the table name and index_id = 1 will return single row for tables with multiple indexes.

Where are the backup files? – SQL Server

In the previous post we explored backupset table in msdb, which holds data about backups, backup times etc. So now that we have good backups, where are the backup files? We can query backupmediafamily table for this information. physical_device_name column holds the full path (if saved on disk else the media name) to backup file.

SELECT * FROM msdb.dbo.backupmediafamily

We can also join backupmediafamily to backupset on media_set_id column to get information about relevant backup files.

SELECT physical_device_name, bs.backup_start_date, bs.backup_finish_date FROM msdb.dbo.backupmediafamily bf
INNER JOIN msdb.dbo.backupset bs ON bf.media_set_id = bs.media_set_id
WHERE 1=1
AND bf.media_set_id = 18518

Get information about backups – SQL Server

backupset table in msdb can be used to get information about database backups. This table holds information about of type of backup being done, recovery_model, backup_size, backup_start_date, backup_finish_date etc.

Full backups are of type ‘D’ (Database Backup), log backups are type ‘L’ (Log Backup) and incremental are of type ‘I’ (Incremental Backup).

To get full backup details sorted by backup finish time:
SELECT * FROM msdb.dbo.backupset
WHERE 1=1
AND type = 'D'
ORDER BY backup_finish_date DESC

Results can be filtered based on database by including database name like this:
SELECT * FROM msdb.dbo.backupset
WHERE 1=1
AND type = 'D'
AND database_name = ''
ORDER BY backup_finish_date DESC

Get Status of SQL Agent Jobs – SQL Server

There are couple of tables (sysjobs, sysjobhistory) in msdb database which hold information about SQL Agent jobs.
sysjobs table contains: name, enabled, notify information, date_created… etc
sysjobhistory table contains:run_status, run_date, run_time, server… etc

Script below will return jobName, message, enabled, run_status, run_date. Run_status: 0=Failed, 1=Succeeded, 2=Retry, Canceled=3, Steps within jobs=4

SELECT DISTINCT SERVERPROPERTY('ServerName') AS [ServerName],
j.name AS jobName, jh.message, j.enabled, jh.run_status, jh.run_date FROM msdb.dbo.sysjobs j
INNER join msdb.dbo.sysjobhistory jh ON j.job_id = jh.job_id
WHERE 1=1
AND enabled = 1