有什么方法可以跟踪 SQL Server 2008 数据库大小随时间的增长情况?我可以从仪表板的某个地方看到这些信息吗?还是我必须编写查询脚本才能执行此操作?
谢谢
答案1
我认为跟踪数据库大小增长的最佳方法是每周或按照您想要的频率收集其大小并将其插入到另一个 SQL 实例(不同于所监视的数据库的实例)的静态表中。
您可以使用下面的查询,以兆字节为单位返回数据和日志文件大小,并将数据插入另一个 tempdb 表。一个月或一年后,您将能够通过分析该表的输出来检查增长情况,甚至可以创建图表。
CREATE TABLE tempdb..DB_size_growth
(dbname NVARCHAR(256),
mb_data_file NUMERIC(12,2),
data_file_nr INT ,
mb_log_file NUMERIC(12,2),
log_file_nr INT)
DECLARE @dbname AS NVARCHAR(3000)
DECLARE @exec AS NVARCHAR(3999)
DECLARE DB_NAME CURSOR FOR
SELECT name FROM master.dbo.sysdatabases where has_dbaccess(name) = 1
OPEN DB_NAME
FETCH NEXT FROM DB_NAME INTO @dbname
TRUNCATE TABLE tempdb..DB_size_growth
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @exec = 'INSERT INTO tempdb..DB_size_growth (dbname, mb_data_file, data_file_nr, mb_log_file, log_file_nr)
SELECT b.name AS dbname
,(SELECT convert(numeric(12,3),convert(numeric(12,2),((sum(a1.size))*8))/1024)
from ' + QUOTENAME(@dbname, '[') + '.dbo.sysfiles as a1 where a1.groupid <> 0 ) as mb_data_file
,(SELECT count(a1.size) from ' + QUOTENAME(@dbname, '[') + '.dbo.sysfiles as a1 where a1.groupid <> 0 ) as data_file_nr
,(SELECT convert(numeric(12,3),convert(numeric(12,2),((sum(a.size))*8))/1024)
from ' + QUOTENAME(@dbname, '[') + '.dbo.sysfiles as a where a.groupid = 0 ) as mb_log_file
,(SELECT count(a.size) from ' + QUOTENAME(@dbname, '[') + '.dbo.sysfiles as a where a.groupid = 0 ) as log_file_nr
FROM master.dbo.sysdatabases as b WHERE name = '''+ @dbname +'''
group by b.name'
EXEC (@exec)
FETCH NEXT FROM DB_NAME INTO @dbname
END
CLOSE DB_NAME
DEALLOCATE DB_NAME
select * from tempdb..DB_size_growth
答案2
仪表板中的信息是通过读取默认跟踪获得的。您可以按照说明读取默认数据库这里