博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL SERVER 监控数据文件增长情况
阅读量:5864 次
发布时间:2019-06-19

本文共 14580 字,大约阅读时间需要 48 分钟。

原文:

     在项目前期评估数据库的增长情况,然后根据数据库数据量的增长情况来规划存储的分配其实是一件比较麻烦的事情。因为项目没有上线,用什么来评估数据库的数据增长情况呢? 如果手头没有实际的数据,我们只能从表的数量以及预计一天的数据增长情况来预估数据增长量。当然这里猜测的成分较大。这个是非常不靠谱,也是不准确的。当然我们可以监控测试环境的数据库大小的增长情况来评估数据增长情况。我们可以监控数据库大小的变化来估计生产环境的数据增长情况。当然生产环境和测试环境的区别还是蛮大的。但是这样比那种瞎猜式的还是要靠谱得多。

    在项目中期,我们在管理、维护数据库当中,也是需要监控数据库的增长情况的。这样有利于我们了解系统的数据变化情况,利于长期的存储规划,也能提前发现一些异常情况,及时调整数据库数据文件的增长设置。总之来说,监控数据文件的增长情况是有必要的。数据库管理、维护也是需要大数据和BI分析的吗。这个也是一个趋势。

   为了监控数据库的数据文件增长情况,我写了一个存储过程用来获取数据库数据文件的一些详细信息。然后可以按天、按周、按月份这三种频率采集数据(具体可以根据需要来采集数据)存放在日表、周表、月表。需要时,即可拿来做一下分析。

基础表Maint.DataBaseSizeDtl_Day,Maint.DataBaseSizeDtl_Week,Maint.DataBaseSizeDtl_Month

USE YourSQLDba;
GO
 
IF EXISTS(SELECT 1 FROM sys.objects WHERE type='U' AND object_id=OBJECT_ID('Maint.DataBaseSizeDtl_Day'))
BEGIN
    DROP TABLE Maint.DataBaseSizeDtl_Day;
END
GO
 
 
CREATE TABLE Maint.DataBaseSizeDtl_Day
(
     DateCD                DATETIME
    ,DataBaseId            INT 
    ,FileId                INT
    ,DataBaseName        NVARCHAR(256)
    ,LogicalName        NVARCHAR(256)
    ,FileTypeDesc        NVARCHAR(120)
    ,PhysicalName        NVARCHAR(520)
    ,StateDesc            NVARCHAR(120)
    ,MaxSize            NVARCHAR(32)
    ,IsPercentGrwoth    BIT
    ,Growth                NVARCHAR(24)
    ,IsReadOnly            BIT
    ,DataBaseSize        FLOAT
    CONSTRAINT PK_DataBaseSizeDtl_Day PRIMARY KEY(DateCD, DataBaseId,FileId)
);
 
 
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Database Size Detail Records every day', @level0type=N'SCHEMA', @level0name=N'Maint', @level1type=N'TABLE', @level1name=N'DataBaseSizeDtl_Day';
 
 
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'采集数据库记录的时间',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'DateCD';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'the datebase''s identity number',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'DataBaseId';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'the datebase file''s identity number',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'FileId';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库名称',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'DataBaseName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库逻辑名称',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'LogicalName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件类型',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'FileTypeDesc';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库物理文件',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'PhysicalName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库状态',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'StateDesc';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件最大值',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'MaxSize';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件是否按百分比增长',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'IsPercentGrwoth';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件是否自动增长',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'Growth';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库是否只读',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'IsReadOnly';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件大小',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Day', @level2type=N'COLUMN',@level2name=N'DataBaseSize';
 
 
 
IF EXISTS(SELECT 1 FROM sys.objects WHERE type='U' AND object_id=OBJECT_ID('Maint.DataBaseSizeDtl_Week'))
BEGIN
    DROP TABLE Maint.DataBaseSizeDtl_Week;
END
GO
 
CREATE TABLE Maint.DataBaseSizeDtl_Week
(
     DateCD                DATETIME
    ,WeekCD                INT
    ,DataBaseId            INT 
    ,FileId                INT
    ,DataBaseName        NVARCHAR(256)
    ,LogicalName        NVARCHAR(256)
    ,FileTypeDesc        NVARCHAR(120)
    ,PhysicalName        NVARCHAR(520)
    ,StateDesc            NVARCHAR(120)
    ,MaxSize            NVARCHAR(32)
    ,IsPercentGrwoth    BIT
    ,Growth                NVARCHAR(24)
    ,IsReadOnly            BIT
    ,DataBaseSize        FLOAT
    CONSTRAINT PK_DataBaseSizeDtl_Week PRIMARY KEY(WeekCD,DateCD, DataBaseId,FileId)
);
GO
 
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Database Size Detail Records every week(Sunday)', @level0type=N'SCHEMA', @level0name=N'Maint', @level1type=N'TABLE', @level1name=N'DataBaseSizeDtl_Week';
 
 
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'采集数据库记录的时间',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'DateCD';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'第几周',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'WeekCD';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'the datebase''s identity number',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'DataBaseId';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'the datebase file''s identity number',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'FileId';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库名称',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'DataBaseName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库逻辑名称',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'LogicalName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件类型',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'FileTypeDesc';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库物理文件',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'PhysicalName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库状态',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'StateDesc';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件最大值',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'MaxSize';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件是否按百分比增长',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'IsPercentGrwoth';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件是否自动增长',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'Growth';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库是否只读',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'IsReadOnly';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件大小',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Week', @level2type=N'COLUMN',@level2name=N'DataBaseSize';
 
 
 
