SQLServer查看各个表大小

该文提供了一段SQL脚本,用于在SQLServer中查看各个用户表的大小,包括总大小、数据大小、索引大小和未使用空间,以及表的行数。此外,还展示了如何查看所有表的占用空间和特定表字段的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQLServer查看各个表大小

--SQLServer查看各个表大小

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 
 
  /* 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

–查看数据表占用硬盘的空间大小

	 create table test(
name varchar(50),
rows int,
reserved varchar(20),
data varchar(20),
index_size varchar(20),
unused varchar(20)
)
insert into test
exec sp_MSforeachtable "exec sp_spaceused '?'"
select * from test order by cast(replace(reserved,'KB','') as int) desc

–查看指定数据表字段大小

SELECT 
     表名       = case when a.colorder=1 then d.name else '' end,
     表说明     = case when a.colorder=1 then isnull(f.value,'') else '' end,
     字段序号   = a.colorder,
     字段名     = a.name,
     标识       = case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
     主键       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id and name in (
                      SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid))) then '√' else '' end,
     类型       = b.name,
     占用字节数 = a.length,
     长度       = COLUMNPROPERTY(a.id,a.name,'PRECISION'),
     小数位数   = isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
     允许空     = case when a.isnullable=1 then '√'else '' end,
     默认值     = isnull(e.text,''),
     字段说明   = isnull(g.[value],'')
 FROM 
     syscolumns a
 left join 
     systypes b 
 on 
     a.xusertype=b.xusertype
 inner join 
     sysobjects d 
 on 
     a.id=d.id  and d.xtype='U' and  d.name<>'dtproperties'
 left join 
     syscomments e 
 on 
     a.cdefault=e.id
 left join 
 sys.extended_properties   g 
 on 
     a.id=G.major_id and a.colid=g.minor_id  
 left join
 sys.extended_properties f
 on 
     d.id=f.major_id and f.minor_id=0
 where 
     d.name='GYUserConfig'    --如果只查询指定表,加上此where条件,tablename是要查询的表名;去除where条件查询所有的表信息
 order by 
     a.id,a.colorder
<think>好的,用户想在SQL Server 2008中查看每个大小。我需要先回想一下相关知识。记得在SQL Server中,可以通过系统视图来获取和索引的空间使用情况,比如使用sys.tables和sys.dm_db_partition_stats结合。 首先,用户需要的是每个的行数和占用空间。可能需要编写一个查询,连接多个系统视图,比如sys.tables、sys.indexes和sys.dm_db_partition_stats。可能需要用到SUM函数来统计每个的总页数,然后转换成MB。 然后,记得每个页的大小是8KB,所以总页数乘以8再除以1024可以得到MB。同时,sys.dm_db_partition_stats中的used_page_count包括了数据和索引的页数,所以可能需要用这个字段。 另外,还要考虑是否要包括索引的大小,或者单独显示数据和索引的大小。可能需要将结果按照名分组,并计算每个的总大小。 需要检查语法是否正确,确保在SQL Server 2008中这些系统视图是否存在。比如sys.dm_db_partition_stats在2005及以后版本都有,所以2008应该是支持的。 然后,可能需要提供一个示例查询,让用户可以直接运行。同时,解释每个步骤的作用,比如字段的含义和计算方式。用户可能还需要知道如何执行这个查询,比如在SSMS中新建查询窗口,然后运行。 另外,用户提到的引用[^1]是关于统计大小进行性能优化和容量规划,可以提到这一点,说明查询结果的作用。比如,优化性能或规划存储时,了解各大小很重要。 最后,生成相关问题,可能包括如何优化大、如何清理数据,或者如何自动化这个统计过程。这样用户可以有进一步的学习方向。</think>### 在 SQL Server 2008 中查看大小的步骤 #### 方法一:使用系统视图和动态管理视图 可通过组合系统视图 `sys.tables` 和动态管理视图 `sys.dm_db_partition_stats` 实现: ```sql SELECT t.NAME AS 名, p.rows AS 行数, SUM(a.total_pages) * 8 / 1024 AS 总空间(MB), SUM(a.used_pages) * 8 / 1024 AS 已用空间(MB), (SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 AS 未用空间(MB) FROM sys.tables t INNER JOIN sys.indexes i ON t.object_id = i.object_id INNER JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id INNER JOIN sys.allocation_units a ON p.partition_id = a.container_id WHERE t.is_ms_shipped = 0 GROUP BY t.Name, p.Rows ORDER BY 总空间(MB) DESC; ``` **说明**: 1. `sys.tables` 存储所有用户信息 2. `sys.dm_db_partition_stats` 提供分区级存储统计 3. 空间计算基于 SQL Server 的 8KB 页大小标准[^1] #### 方法二:存储过程 `sp_spaceused` 针对单个的快速查询: ```sql EXEC sp_spaceused '名'; ``` 查看所有的空间使用: ```sql EXEC sp_msforeachtable 'EXEC sp_spaceused [?]'; ``` #### 方法三:通过 SSMS 图形界面 1. 打开 SQL Server Management Studio 2. 右键点击目标数据库 > 报 > 标准报 3. 选择「按的磁盘使用情况」
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值