SQLServer中系統存儲過程sp_spaceused SQL Server查看所有表大小、表行數和占用空間信息


SQLServer中系統存儲過程sp_spaceused

 

sp_spaceused

執行sp_spaceused存儲過程的時候可以不用帶參數,直接執行,或者exec sp_spaceused都可以,返回兩個結果集:

 

列名 數據類型 描述
database_name varchar(18) 當前數據庫的名稱。
database_size varchar(18) 當前數據庫的大小。
unallocated space varchar(18) 數據庫的未分配空間。

 

 

列名 數據類型 描述
reserved varchar(18) 保留的空間總量。
Data varchar(18) 數據使用的空間總量。
index_size varchar(18) 索引使用的空間。
Unused varchar(18) 未用的空間量。

 

 有關表的空間信息

下例報告為 aa 表分配(保留)的空間量、數據使用的空間量、索引使用的空間量以及由數據庫對象保留的未用空間量。

USE Test3
EXEC sp_spaceused 'aa'

 

SQL Server查看所有表大小、表行數和占用空間信息

一、查看表名和對應的數據行數

select  a.name as '表名',b.rows as '表數據行數'
from sysobjects a inner join sysindexes b
on a.id = b.id
where   a.type = 'u'
and b.indid in (0,1)
--and a.name not like 't%'
order by b.rows desc


二、查看表名和表占用空間信息
--判斷臨時表是否存在,存在則刪除重建
if exists(select 1 from tempdb..sysobjects where id=object_id('tempdb..#tabName') and xtype='u')
drop table #tabName
go
create table #tabName(
tabname varchar(100),
rowsNum varchar(100),
reserved varchar(100),
data varchar(100),
index_size varchar(100),
unused_size varchar(100)
)
 
declare @name varchar(100)
declare cur cursor for
select name from sysobjects where xtype='u' order by name
open cur
fetch next from cur into @name
while @@fetch_status=0
begin
    insert into #tabName
    exec sp_spaceused @name
    --print @name
 
    fetch next from cur into @name
end
close cur
deallocate cur

select tabname as '表名',rowsNum as '表數據行數',reserved as '保留大小',data as '數據大小',index_size as '索引大小',unused_size as '未使用大小'
from #tabName
--where tabName not like 't%'
order by cast(rowsNum as int) desc

 


--系統存儲過程說明:

--sp_spaceused 該存儲過程在系統數據庫master下。
exec sp_spaceused '表名' --該表占用空間信息
exec sp_spaceused           --當前數據庫占用空間信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM