Table of Contents
1. 创建表
2. 新建作业
3. 查询结果
Home Database Mysql Tutorial 监控SQLServer 数据库表每天的空间变化情况

监控SQLServer 数据库表每天的空间变化情况

Jun 07, 2016 pm 03:44 PM
sqlserver Variety Condition database monitor space read

阅读完桦仔的《分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)》后,我想使用文中提供的代码做一个统计表每天的新增行数及新增存储空间的功能 实现步骤如下: 1. 创建表 创建表,存储每天的表空间占用情况 CREATE TABLE [ dbo ]

阅读完桦仔的《分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)》后,我想使用文中提供的代码做一个统计表每天的新增行数及新增存储空间的功能

实现步骤如下:

1. 创建表

创建表,存储每天的表空间占用情况

<span>CREATE</span> <span>TABLE</span> <span>[</span><span>dbo</span><span>]</span>.<span>[</span><span>t_rpt_table_spaceinfo</span><span>]</span><span>(
    </span><span>[</span><span>table_name</span><span>]</span> <span>[</span><span>sysname</span><span>]</span> <span>NOT</span> <span>NULL</span><span>,
    </span><span>[</span><span>record_date</span><span>]</span> <span>[</span><span>date</span><span>]</span> <span>NOT</span> <span>NULL</span><span>,
    </span><span>[</span><span>record_time</span><span>]</span> <span>[</span><span>time</span><span>]</span>(<span>7</span>) <span>NOT</span> <span>NULL</span><span>,
    </span><span>[</span><span>rows_count</span><span>]</span> <span>[</span><span>bigint</span><span>]</span> <span>NULL</span><span>,
    </span><span>[</span><span>reserved</span><span>]</span> <span>[</span><span>bigint</span><span>]</span> <span>NULL</span><span>,
    </span><span>[</span><span>data_size</span><span>]</span> <span>[</span><span>bigint</span><span>]</span> <span>NULL</span><span>,
    </span><span>[</span><span>index_size</span><span>]</span> <span>[</span><span>bigint</span><span>]</span> <span>NULL</span><span>,
    </span><span>[</span><span>unused</span><span>]</span> <span>[</span><span>bigint</span><span>]</span> <span>NULL</span><span>,
 </span><span>CONSTRAINT</span> <span>[</span><span>PK_t_rpt_table_spaceinfo</span><span>]</span> <span>PRIMARY</span> <span>KEY</span> <span>CLUSTERED</span><span> 
(
    </span><span>[</span><span>table_name</span><span>]</span> <span>ASC</span><span>,
    </span><span>[</span><span>record_date</span><span>]</span> <span>ASC</span><span>,
    </span><span>[</span><span>record_time</span><span>]</span> <span>ASC</span><span>
)
)</span>
Copy after login

2. 新建作业

新建作业,作业计划每天凌晨运行一次,每天记录表占用的空间情况,存储到上一步建立的表中

作业中执行的T-SQL代码为:

<span>SET</span> NOCOUNT <span>ON</span>  
<span>/*</span><span>创建临时表,存放用户表的空间及数据行数信息</span><span>*/</span>
<span>CREATE</span> <span>TABLE</span><span> #tablespaceinfo
    (
      nameinfo </span><span>VARCHAR</span>(<span>500</span><span>) ,
      rowsinfo </span><span>BIGINT</span><span> ,
      reserved </span><span>VARCHAR</span>(<span>20</span><span>) ,
      datainfo </span><span>VARCHAR</span>(<span>20</span><span>) ,
      index_size </span><span>VARCHAR</span>(<span>20</span><span>) ,
      unused </span><span>VARCHAR</span>(<span>20</span><span>)
    )  
 
</span><span>DECLARE</span> <span>@tablename</span> <span>VARCHAR</span>(<span>255</span><span>);  

</span><span>/*</span><span>使用游标,循环得到表空间使用情况</span><span>*/</span> 
<span>DECLARE</span> Info_cursor <span>CURSOR</span>
<span>FOR</span>
    <span>SELECT</span>  <span>'</span><span>[</span><span>'</span> <span>+</span> <span>[</span><span>name</span><span>]</span> <span>+</span> <span>'</span><span>]</span><span>'</span>
    <span>FROM</span><span>    sys.tables
    </span><span>WHERE</span>   type <span>=</span> <span>'</span><span>U</span><span>'</span><span>;  
 
</span><span>OPEN</span><span> Info_cursor  
</span><span>FETCH</span> <span>NEXT</span> <span>FROM</span> Info_cursor <span>INTO</span> <span>@tablename</span>  
 
<span>WHILE</span> <span>@@FETCH_STATUS</span> <span>=</span> <span>0</span>
    <span>BEGIN</span> 
        <span>INSERT</span>  <span>INTO</span><span> #tablespaceinfo
                </span><span>EXEC</span> sp_spaceused <span>@tablename</span>  
        <span>FETCH</span> <span>NEXT</span> <span>FROM</span><span> Info_cursor  
    </span><span>INTO</span> <span>@tablename</span>  
    <span>END</span> 