IF EXISTS(SELECT 1 FROM sys.objects WHERE type='U' AND object_id=OBJECT_ID('Maint.DataBaseSizeDtl_Month'))
BEGIN
    DROP TABLE Maint.DataBaseSizeDtl_Month;
END
GO
 
CREATE TABLE Maint.DataBaseSizeDtl_Month
(
     DateCD                DATETIME
    ,MonthCD            INT
    ,DataBaseId            INT 
    ,FileId                INT
    ,DataBaseName        NVARCHAR(256)
    ,LogicalName        NVARCHAR(256)
    ,FileTypeDesc        NVARCHAR(120)
    ,PhysicalName        NVARCHAR(520)
    ,StateDesc            NVARCHAR(120)
    ,MaxSize            NVARCHAR(32)
    ,IsPercentGrwoth    BIT
    ,Growth                NVARCHAR(24)
    ,IsReadOnly            BIT
    ,DataBaseSize        FLOAT
    CONSTRAINT PK_DataBaseSizeDtl_Month PRIMARY KEY(MonthCD,DateCD, DataBaseId,FileId)
);
 
 
 
USE YourSQLDba;
GO
 
 
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'Database Size Detail Records every month(the first day)', @level0type=N'SCHEMA', @level0name=N'Maint', @level1type=N'TABLE', @level1name=N'DataBaseSizeDtl_Month';
 
 
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'采集数据库记录的时间',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'DateCD';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'采集数据的月份',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'MonthCD';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'the datebase''s identity number',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'DataBaseId';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'the datebase file''s identity number',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'FileId';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库名称',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'DataBaseName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库逻辑名称',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'LogicalName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件类型',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'FileTypeDesc';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库物理文件',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'PhysicalName';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库状态',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'StateDesc';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件最大值',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'MaxSize';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件是否按百分比增长',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'IsPercentGrwoth';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件是否自动增长',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'Growth';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库是否只读',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'IsReadOnly';
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'数据库文件大小',
                                @level0type='SCHEMA', @level0name='Maint', @level1type=N'TABLE',@level1name=N'DataBaseSizeDtl_Month', @level2type=N'COLUMN',@level2name=N'DataBaseSize';

 

存储过程[Maint].[Usp_Monitor_Database_Size]

 

 
作业YourSQLDba_Monitor_Database_Daily_Growth
 

作业YourSQLDba_Monitor_Database_Week_Growth

 

作业YourSQLDba_Monitor_Database_Month_Grwoth

 

转载地址:http://awfnx.baihongyu.com/

你可能感兴趣的文章
滴滴春节前裁员:内部人士称是重组人才体系 末尾淘汰
查看>>
IT基础架构又有新概念?可组合基础设施是什么?
查看>>
万科创始人王石出任华大基因联席董事长 协助汪建管理
查看>>
西藏林芝边境小康村迎藏历新年
查看>>
普悠玛号将开放卖站票 台铁局:安全测试报告合格
查看>>
广东法院公开裁判文书超350万份
查看>>
福建宁德自闭症儿童家长:有爱,我们不孤独
查看>>
CBA全明星赛再遭吐槽,是因为不用心吗?
查看>>
区块链应用网易星球将上线黑钻直接兑换,这是要复活的节奏吗
查看>>
看看100万程序员怎么评论:美国 IT 公司程序员会加班吗?
查看>>
库克:销量下滑,因为中国人都等着攒钱买iphone8
查看>>
java开发转行大数据开发的学习路径
查看>>
使用RecyclerView的两个非传统型崩溃
查看>>
Java 多线程设计模式之基础概念
查看>>
springboot(六):如何优雅的使用mybatis
查看>>
漫画:程序员的“情人”节应该这样度过
查看>>
并发容器之BlockingQueue
查看>>
聊一聊rank-1和rank-5准确度
查看>>
这是一份收藏量超过2万6的计算机科学学习笔记
查看>>
Spring SpringMVC Mybatis整合
查看>>