查看SQL Server資料空間分配情況

來源:互聯網
上載者:User
server|資料 今天客戶反映資料庫檔案空間增長過快 ,需要分析資料庫表存放空間分配情況,臨時寫了以下過程,

與大家共用。


/********************************
功能:擷取表的空間分布情況  ycsoft 2005-07-13
**********************************/

if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
create table  tablespaceinfo                         --建立結果儲存表
              (nameinfo varchar(50) ,
               rowsinfo int , reserved varchar(20) ,
               datainfo varchar(20)  ,
               index_size varchar(20) ,
               unused varchar(20) )


delete from tablespaceinfo --清空資料表

declare @tablename varchar(255)  --表名稱

declare @cmdsql varchar(500)

DECLARE Info_cursor CURSOR FOR
select o.name 
from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1
     and o.name not like N'#%%'  order by o.name

OPEN Info_cursor

FETCH NEXT FROM Info_cursor
INTO @tablename

WHILE @@FETCH_STATUS = 0
BEGIN

  if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
  execute sp_executesql
         N'insert into tablespaceinfo  exec sp_spaceused @tbname',
          N'@tbname varchar(255)',
          @tbname = @tablename

  FETCH NEXT FROM Info_cursor
  INTO @tablename
END

CLOSE Info_cursor
DEALLOCATE Info_cursor
GO


--knowsky.com資料庫資訊
sp_spaceused @updateusage = 'TRUE' 

--表資訊
select *
from tablespaceinfo 
order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

備忘:

Name nvarchar(20) 為其請求空間使用資訊的表名。
Rows char(11) 表中現有的行數。
reserved varchar(18) 表保留的空間總量。
Data varchar(18) 表中的資料所使用的空間量。
index_size varchar(18) 表中的索引所使用的空間量。
Unused varchar(18) 表中未用的空間量。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.