SQL計算資料庫表佔用的真實空間大小

來源:互聯網
上載者:User

   最近客戶的資料庫暴漲,一開始找不到原因,用sql查詢表的空間大小,結果之前用的sql語句只能查詢除了text,image外的欄位大小.搞得一時間混亂不堪.後來諮詢了大師們,找到用sp_spaceused這個系統函數可以查詢表的真實大小,包含text等欄位.sql如下:

declare @id            NVARCHAR(100)create table #spt_space(    [name]            NVARCHAR(50) null,    [rows]          int null,    [reserved]       NVARCHAR(50) null,    [data]         NVARCHAR(50) null,    [index_size]        NVARCHAR(50) null,    [unused]         NVARCHAR(50) null)set nocount ondeclare c_tables cursor forselect name from sysobjects where xtype = 'U'open c_tables fetch next from c_tables into @idwhile @@fetch_status = 0begin    /* Code from sp_spaceused */    insert into #spt_space ([name],[rows],reserved,data,index_size,unused)    EXEC sp_spaceused @id    fetch next from c_tables   into @idendSELECT * FROM(SELECT *,CAST(REPLACE(reserved,'KB','') AS INT) AS reservedindex FROM #spt_space) corder by c.reservedindex descdrop table #spt_spaceclose c_tablesdeallocate c_tables

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.