Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

The Automated DBA: Space Usage Snapshotter (sysadmin; central DB)

By Jesse Roberge,

*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
Util_BuildSpaceLog
By Jesse Roberge - YeshuaAgapao@Yahoo.com

The SpaceLog builder
Records database space usage and buffer cache usage statistics into the SpaceLog_* tables.
Keeps a record of database and table growth for reporting of growth trends.
Stats include Reserved, Used, Data, and buffer cache page counts for row, lob (VarChar-Max, text etc), and overflow data (data rows with varchar columns going over 8000 bytes).
Stats also include some computed columns such as BTree (Used-Data), unused (Reserved-Used), and sums of row+lob+overflow for each of reserved, used, data, and buffer.
The database, dataspace, schema, and table levels give stats for the nonclustered index and the table itself (heap or clustered index).
Designed to run and record to a central 'admin' database location.
Requires VIEW_SERVER_STATE due to the querying of sys.dm_os_buffer_descriptors for buffer cache usage stats. DB-owner does not have this permission.
Sysadmin does have this permission. VIEW_SERVER_STATE can be granted as a separate permission to some or all dbo users.

Required Input Parameters
None

Optional Input Parameters
@GatherDatabaseName sysname='', Name of the database to gather the space usage stats from. If omitted, the name will be looked up from the ID. If both ID and name are omitted, the ID/Name of the current database is used.
@GatherDatabaseID int=0, ID of the database to gather the space usage stats from. If omitted, the ID will be looked up from the name. If both ID and name are provided, the ID takes precedence.
@RecordDatabaseName sysname='', Name of the database to record the space usage stats to. If omitted, the name will be looked up from the ID. If both ID and name are omitted, the ID/Name of the current database is used.
@RecordDatabaseID int=0, ID of the database to record the space usage stats to. If omitted, the ID will be looked up from the name. If both ID and name are provided, the ID takes precedence.
@RecordSchemaName sysname='', Name of the schema to record the space usage stats to. If omitted, the name will be looked up from the ID. If both ID and name are omitted, the 'dbo' (ID=1) schema is used.
@RecordSchemaID int=0, ID of the schema to record the space usage stats to. If omitted, the ID will be looked up from the name. If both ID and name are provided, the ID takes precedence.
@UpdateUsage tinyint=0 Default and recommended to be off. Use only if you must have the most accurate and up to date numbers. Will run DBCC UpdateUsage to scan every table in the database to re-count all allocations and rows, which can hog disk IO for serveral hours.

Usage
EXECUTE Admin.Util_BuildSpaceLog
@GatherDatabaseName='Baseball',
@RecordDatabaseName='Admin',
@RecordSchemaName='Admin',

EXECUTE Admin.Util_BuildSpaceLog
@GatherDatabaseID=10,
@RecordDatabaseID=7,
@RecordSchemaID=2,
@UpdateUsage=1

DECLARE @SQL nVarChar(4000)
SET @SQL=''
SELECT @SQL=@SQL+'EXECUTE Admin.Util_BuildSpaceLog @GatherDatabaseName=''' + name + ''', @RecordDatabaseName=''Admin'', @RecordSchemaName=''Admin''' + CHAR(13)+CHAR(10)
FROM sys.databases
WHERE database_id<>2
EXECUTE (@SQL)
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

Total article views: 2671 | Views in the last 30 days: 4
 
Related Articles
FORUM

Limit buffer pool memory amount by database

Limit buffer pool memory amount by database

FORUM

ADMIN Database

The concept of admin dataase

FORUM

Who is responsible for databases: DBA or Admins

System Admins want authority to backup, restore databases

FORUM

Insufficient Buffer Space issues

Applications lose connectivity to sql server database beyond certain connections

SCRIPT

The Automated DBA: Space Usage Snapshotter Lite (non-sysadmin dbo)

Records snapshots of space usage stats by partition, index, table, schema, filegroup, and database. ...

 
Contribute