日常数据维护中容量规划是每个DBA的基础工作之一,也是非常重要的工作。在生产环境中一些比较重要的业务数据库会用前期容量规划不足,或出现意外的爆发式的数据增长,直至盛满整个磁盘空间,系统会无法使用,最终业务受到影响。这时候,如果没有及时的磁盘预警的情况下会更糟糕,最终带来经济损失。那么,为了避免这种情况发生,我们可以做到提前预警,主动搜集相关数据文件的大小,例如数据库、数据文件、备份文件大小,以及主要业务数据表大小等,定期追踪并已报告形式给到存储团队,提醒容量的变化。详细的代码如下 :
1.检查数据库文件大小
SELECT convert(varchar(10),getdate(),120) as 'Date',d.name as 'Database Name',sum(CONVERT(decimal(9,3),convert(float,l.size) * (8192.0/1024.0/1024.)/1024.)) as 'Size(GB)'
FROM sys.master_files l
LEFT JOIN sys.databases d ON l.database_id=d.database_id
WHERE d.database_id>4--排除系统数据库
group by d.name
order by 'Size(GB)' desc
--或者...
---MDF+LDF
SELECT DISTINCT
--a.database_id,
a.name AS dbName,
a.compatibility_level,
CONVERT(VARCHAR(25), a.create_date, 106) AS dbCreateDate,
CONVERT(VARCHAR(25), CAST(CAST(SUM(b.size * 8.0 / 1024.0/1024.) AS NUMERIC(10,2)) AS money), 1) AS 'Size(GB)',
DATABASEPROPERTYEX(a.name, 'Recovery') AS recoveryMode,
DATABASEPROPERTYEX(a.name, 'Status') AS databaseStatus,
CASE DATABASEPROPERTYEX(a.name, 'IsAutoCreateStatistics') WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS autoCreateStatsEnabled,
CASE DATABASEPROPERTYEX(a.name, 'IsAutoUpdateStatistics') WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS autoUpdateStatsEnabled,
DATABASEPROPERTYEX(a.name, 'UserAccess') AS userAccess,
DATABASEPROPERTYEX(a.name, 'Updateability') AS Updateability,
CASE DATABASEPROPERTYEX(a.name, 'IsTornPageDetectionEnabled') WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS tornPageDetectionEnabled,
CASE DATABASEPROPERTYEX(a.name, 'IsRecursiveTriggersEnabled') WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS recursiveTriggersEnabled,
CASE DATABASEPROPERTYEX(a.name, 'IsInStandBy') WHEN 1 THEN 'TRUE' ELSE 'FALSE' END AS isDBStandBy
FROM
master.sys.databases a INNER JOIN master.sys.master_files b ON a.database_id = b.database_id
where a.database_id>4
GROUP BY
a.database_id,
a.name,
a.compatibility_level,
CONVERT(VARCHAR(25), a.create_date, 106)
ORDER BY 1
2.检查数据库备份文件大小
SELECT getdate() as,
b.server_name, Round(SUM(convert(float,b.backup_size) /1024.0/1024.0/1024.0),2) AS 'backup_size_GB',
Round(SUM(convert(float,b.compressed_backup_size)/1024.0/1024.0/1024.0),2) AS 'compressed_backup_size_GB' FROM msdb..backupset b
where b.database_name not in ('model','master','msdb','')
--and b.type='D'
AND backup_start_date>getdate()-1
GROUP BY b.server_name
3.检查表空间大小
SELECT OBJECT_NAME(id) tablename ,
CASE WHEN reserved * 8 > 1024 THEN RTRIM(8 * reserved / 1024) + 'MB'
ELSE RTRIM(reserved * 8) + 'KB'
END DataReserve ,
CASE WHEN dpages * 8 > 1024 THEN RTRIM(8 * dpages / 1024) + 'MB'
ELSE RTRIM(dpages * 8) + 'KB'
END Used ,
CASE WHEN 8 * ( reserved - dpages ) > 1024
THEN RTRIM(8 * ( reserved - dpages ) / 1024) + 'MB'
ELSE RTRIM(8 * ( reserved - dpages )) + 'KB'
END unused ,
CASE WHEN ( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 ) > 1024
THEN RTRIM(( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 )
/ 1024) + 'MB'
ELSE RTRIM(( 8 * dpages / 1024 - rows / 1024 * minlen / 1024 ))
+ 'KB'
END FREE ,
rows AS Rows_Count
FROM sys.sysindexes
WHERE indid = 1
AND status = 2066 -- status='18'
ORDER BY reserved DESC
4.检查表索引大小
--特别提醒:此查询较慢,如果是生产环境请选择非业务时间执行
IF OBJECT_ID('tempdb..#Indexdata', 'U') IS NOT NULL
DROP TABLE #Indexdata
DECLARE
@SizeofIndex BIGINT, @IndexID INT,
@NameOfIndex nvarchar(200),@TypeOfIndex nvarchar(50),
@ObjectID INT,@IsPrimaryKey INT,
@FGroup VARCHAR(20)
create table #Indexdata (name nvarchar(50),
IndexID int, IndexName nvarchar(200),
SizeOfIndex int, IndexType nvarchar(50),
IsPrimaryKey INT,FGroup VARCHAR(20))
DECLARE Indexloop CURSOR FOR
SELECT idx.object_id, idx.index_id, idx.name, idx.type_desc
,idx.is_primary_key,fg.name
FROM sys.indexes idx
join sys.objects so
on idx.object_id = so.object_id JOIN sys.filegroups fg
ON idx.data_space_id = fg.data_space_id
where idx.type_desc != 'Heap'
and so.type_desc not in ('INTERNAL_TABLE','SYSTEM_TABLE')
AND idx.name in(
select
i.name
FROM sys.dm_db_index_usage_stats AS ius
JOIN sys.indexes AS i ON i.index_id = ius.index_id
AND i.object_id = ius.object_id
WHERE ius.database_id = DB_ID() --and i.name like '%ClusteredIndex%'
--and OBJECT_NAME(i.object_id) like'%DAILYSALES'
AND i.is_disabled = 0
)
OPEN Indexloop
FETCH NEXT FROM Indexloop
INTO @ObjectID, @IndexID, @NameOfIndex,
@TypeOfIndex,@IsPrimaryKey,@FGroup
WHILE (@@FETCH_STATUS = 0)
BEGIN
SELECT @SizeofIndex = sum(avg_record_size_in_bytes * record_count)
FROM sys.dm_db_index_physical_stats(DB_ID(),@ObjectID,
@IndexID, NULL, 'detailed')
insert into #Indexdata(name, IndexID, IndexName, SizeOfIndex,
IndexType,IsPrimaryKey,FGroup)
SELECT TableName = OBJECT_NAME(@ObjectID),
IndexID = @IndexID,
IndexName = @NameOfIndex,
SizeOfIndex = CONVERT(DECIMAL(16,1),(@SizeofIndex/(1024.0 * 1024))),
IndexType = @TypeOfIndex,
IsPrimaryKey = @IsPrimaryKey,
FGroup = @FGroup
FETCH NEXT FROM Indexloop
INTO @ObjectID, @IndexID, @NameOfIndex,
@TypeOfIndex,@IsPrimaryKey,@FGroup
END
CLOSE Indexloop
DEALLOCATE Indexloop
select name as TableName, IndexName, IndexType,
SizeOfIndex AS [Size of index(MB)],
case when IsPrimaryKey = 1 then 'Yes' else 'No' End as [IsPrimaryKey]
,FGroup AS [File Group]
from #Indexdata order by SizeOfIndex DESC
-----------------------------------------------------------------------------------------
SameZhao