一個統計表天天的新增行數及新增存儲空間的功效。本站提示廣大學習愛好者:(一個統計表天天的新增行數及新增存儲空間的功效)文章只能為提供參考,不一定能成為您想要的結果。以下是一個統計表天天的新增行數及新增存儲空間的功效正文
應用文中供給的代碼做一個統計表天天的新增行數及新增存儲空間的功效
完成步調以下:
1. 創立表
創立表,存儲天天的表空間占用情形
CREATE TABLE [dbo].[t_rpt_table_spaceinfo]( [table_name] [sysname] NOT NULL, [record_date] [date] NOT NULL, [record_time] [time](7) NOT NULL, [rows_count] [bigint] NULL, [reserved] [bigint] NULL, [data_size] [bigint] NULL, [index_size] [bigint] NULL, [unused] [bigint] NULL, CONSTRAINT [PK_t_rpt_table_spaceinfo] PRIMARY KEY CLUSTERED ( [table_name] ASC, [record_date] ASC, [record_time] ASC ) )
2. 新建功課
新建功課,功課籌劃天天清晨運轉一次,天天記載表占用的空間情形,存儲到上一步樹立的表中
功課中履行的T-SQL代碼為:
SET NOCOUNT ON /*創立暫時表,寄存用戶表的空間及數據行數信息*/ CREATE TABLE #tablespaceinfo ( nameinfo VARCHAR(500) , rowsinfo BIGINT , reserved VARCHAR(20) , datainfo VARCHAR(20) , index_size VARCHAR(20) , unused VARCHAR(20) ) DECLARE @tablename VARCHAR(255); /*應用游標,輪回獲得表空間應用情形*/ DECLARE Info_cursor CURSOR FOR SELECT '[' + [name] + ']' FROM sys.tables WHERE type = 'U'; OPEN Info_cursor FETCH NEXT FROM Info_cursor INTO @tablename WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO #tablespaceinfo EXEC sp_spaceused @tablename FETCH NEXT FROM Info_cursor INTO @tablename END INSERT INTO t_rpt_table_spaceinfo (record_date, record_time, [table_name], [rows_count] , reserved, [data_size], index_size, unused) SELECT convert(date,getdate()), convert(varchar(8),getdate(),114), nameinfo, rowsinfo ,CAST(REPLACE(reserved, 'KB', '') AS BIGINT) ,CAST(REPLACE(datainfo, 'KB', '') AS BIGINT) ,CAST(REPLACE(index_size, 'KB', '') AS BIGINT) ,CAST(REPLACE(unused, 'KB', '') AS BIGINT) FROM #tablespaceinfo CLOSE Info_cursor DEALLOCATE Info_cursor DROP TABLE [#tablespaceinfo]
3. 查詢成果
持續的數據記載之間做比擬,便可獲得數據的增質變化情形
示例代碼以下:
;with table_spaceinfo as ( select record_date, record_time, table_name, rows_count, reserved, data_size, index_size, unused ,ROW_NUMBER() over(PARTITION by table_name order by record_date,record_time asc) as list_no from t_rpt_table_spaceinfo ) select _a.table_name as 表名,convert(varchar(20),_a.record_date)+' '+convert(varchar(8),_a.record_time)+'~~' +convert(varchar(20),_b.record_date)+' '+convert(varchar(8),_b.record_time) as [時光段規模] ,_b.rows_count-_a.rows_count as [新增的行數] ,_b.data_size - _a.data_size as [新增數據空間(KB)] from table_spaceinfo _a join table_spaceinfo _b on _a.table_name=_b.table_name and _a.list_no=_b.list_no-1 order by [時光段規模]