(1)Question:尼玛一个数据库,动辄几十个G,伤不起啊,怎样才能知道当前数据库里面各个表的大小呢?以便将部分较大的数据库表中不容易被频繁访问的数据归档到历史表中,例如每天将一个自然年以前的数据放入历史表中。
(2)Key:网上搜了一圈,关键字sp_spaceused (参见:http://msdn.microsoft.com/zh-cn/library/ms188776.aspx)
(3)Sample:同时找到了一个示例(参见:http://www.linuxso.com/linuxxitongguanli/519.html 推荐: SQL Server 2005 查看数据库表的大小 按照表大小排列),下面就将这个示例的代码原样抄写下来,供大家分享啦!对了,我在2005上验证过,代码没有什么大问题。
(4)Code:
原文地址:http://bbs.csdn.net/topics/380068082
(2)Key:网上搜了一圈,关键字sp_spaceused (参见:http://msdn.microsoft.com/zh-cn/library/ms188776.aspx)
(3)Sample:同时找到了一个示例(参见:http://www.linuxso.com/linuxxitongguanli/519.html 推荐: SQL Server 2005 查看数据库表的大小 按照表大小排列),下面就将这个示例的代码原样抄写下来,供大家分享啦!对了,我在2005上验证过,代码没有什么大问题。
(4)Code:
(4.1)将表大小占用情况存放到新创建的 tablespaceinfo表中
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) create table tablespaceinfo --创建结果存储表 (nameinfo varchar(50) , rowsinfo int , reserved varchar(20) , datainfo varchar(20) , index_size varchar(20) , unused varchar(20) ) delete from tablespaceinfo --清空数据表 declare @tablename varchar(255) --表名称 declare @cmdsql varchar(500) DECLARE Info_cursor CURSOR FOR select o.name from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1 and o.name not like N'#%%' order by o.name OPEN Info_cursor FETCH NEXT FROM Info_cursor INTO @tablename WHILE @@FETCH_STATUS = 0 BEGIN if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1) execute sp_executesql N'insert into tablespaceinfo exec sp_spaceused @tbname', N'@tbname varchar(255)', @tbname = @tablename FETCH NEXT FROM Info_cursor INTO @tablename END CLOSE Info_cursor DEALLOCATE Info_cursor GO据说:
sp_spaceused的结果有时是不准确的, 要加updateusage选项才行,修改版:
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[tablespaceinfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) create table tablespaceinfo --创建结果存储表 (nameinfo varchar(50) , rowsinfo int , reserved varchar(20) , datainfo varchar(20) , index_size varchar(20) , unused varchar(20) ) delete from tablespaceinfo --清空数据表 declare @tablename varchar(255) --表名称 declare @cmdsql varchar(500) DECLARE Info_cursor CURSOR FOR select o.name from dbo.sysobjects o where OBJECTPROPERTY(o.id, N'IsTable') = 1 and o.name not like N'#%%' order by o.name OPEN Info_cursor FETCH NEXT FROM Info_cursor INTO @tablename WHILE @@FETCH_STATUS = 0 BEGIN if exists (select * from dbo.sysobjects where id = object_id(@tablename) and OBJECTPROPERTY(id, N'IsUserTable') = 1) execute sp_executesql N'insert into tablespaceinfo exec sp_spaceused @tbname,@updateusage=true', N'@tbname varchar(255)', @tbname = @tablename FETCH NEXT FROM Info_cursor INTO @tablename END CLOSE Info_cursor DEALLOCATE Info_cursor GO(4.2)查看当前数据库大小情况
--itlearner注:显示数据库信息 sp_spaceused @updateusage = 'TRUE'(4.3)查看存放了当前数据库各个表大小的tablespaceinfo表中记录
--itlearner注:显示表信息 select * from tablespaceinfo order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc
原文地址:http://bbs.csdn.net/topics/380068082