sql查看所有表大小的方法

时间:2024-01-24 04:42:13 


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

标签:sql
0
投稿

猜你喜欢

  • 通过java备份恢复mysql数据库的实现代码

    2024-01-25 23:44:58
  • sql语言中delete删除命令语句详解

    2024-01-28 02:20:02
  • 如何实现在下拉菜单里输入文字?

    2010-06-03 10:31:00
  • php中json 序列化为 [] 的弊端

    2023-05-25 00:14:30
  • 一篇文章弄懂PHP和HTML的嵌套写法

    2023-06-20 04:51:22
  • Python爬虫之网页图片抓取的方法

    2021-12-19 00:47:20
  • Python开根号的几种方式详解

    2021-07-10 20:37:34
  • React开发进阶redux saga使用原理详解

    2023-08-06 09:08:33
  • 基于Python实现的恋爱对话小程序详解

    2023-01-14 11:08:38
  • Go语言写入字符串到文件的方法

    2024-05-21 10:21:54
  • 基于mysq字段选择的详解

    2024-01-23 20:11:20
  • 获取MSSQL数据字典的SQL语句

    2024-01-20 11:35:16
  • python接入使用百度翻译流程

    2022-11-26 01:01:43
  • 详解Go语言中结构体与JSON间的转换

    2024-04-28 09:10:50
  • Python实现的查询mysql数据库并通过邮件发送信息功能

    2024-01-21 11:51:36
  • Python中的__new__与__init__魔术方法理解笔记

    2021-12-18 14:29:26
  • python设置值及NaN值处理方法

    2022-11-30 01:48:42
  • 地图网站的需求功能与体验

    2009-03-01 11:15:00
  • django中的图片验证码功能

    2022-06-10 00:07:54
  • Golang爬虫框架colly使用浅析

    2024-02-06 23:28:06
  • asp之家 网络编程 m.aspxhome.com