葡京网投哪个正规 > 首页 > 葡京网投哪个正规:mssqlserver查看数据库中各表的占用空间情况,Server查看表空间占用情况

原标题:葡京网投哪个正规:mssqlserver查看数据库中各表的占用空间情况,Server查看表空间占用情况

浏览次数:129 时间:2020-05-08

本文章分享一篇关于mssqlserver查看数据库中各表的占用空间情况有需要的同学可以看看。

 

同样的命题在SQL Server 2005中,微软发布了一个报表,可以直接查看,但是这个报表不能用于SQL Server 2000。
于是我就写了下面这几句话,可以返回同样的结果。
create table tmp (name varchar(50),rows int,reserved varchar(50),
  data varchar(50),index_size varchar(50),unused varchar(50))
insert into tmp (name,rows,reserved,
  data,index_size,unused) exec sp_msforeachTable @Command1="sp_spaceused '?'"--sp_spaceused 't_vehicle'
select * from tmp order by data desc
drop table tmp
--看单个表, 占用数据的情况
sp_spaceused 'retail11'
--查看整个数据库所有表占用空间的情况 但发现从sysindexes中取表, 会少一部分数据表(无主键,无索引的表)
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
--查看整个数据库所有表占用空间的情况 但看起来比较麻烦
exec sp_MSforeachtable "exec sp_spaceused '?'"

使用如下sql script可以获得每个数据库表所占用的空间大小,单位是KB

查看数据库中各表的占用空间情况:

查看数据库中各表的占用空间情况:

来源:

create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_size varchar(100),unused varchar(100))

代码如下复制代码 create table tablesize (name varchar(50),rows int,reserved varchar(50), data varchar(50),index_size varchar(50),unused varchar(50))insert into tablesize (name,rows,reserved, data,index_size,unused) exec sp_msforeachTable @Command1=sp_spaceused '?'update tablesize set data=replace(data,'KB','')select *,convert(int,data) as a from tablesize order by A desc drop table tablesize

 

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 #data
exec sp_spaceused @name
print @name

create table tablesize (name varchar(50),rows int,reserved varchar(50),
  data varchar(50),index_size varchar(50),unused varchar(50))
insert into tablesize (name,rows,reserved,
  data,index_size,unused) exec sp_msforeachTable @Command1="sp_spaceused '?'"
 
update tablesize set data=replace(data,'KB','')
葡京网投哪个正规, 
select *,convert(int,data) as a from tablesize order by A desc
 
drop table tablesize

fetch next from cur into @name
end
close cur
deallocate cur

create table #DataNew(name varchar(100),row int,reserved int,data int,index_size int,unused int)

insert into #葡京正网网投,dataNew
select name,convert(int,row) as [row],convert(int,replace(reserved,'KB',''))as reserved,convert(int,replace(data,'KB','')) as [data],
convert(int,replace(index_size,'KB','')) as [index_size],convert(int,replace(unused,'KB','')) as [unused] from #data

select * from #dataNew order by data desc

drop table #Data
drop table #dataNew

 

本文由葡京网投哪个正规发布于首页,转载请注明出处:葡京网投哪个正规:mssqlserver查看数据库中各表的占用空间情况,Server查看表空间占用情况

关键词:

上一篇:触发器代码,Oracle使用触发器和mysql中使用触发器的比较

下一篇:没有了