sql查看所有表大小的方法

本文提供了一种使用SQL脚本来查询数据库中所有表的大小、已用空间、索引大小和未使用空间的方法。通过创建临时表和游标,遍历用户表并计算每个表的详细空间使用情况。

sql查看所有表大小的方法。

代码:

declare @id int
declare @type character(2) 
declare @pages int 
declare @dbname sysname
declare @dbsize dec(15,0)
declare @bytesperpage dec(15,0)
declare @pagesperMB dec(15,0)
create table #spt_space
(
[objid] int null,
[rows] int null,
[reserved] dec(15) null,
[data] dec(15) null,
[indexp] dec(15) null,
[unused] dec(15) null
)
set nocount on
-- Create a cursor to loop through the user tables
declare c_tables cursor for
select id from sysobjects where xtype = 'U'
open c_tables fetch next from c_tables into @id
while @@fetch_status = 0
begin --- www.jbxue.com
/* Code from sp_spaceused */
insert into #spt_space (objid, reserved)
select objid = @id, sum(reserved)
from sysindexes
where indid in (0, 1, 255) and id = @id


select @pages = sum(dpages)
from sysindexes
where indid < 2
and id = @id

select @pages = @pages + isnull(sum(used), 0)
from sysindexes
where indid = 255 and id = @id

update #spt_space set data = @pages
where objid = @id

/* index: sum(used) where indid in (0, 1, 255) - data */

update #spt_space
set indexp = (select sum(used)
from sysindexes
where indid in (0, 1, 255)
and id = @id) - data
where objid = @id

/* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */
update #spt_space
set unused = reserved - (
select sum(used)
from sysindexes
where indid in (0, 1, 255) and id = @id
)
where objid = @id

update #spt_space set [rows] = i.[rows]
from sysindexes i
where i.indid < 2 and i.id = @id and objid = @id

fetch next from c_tables into @id
end
select TableName = (select left(name,60) from sysobjects where id = objid),
[Rows] = convert(char(11), rows),
ReservedKB = ltrim(str(reserved * d.low / 1024.,15,0) + ' ' + 'KB'),
DataKB = ltrim(str(data * d.low / 1024.,15,0) + ' ' + 'KB'),
IndexSizeKB = ltrim(str(indexp * d.low / 1024.,15,0) + ' ' + 'KB'),
UnusedKB = 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'
order by reserved desc
drop table #spt_space
close c_tables
deallocate c_tables

转载于:https://www.cnblogs.com/cfinder010/p/3551098.html

### 找人大金仓数据库中的数据大小 为了询人大金仓数据库中的数据大小,可以使用以下 SQL 语句: ```sql SELECT relname AS 名, pg_size_pretty(pg_total_relation_size(oid) ::bigint) AS 总大小, -- 包含索引 pg_size_pretty(sys_relation_size(oid) ::bigint) AS 实际大小 -- 数据 FROM sys_class t WHERE relname = 'ajjbx'; -- 替换为要询的具体名 ``` 此询返回指定的总大小(包括索引)以及实际存储数据所占用的空间大小[^1]。 对于更详细的统计信息,还可以通过 `sys_stat_user_tables` 视图来获取有关活元组和死元组的数量: ```sql SELECT relname, n_live_tup AS 活元组数, n_dead_tup AS 死元组数 FROM sys_stat_user_tables ORDER BY n_dead_tup DESC; ``` 这有助于了解内活动记录与已删除但未回收空间的记录情况[^2]。 #### 获取空间的信息及其大小 如果还需要进一步了解所在空间的相关属性,则可执行如下命令: ```sql SELECT spcname AS "Name", sys_catalog.sys_get_userbyid(spcowner) AS "Owner", sys_catalog.sys_tablespace_location(oid) AS "Location", sys_catalog.array_to_string(spcacl, E'\n') AS "Access privileges", spcoptions AS "Options", sys_catalog.sys_size_pretty(sys_catalog.sys_tablespace_size(oid)) AS "Size" FROM sys_catalog.sys_tablespace ORDER BY 1; ``` 这段脚本提供了关于各个空间的名字、拥有者、位置路径、访问权限列以及其他配置选项等详情,并且计算出了它们各自的尺寸大小[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值