一个查看MSSQLServer数据库空间使用情况的存储过程 SpaceUsed

所属分类: 数据库 / MsSql 阅读数: 782
收藏 0 赞 0 分享
运行下面存储过程

然后直接使用 SpaceUsed 就可以查看了.

存储过程代码

 程序代码

复制代码 代码如下:

Create procedure SpaceUsed 

as 

begin 

declare @id       int                  -- The object id of @objname. 

declare @type       character(2) -- The object type. 

declare       @pages       int                  -- Working variable for size calc. 

declare @dbname sysname 

declare @dbsize dec(15,0) 

declare @logsize dec(15) 

declare @bytesperpage       dec(15,0) 

declare @pagesperMB              dec(15,0) 

declare @objname nvarchar(776)        -- The object we want size on. 

declare @updateusage varchar(5)             -- Param. for specifying that 

create table #temp1 



       表名              varchar(200) null, 

       行数               char(11) null, 

       保留空间        varchar(15) null, 

       数据使用空间       varchar(15) null, 

       索引使用空间       varchar(15) null, 

        未用空间          varchar(15) null 



--select @objname='N_dep'                               -- usage info. should be updated. 

select @updateusage='false' 

/*Create temp tables before any DML to ensure dynamic 

**  We need to create a temp table to do the calculation. 

**  reserved: sum(reserved) where indid in (0, 1, 255) 

**  data: sum(dpages) where indid < 2 + sum(used) where indid = 255 (text) 

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

**  unused: sum(reserved) - sum(used) where indid in (0, 1, 255) 

*/ 

declare cur_table cursor for 

  select name from sysobjects where type='u' 

Open cur_table 

fetch next from cur_table into @objname 

While @@FETCH_STATUS=0 

begin 

create table #spt_space 



       rows              int null, 

       reserved    dec(15) null, 

       data        dec(15) null, 

       indexp             dec(15) null, 

       unused             dec(15) null 



/* 

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

/* 

**  Check to see that the objname is local. 

*/ 

if @objname IS NOT NULL 

begin 

       select @dbname = parsename(@objname, 3) 

       if @dbname is not null and @dbname <> db_name() 

              begin 

                     raiserror(15250,-1,-1) 

                     return (1) 

              end 

       if @dbname is null 

              select @dbname = db_name() 

       /* 

       **  Try to find the object. 

       */ 

       select @id = null 

       select @id = id, @type = xtype 

              from sysobjects 

                     where id = object_id(@objname) 

       /* 

       **  Does the object exist? 

       */ 

       if @id is null 

              begin 

                     raiserror(15009,-1,-1,@objname,@dbname) 

                     return (1) 

              end 

       if not exists (select * from sysindexes 

                            where @id = id and indid < 2) 

              if      @type in ('P ','D ','R ','TR','C ','RF') --data stored in sysprocedures 

                            begin 

                                   raiserror(15234,-1,-1) 

                                   return (1) 

                            end 

              else if @type = 'V ' -- View => no physical data storage. 

                            begin 

                                   raiserror(15235,-1,-1) 

                                   return (1) 

                            end 

              else if @type in ('PK','UQ') -- no physical data storage. --?!?! too many similar messages 

                            begin 

                                   raiserror(15064,-1,-1) 

                                   return (1) 

                            end 

              else if @type = 'F ' -- FK => no physical data storage. 

                            begin 

                                   raiserror(15275,-1,-1) 

                                   return (1) 

                            end 

end 

/* 

**  Update usages if user specified to do so. 

*/ 

if @updateusage = 'true' 

       begin 

              if @objname is null 

                     dbcc updateusage(0) with no_infomsgs 

              else 

                     dbcc updateusage(0,@objname) with no_infomsgs 

              print ' ' 

       end 

set nocount on 

/* 

**  If @id is null, then we want summary data. 

*/ 

/*    Space used calculated in the following way 

**       @dbsize = Pages used 

**       @bytesperpage = d.low (where d = master.dbo.spt_values) is 

**    the # of bytes per page when d.type = 'E' and 

**       d.number = 1. 

**    Size = @dbsize * d.low / (1048576 (OR 1 MB)) 

*/ 

if @id is null 

