GO
/****** Object: StoredProcedure [dbo].[SpaceUsed] Script Date: 2017-12-01 11:15:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[sqlTool_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

SQL Server 数据库空间使用情况的更多相关文章

  1. SQL Server数据库空间管理 (1)

    数据库经常遇到的问题: 1).数据库文件空间用尽  2).日志文件不停增长 3).数据库文件无法收缩  4).自动增长和自动收缩 本系列就以上面的4个问题入手分析并总结数据库空间的管理方法.   1. ...

  2. SQL Server 查看空间使用情况的 5 种方法

    解决方法: 方法 1.sp_spaceused 方法 2.dbcc sqlperf 方法 3.dbcc showfilestats 方法 4.dbcc showcontig 方法 5.sys.dm_d ...

  3. SQL Server数据库空间管理 (2)

     本篇内容主要解决剩余的两个问题:2).日志文件不停增长  4).自动增长和自动收缩 日志文件不停增长的解决 首先,当日志文件超过预期的时候,我们然要看看日志文件中存放了什么内容:DBCC LOG ; ...

  4. MS SQL Server数据库修复/MDF数据文件数据恢复/MDF质疑/mdf无法附加

    微软的SQL Server 数据库最常用的有两种类型的文件: 1.主要数据文件,文件后缀一般是.MDF: 2.事务日志文件,文件后缀一般是.LDF. 用户数据表.视图.存储过程等等数据,都是存放在MD ...

  5. 3. SQL Server数据库状态监控 - 可用空间

    原文:3. SQL Server数据库状态监控 - 可用空间 数据库用来存放数据,那么肯定需要存储空间,所以对磁盘空间的监视自然就很有必要了. 一. 磁盘可用空间 1. 操作系统命令或脚本.接口或工具 ...

  6. Python监控SQL Server数据库服务器磁盘使用情况

    本篇博客总结一下Python采集SQL Server数据库服务器的磁盘使用信息,其实这里也是根据需求不断推进演化的一个历程,我们监控服务器的磁盘走了大概这样一个历程: 1:使用SQL Server作业 ...

  7. Sql Server数据库备份和恢复:原理篇

    本文与您探讨为什么Sql Server有完整备份.差异备份和事务日志备份三种备份方式,以及为什么数据库又有简单模式.完整模式和大容量日志模式这三种恢复模式.本文内容适用于2005以上所有版本的Sql ...

  8. SQL SERVER 数据库备份的三种策略及语句

    1.全量数据备份    备份整个数据库,恢复时恢复所有.优点是简单,缺点是数据量太大,非常耗时 全数据库备份因为容易实施,被许多系统优先采用.在一天或一周中预定的时间进行全数据库备份使你不用动什么脑筋 ...

  9. SQL Server数据库性能优化之SQL语句篇【转】

    SQL Server数据库性能优化之SQL语句篇http://www.blogjava.net/allen-zhe/archive/2010/07/23/326927.html 近期项目需要, 做了一 ...

随机推荐

  1. javascript中闭包最简单的简绍

    javascript中闭包是什么 JavaScript 变量可以是局部变量或全局变量.私有变量可以用到闭包.闭包就是将函数内部和函数外部连接起来的一座桥梁. 函数的闭包使用场景:比如我们想要一个函数来 ...

  2. 【HTML&CSS】基本的入门

    在公司培训一段时间不久就去流浪了一段时间,现在回来重新捧起心爱的编程,特别亲切. 自学HTML&CSS,别人说了很多,这那这那的,无论简单还是困难,不亲自去俯下身子学习,怎么都学不会HTML和 ...

  3. H5实现拍照上传功能

    <input type="file" capture="camera" accept="image/*" >

  4. sublime text3 jQuery Emmet 插件 安装方法,快捷键

    preference->package control->install package> emmet / jQuery 先说jQuery jQuery 集成了很多JS的补全功能.例 ...

  5. 解决servlet在web.xml中的路径跳转问题

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" ...

  6. 线程Event事件

    事件(event) 事件是不同线程之间的同步对象 enent可以通过设置.等待.清除一个标识(flag),来进行线程间的控制 线程可以通过获取这个标志位(flag)的状态(设置或未设置)来控制线程 事 ...

  7. [Swift] 使用Playground

    使用Playground 1. 新建Playground 2. 写最简单的代码

  8. python错误和异常(1)

    1  SyntaxError: Python解释器语法错误,是唯一不是在运行时发生的异常. 2 检测和处理异常 try语句有两种主要形式:try-except 和 try-finally. 只能二选一 ...

  9. Python学习---生成器的学习1210

    在Python中,这种一边循环一边计算的机制,称为生成器: 结论: 生成器本质是一个函数,不同于函数的是它生成的是一个对象,不执行函数内的代码 1.1. 列表生成器 列表生成器: 列表是直接生成数字在 ...

  10. 沉淀再出发:ElasticSearch的中文分词器ik

    沉淀再出发:ElasticSearch的中文分词器ik 一.前言   为什么要在elasticsearch中要使用ik这样的中文分词呢,那是因为es提供的分词是英文分词,对于中文的分词就做的非常不好了 ...