<span>INSERT</span> <span>INTO</span><span> t_rpt_table_spaceinfo
(record_date, record_time, </span><span>[</span><span>table_name</span><span>]</span>, <span>[</span><span>rows_count</span><span>]</span><span>
, reserved, </span><span>[</span><span>data_size</span><span>]</span><span>, index_size, unused)
</span><span>SELECT</span> <span>convert</span>(date,<span>getdate</span>()), <span>convert</span>(<span>varchar</span>(<span>8</span>),<span>getdate</span>(),<span>114</span><span>), nameinfo, rowsinfo
,</span><span>CAST</span>(<span>REPLACE</span>(reserved, <span>'</span><span>KB</span><span>'</span>, <span>''</span>) <span>AS</span> <span>BIGINT</span>) ,<span>CAST</span>(<span>REPLACE</span>(datainfo, <span>'</span><span>KB</span><span>'</span>, <span>''</span>) <span>AS</span> <span>BIGINT</span><span>) 
,</span><span>CAST</span>(<span>REPLACE</span>(index_size, <span>'</span><span>KB</span><span>'</span>, <span>''</span>) <span>AS</span> <span>BIGINT</span>) ,<span>CAST</span>(<span>REPLACE</span>(unused, <span>'</span><span>KB</span><span>'</span>, <span>''</span>) <span>AS</span> <span>BIGINT</span><span>)  
</span><span>FROM</span><span> #tablespaceinfo
 
</span><span>CLOSE</span><span> Info_cursor  
</span><span>DEALLOCATE</span><span> Info_cursor  
</span><span>DROP</span> <span>TABLE</span> <span>[</span><span>#tablespaceinfo</span><span>]</span>
Copy after login

3. 查询结果

连续的数据记录之间做比较,即可得到数据的增量变化情况

示例代码如下:

;<span>with</span> table_spaceinfo <span>as</span><span> 
(
   </span><span>select</span><span> record_date, record_time, table_name, rows_count, reserved, data_size, index_size, unused
        ,ROW_NUMBER() </span><span>over</span>(PARTITION <span>by</span> table_name <span>order</span> <span>by</span> record_date,record_time <span>asc</span>) <span>as</span><span> list_no
   </span><span>from</span><span> t_rpt_table_spaceinfo
)
</span><span>select</span> _a.table_name <span>as</span> 表名,<span>convert</span>(<span>varchar</span>(<span>20</span>),_a.record_date)<span>+</span><span>'</span> <span>'</span><span>+</span><span>convert</span>(<span>varchar</span>(<span>8</span>),_a.record_time)<span>+</span><span>'</span><span>~~</span><span>'</span>
    <span>+</span><span>convert</span>(<span>varchar</span>(<span>20</span>),_b.record_date)<span>+</span><span>'</span> <span>'</span><span>+</span><span>convert</span>(<span>varchar</span>(<span>8</span>),_b.record_time) <span>as</span> <span>[</span><span>时间段范围</span><span>]</span><span>
    ,_b.rows_count</span><span>-</span>_a.rows_count <span>as</span> <span>[</span><span>新增的行数</span><span>]</span><span>
    ,_b.data_size </span><span>-</span> _a.data_size <span>as</span> <span>[</span><span>新增数据空间(KB)</span><span>]</span>
<span>from</span><span> table_spaceinfo _a
</span><span>join</span> table_spaceinfo _b <span>on</span> _a.table_name<span>=</span>_b.table_name <span>and</span> _a.list_no<span>=</span>_b.list_no<span>-</span><span>1</span>
<span>order</span> <span>by</span> <span>[</span><span>时间段范围</span><span>]</span> 
Copy after login

如有不对的地方,欢迎拍砖,谢谢!O(∩_∩)O

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to import mdf file into sqlserver How to import mdf file into sqlserver Apr 08, 2024 am 11:41 AM

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

How to delete sqlserver if the installation fails? How to delete sqlserver if the installation fails? Apr 05, 2024 pm 11:27 PM

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer

What is the difference between mysql and sqlserver syntax What is the difference between mysql and sqlserver syntax Apr 22, 2024 pm 06:33 PM

The syntax differences between MySQL and SQL Server are mainly reflected in database objects, data types, SQL statements and other aspects. Database object differences include the storage engine and how filegroups are specified, and the creation of indexes and constraints. Data type differences involve differences in numeric types, character types, and date and time types. SQL statement differences are reflected in result set limitations, data insertion, update and delete operations, etc. Other differences include how identity columns, views, and stored procedures are created. Understanding these differences is important to avoid errors when using different database systems.

What should I do if sqlserver cannot be deleted and cannot be reinstalled? What should I do if sqlserver cannot be deleted and cannot be reinstalled? Apr 05, 2024 pm 11:30 PM

The problem that SQL Server cannot be reinstalled due to incomplete deletion can be solved by following the following steps: manually delete files and registry entries; use SQL Server installation and uninstall tools; use third-party uninstall tools; check Windows Event Viewer; restart the computer; reinstall SQL Server.

Where is the navicat database file? Where is the navicat database file? Apr 23, 2024 am 10:57 AM

The location where the Navicat database configuration files are stored varies by operating system: Windows: The user-specific path is %APPDATA%\PremiumSoft\Navicat\macOS: The user-specific path is ~/Library/Application Support/Navicat\Linux: The user-specific path is ~/ .config/navicat\The configuration file name contains the connection type, such as navicat_mysql.ini. These configuration files store database connection information, query history, and SSH settings.

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

See all articles