监控SQLServer 数据库表每天的空间变化情况
阅读完桦仔的《分享一个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>
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>
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>
如有不对的地方,欢迎拍砖,谢谢!O(∩_∩)O

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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

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.

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.

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.

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.

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

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.
