【转载】sp_spaceused2,看库里全部表占用的物理空间
我常用sp_spaceused来查看表所占的空间大小,可是一次只能看一个对象的。
今天有空写了个sp_spaceused2,可以同时看全部表的空间大小。
use master
go
EXEC sp_addmessage @msgnum = 55000, @severity = 16,
@msgtext = N'Objects of type %s do not have space allocated.',
@lang = 'us_english',@replace = 'replace'

EXEC sp_addmessage @msgnum = 55000, @severity = 16,
@msgtext = N'没有为类型 %1! 的对象分配的空间。',
@lang = '简体中文',@replace = 'replace'
go
if exists (select 1 from sysobjects where name = 'sp_spaceused2' and type = 'P')
drop procedure sp_spaceused2
go
create procedure sp_spaceused2 (
@type varchar(2) = 'U', -- The objects type we want size on.
@updateusage varchar(5) = false -- Param. for specifying that
-- usage info. should be updated.
)
as
create table #spt_space
(
id int not null primary key,
name sysname,
rows int null,
reserved dec(15) null,
data dec(15) null,
indexp dec(15) null,
unused dec(15) null
)

-- Check the object type.
if @type not in ('U','S') -- no physical data storage.
begin
raiserror(55000,-1,-1,@type)
return (1)
end

-- Check to see if user wants usages updated.

if @updateusage is not null
begin
select @updateusage=lower(@updateusage)

if @updateusage not in ('true','false')
begin
raiserror(15143,-1,-1,@updateusage)
return(1)
end
end

if @updateusage = 'true'
begin
dbcc updateusage(0) with no_infomsgs
print ' '
end

set nocount on
--id, name, rows
insert into #spt_space (id, name, rows, data)
select i.id, o.name, i.rows, 0
from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid < 2 and o.type = @type

--reserved
update #spt_space
set reserved = r.reserved
from ( select o.id, sum(i.reserved) as reserved from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id

--data
update #spt_space
set data = data + r.pages
from ( select o.id, isnull(sum(i.dpages),0) as pages from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid < 2 and o.type = @type group by o.id) r
where #spt_space.id = r.id

update #spt_space
set data = data + r.used
from ( select o.id, isnull(sum(used), 0) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid = 255 and o.type = @type group by o.id) r
where #spt_space.id = r.id


--index page
update #spt_space
set indexp = r.used - data
from ( select o.id, sum(convert(dec(15),used)) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id

--unused page
update #spt_space
set unused = reserved - r.used
from ( select o.id, sum(convert(dec(15),used)) as used from sysindexes i inner join sysobjects o on i.id = o.id
where i.indid in (0, 1, 255) and o.type = @type group by o.id) r
where #spt_space.id = r.id

--output
select #spt_space.name,
rows = convert(char(11), rows),
reserved = ltrim(str(reserved * d.low / 1024.,15,0) +
' ' + 'KB'),
data = ltrim(str(data * d.low / 1024.,15,0) +
' ' + 'KB'),
index_size = ltrim(str(indexp * d.low / 1024.,15,0) +
' ' + 'KB'),
unused = ltrim(str(unused * d.low / 1024.,15,0) +
' ' + 'KB')
from #spt_space, master.dbo.spt_values d
where d.number = 1
and d.type = 'E'

return 0
go


sp_MS_marksystemobject 'sp_spaceused2'

go
use [dbname]
exec sp_spaceused2
出处:http://blog.myspace.cn/1300316663/archive/2007/05/23/249582259.aspx
今天有空写了个sp_spaceused2,可以同时看全部表的空间大小。



























































































































【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构