Using one SQL Server instance for many projects is not unusual. However, defining the most active project may be quite difficult. Today, I would like to share several ways to analyze the activity of each particular database.
In this article, we are going to consider the following points:
- The number of database connections
- Disk space
- RAM capacity
- The activity of the database files for a particular period of time
The number of database connections
To define the number of connections, use master.dbo.sysprocesses
SELECT DB_NAME(p.dbid) db, COUNT(*) quantity FROM master.dbo.sysprocesses p WHERE p.spid > 50 group by DB_NAME(p.dbid) ORDER BY 1
Note that the number of connections does not display the database activity and load. Connections can be either idle or active.
CREATE TABLE #sizingDB (dbname nvarchar(255), type_desc nvarchar(50), size_mb bigint) INSERT INTO #sizingDB exec sp_msforeachdb @command1 = 'use [?]; SELECT DB_NAME(),type_desc, SUM(size)*8/1024 as size FROM sys.database_files GROUP BY type_desc' SELECT * FROM #sizingDB WHERE dbname NOT IN ('master','msdb','model') ORDER BY dbname, type_desc DESC DROP TABLE #sizingDB
The query returns two rows for each database. The first row is data size and the second one is a transaction log.
The sum of data files and logs returns only one row for each database.
select db_name(dbid), sum(cast(size as bigint)) * 8 / 1024 as SizeGB, sum(case when f.groupid = 0 then 0 else cast(size as bigint) end) * 8 / 1024 as DataSizeMb, sum(case when f.groupid != 0 then 0 else cast(size as bigint) end) * 8 / 1024 as LogSizeMb from master.sys.sysaltfiles as f group by db_name(dbid) order by SizeGB desc
WITH AggregateBufferPoolUsage AS (SELECT DB_NAME(database_id) AS [Database Name], CAST(COUNT(*) * 8/1024.0 AS DECIMAL (10,2)) AS [CachedSize] FROM sys.dm_os_buffer_descriptors WITH (NOLOCK) WHERE database_id > 4 -- system databases AND database_id <> 32767 -- ResourceDB GROUP BY DB_NAME(database_id)) SELECT ROW_NUMBER() OVER(ORDER BY CachedSize DESC) AS [Buffer Pool Rank], [Database Name], CachedSize AS [Cached Size (MB)], CAST(CachedSize / SUM(CachedSize) OVER() * 100.0 AS DECIMAL(5,2)) AS [Buffer Pool Percent] FROM AggregateBufferPoolUsage ORDER BY [Buffer Pool Rank];
The Buffer Pool Percent column reflects the percentage of memory consumption of the total capacity.
The activity of the database files for a particular period of time
SELECT DB_NAME(saf.dbid) AS [db], saf.name AS [name], vfs.BytesRead/1048576 AS [read], vfs.BytesWritten/1048576 AS [write] INTO #dbusage FROM master..sysaltfiles AS saf JOIN ::fn_virtualfilestats(NULL,NULL) AS vfs ON vfs.dbid = saf.dbid AND vfs.fileid = saf.fileid AND saf.dbid NOT IN (1,3,4) WHERE DB_NAME(saf.dbid) <> 'tempdb' ORDER BY vfs.BytesRead/1048576 + BytesWritten/1048576 DESC WAITFOR DELAY '00:01:00' SELECT DB_NAME(saf.dbid) AS [db], saf.name AS [name], vfs.BytesRead/1048576 AS [read], vfs.BytesWritten/1048576 AS [write] INTO #dbusage2 FROM master..sysaltfiles AS saf JOIN ::fn_virtualfilestats(NULL,NULL) AS vfs ON vfs.dbid = saf.dbid AND vfs.fileid = saf.fileid AND saf.dbid NOT IN (1,3,4) WHERE DB_NAME(saf.dbid) <> 'tempdb' ORDER BY vfs.BytesRead/1048576 + BytesWritten/1048576 DESC SELECT t.db,t.name,(t2.[read] - t.[read]) as tread,(t2.[write] - t.[write]) as [twrite] FROM #dbusage t INNER JOIN #dbusage2 t2 on t.db= t2.db AND t.name=t2.name DROP TABLE #dbusage DROP TABLE #dbusage2
The script will collect information per minute by default. If you need a report for a longer period of time, change WAITFOR DELAY ’00:01:00′.
The report returns information for each database file.
Even if you have many projects on one SQL Server instance, you can still get enough information about each of them. Of course, if the project is important and requires special conditions to access, I strongly recommend putting it into a separate instance, as we can not see and implement everything within one instance and many projects.
Latest posts by Dmitry Zaytsev (see all)
- How to Analyze Activity of One Database in SQL Server - November 13, 2017
- Why Multiple JOINs are bad for Query or Do Not Get in the Way of Optimizer - June 20, 2017