begin 

       select @dbsize = sum(convert(dec(15),size)) 

              from dbo.sysfiles 

              where (status & 64 = 0) 

       select @logsize = sum(convert(dec(15),size)) 

              from dbo.sysfiles 

              where (status & 64 <> 0) 

       select @bytesperpage = low 

              from master.dbo.spt_values 

              where number = 1 

                     and type = 'E' 

       select @pagesperMB = 1048576 / @bytesperpage 

       select  database_name = db_name(), 

              database_size = 

                     ltrim(str((@dbsize + @logsize) / @pagesperMB,15,2) + ' MB'), 

              'unallocated space' = 

                     ltrim(str((@dbsize - 

                            (select sum(convert(dec(15),reserved)) 

                                   from sysindexes 

                                          where indid in (0, 1, 255) 

                            )) / @pagesperMB,15,2)+ ' MB') 

       print ' ' 

       /* 

       **  Now calculate the summary data. 

       **  reserved: sum(reserved) where indid in (0, 1, 255) 

       */ 

       insert into #spt_space (reserved) 

              select sum(convert(dec(15),reserved)) 

                     from sysindexes 

                            where indid in (0, 1, 255) 

       /* 

      ** data: sum(dpages) where indid < 2 

       **    + sum(used) where indid = 255 (text) 

       */ 

       select @pages = sum(convert(dec(15),dpages)) 

                     from sysindexes 

                            where indid < 2 

       select @pages = @pages + isnull(sum(convert(dec(15),used)), 0) 

              from sysindexes 

                     where indid = 255 

       update #spt_space 

              set data = @pages 

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

       update #spt_space 

              set indexp = (select sum(convert(dec(15),used)) 

                            from sysindexes 

                                   where indid in (0, 1, 255)) 

                         - data 

       /* unused: sum(reserved) - sum(used) where indid in (0, 1, 255) */ 

       update #spt_space 

              set unused = reserved 

                            - (select sum(convert(dec(15),used)) 

                                   from sysindexes 

                                          where indid in (0, 1, 255)) 

       select 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' 

end 

/* 

**  We want a particular object. 

*/ 

else 

begin 

       /* 

       **  Now calculate the summary data. 

       **  reserved: sum(reserved) where indid in (0, 1, 255) 

       */ 

       insert into #spt_space (reserved) 

              select sum(reserved) 

                     from sysindexes 

                            where indid in (0, 1, 255) 

                                   and id = @id 

       /* 

      ** data: sum(dpages) where indid < 2 

       **    + sum(used) where indid = 255 (text) 

       */ 

       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 

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

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

       update #spt_space 

              set rows = i.rows 

                     from sysindexes i 

                            where i.indid < 2 

                                   and i.id = @id 

        insert into #temp1 

       select name = object_name(@id), 

              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' 

Drop table #spt_space 

end 

fetch next from cur_table into @objname 

end 

Close cur_table 

DEALLOCATE cur_table 

Select * from #temp1 order by len(数据使用空间) desc,数据使用空间 desc,保留空间 desc 

Drop table #temp1 

return (0) 

end 

GO 

更多精彩内容其他人还在看

浅析SQL Server 聚焦索引对非聚集索引的影响

本篇文章对SQL Server的聚焦索引和非聚集索引进行简单分析,从而总结出聚焦索引对非聚集索引的影响。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅析SQL Server的聚焦使用索引和查询执行计划

本文通过介绍默认使用索引、强制使用聚集索引、强制使用非聚集索引让我们知道对于检索所有列结果集使用主键的聚集索引是最佳选择。有兴趣的朋友可以看下
收藏 0 赞 0 分享

详解SQL Server中的数据类型

本文主要讲解了SQL中的数据类型以及几个需要注意的地方,简短的内容,深入的理解。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅析SQL Server的分页方式 ISNULL与COALESCE性能比较

本文上述重点讲述了SQL Server的分页方式,COALESCE和ISNULL函数区别之处,简短的内容,深入的理解。有兴趣的朋友可以看下
收藏 0 赞 0 分享

浅述SQL Server的聚焦强制索引查询条件和Columnstore Index

本文主要讲了强制使用索引条件来进行查询,当对于使用默认创建索引进行查询计划时觉得不是最优解,可以尝试使用强制索引来进行对比找出更好得解决方案。简短的内容,深入的理解.有兴趣的朋友可以看下
收藏 0 赞 0 分享

详解SQL Server的聚焦过滤索引

本文主要讲解了通过过滤索引来提高查询性能,同时也给出了其不同的场景以及其使用优点和明显的缺点。简短的内容,深入的理解,有兴趣的朋友可以看下
收藏 0 赞 0 分享

解析SQL Server聚焦移除(Bookmark Lookup、RID Lookup、Key Lookup)

本文主要讲解索引性能优化,着重对Bookmark Lookup、RID Lookup、Key Lookup三者进行移除的实现进行解析,以此来提高查询性能。希望对大家有所帮助
收藏 0 赞 0 分享

SqlServer 注释符 单行注释与多行注释

这篇文章主要介绍了SqlServer 注释符 单行注释与多行注释,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL设置SQL Server最大连接数及查询语句

今天遇到了关于Sql Server最大连接数(Max Pool Size)的问题,后来通过查找一些资料解决了,所以想着总结下关于SQL Server最大连接数的内容,所以这篇文章主要介绍了SQL设置SQL Server最大连接数与查询语句,有需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

浅谈SQL Server交叉联接 内部联接

本文主要讲了SQL Server的交叉联接和内部联接,同时也给出了使用需要注意的地方。有需要的朋友可以看下
收藏 0 赞 0 分享
查看更多