檢視MSSQL資料庫每個表占用的空間大小

2022-01-14 13:32:40 字數 2640 閱讀 6799

檢視mssql資料庫每個表占用的空間大小

sp_spaceused

顯示行數、保留的磁碟空間以及當前資料庫中的表所使用的磁碟空間,或顯示由整個資料庫保留和使用的磁碟空間。

語法sp_spaceused [[@objname =] 'objname']

[,[@updateusage =] 'updateusage']

引數[@objname =] 'objname'

是為其請求空間使用資訊(保留和已分配的空間)的表名。objname 的資料型別是 nvarchar(776),預設設定為 null。

[@updateusage =] 'updateusage'

表示應在資料庫內(未指定 objname 時)還是在特定的物件上(指定 objname 時)執行 dbcc updateusage。值可以是 true 或 false。updateusage 的資料型別是 varchar(5),預設設定為 false。

返回**值

0(成功)或 1(失敗)

示例a. 有關表的空間資訊

下例報告為 titles 表分配(保留)的空間量、資料使用的空間量、索引使用的空間量以及由資料庫物件保留的未用空間量。

use pubs

exec sp_spaceused 'titles'

b. 有關整個資料庫的已更新空間資訊

下例概括當前資料庫使用的空間並使用可選引數 @updateusage。

use pubs

sp_spaceused @updateusage = 'true'

不過此方法,只能檢視乙個表的大小,乙個資料庫中一般會有多個表,如何一次性檢視某資料庫的所有表大小呢?

第一種方法(較簡單,看的有些吃力):

exec sp_msforeachtable "exec sp_spaceused '?'"

第二種方法(較複雜,但看的比較清楚,原作者不詳):

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

endclose info_cursor

deallocate info_cursor

go--itlearner注:顯示資料庫資訊

sp_spaceused @updateusage = 'true'

--itlearner注:顯示表資訊

select *

from tablespaceinfo

order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

第三種方法:

select object_name(id) tablename,8*reserved/1024 reserved,rtrim(8*dpages/1024)+'mb' used,8*(reserved-dpages)/1024 unused,8*dpages/1024-rows/1024*minlen/1024 free,

rows,* from sysindexes

where indid=1

order by reserved desc

檢視SQLServer資料庫每個表占用的空間大小

建立儲存過程 create procedure dbo sys viewtablespace as begin set nocount on create table dbo tableinfo 表名 varchar 50 collate chinese prc ci as null 記錄數 int...

檢視 MySQL 資料庫中每個表占用的空間大小

如果想知道mysql資料庫中每個表占用的空間 表記錄的行數的話,可以開啟mysql的 information schema 資料庫。在該庫中有乙個 tables 表,這個表主要字段分別是 table schema 資料庫名 table name 表名 engine 所使用的儲存引擎 tables r...

檢視 MySQL 資料庫中每個表占用的空間大小

我在做爬蟲的過程中,剛剛爬了幾萬條資料,放在了mysql資料庫裡,於是想看看mysql中這個資料庫大小以及每個表的大小,於是進行了查閱,主要查到了說法,組合在一起趕緊特別的好,如下。第一種 如果想知道mysql資料庫中每個表占用的空間 表記錄的行數的話,可以開啟mysql的 information ...