*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=
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)
*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=