Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to list the SQL Server Databases, Size and Utilisation by Specific Application or Service

I am not sure if its possible, but would really appreciate any assistance.

I am looking for a script which can generate the complete list of SQL Server databases with the following details for each database of a SQL Server instance:

  • Name
  • Sizes
  • Utilisation by specific application or service
  • Hardware Utilisation (CPU, memory, I/O, etc.)
like image 733
muddu83 Avatar asked May 29 '12 09:05

muddu83


3 Answers

Using EXEC sp_databases may show the wrong sizes for the DB. Here's a nice, reliable query that will give all database names, sizes and statuses, although not which apps are utilising the DBs:

SELECT
    D.name,
    F.Name AS FileType,
    F.physical_name AS PhysicalFile,
    F.state_desc AS OnlineStatus,
    CAST((F.size*8)/1024 AS VARCHAR(26)) + ' MB' AS FileSize,
    CAST(F.size*8 AS VARCHAR(32)) + ' Bytes' as SizeInBytes
FROM 
    sys.master_files F
    INNER JOIN sys.databases D ON D.database_id = F.database_id
ORDER BY
    D.name
like image 114
Chris Halcrow Avatar answered Sep 19 '22 16:09

Chris Halcrow


To get only general information (database name, files and size) you can have some success running the "sp_databases" stored procedure:

exec sp_databases

If the above didn't work in SQL Server 2000, you can try the following:

select *
from sys.sysdatabases

But you can get a more detailed trace and audit data using the "SQL Profiler" that is shipped with SQLServer.

like image 23
jfoliveira Avatar answered Sep 19 '22 16:09

jfoliveira


You could use Nagios for your various monitoring tasks. Nagios provides complete monitoring of MSSQL - including availability, database and table sizes, cache ratios, and other key metrics.

like image 43
David Brabant Avatar answered Sep 18 '22 16:09

David Brabant