zoukankan      html  css  js  c++  java
  • SQL中查看数据库各表的大小

    SQL中查看数据库各表的大小

    编写人:CC阿爸

     

    2014-6-17

     

    在日常SQL数据库的操作中,如何快速的查询数据库中各表中数据的大小。

    以下有两种方法供参考:

    第一种:

    create table #t(name varchar(255), rows bigint, reserved varchar(20), data varchar(20), index_size varchar(20), unused varchar(20))

    exec sp_MSforeachtable "insert into #t exec sp_spaceused '?'"

    select * from #t order by rows desc

     

    select 'select * from '+name  from #t order by rows desc

     

    drop table #t

     

    第二种:

    CREATE PROCEDURE get_tableinfo AS

     

    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') =

         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

     

    ----执行存储过程

    --exec get_tableinfo

    --

    --查询运行该存储过程后得到的结果

    select * from tablespaceinfo 

    order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

  • 相关阅读:
    Android中Bitmap对象和字节流之间的相互转换
    Android 6.0以后的版本报错:open failed: EACCES (Permission denied)
    C#—ASP.NET:集成极光推送(Push API v3)
    极光推送(C#)
    模仿今日头条导航栏滑动显示更多
    使用VMWare虚拟mac系统,设置网络的正确姿势
    vmware panic(CPU 0 caller 0x)launchd exited
    VMware15安装MAC(MAC OS 10.13)(OS X 10.14)原版可升级最新可解锁macOS Unlocker3.0(OS X 10.13)
    Flutter Dart List.map() 获取下标
    Flutter利用GridView实现网格的商品布局
  • 原文地址:https://www.cnblogs.com/bribe/p/3791704.html
Copyright © 2011-2022 走看看