【问题标题】:I need to know how much disk space a table is using in SQL Server我需要知道一个表在 SQL Server 中使用了多少磁盘空间
【发布时间】:2010-09-05 09:17:29
【问题描述】:

我认为大多数人都知道如何通过 GUI(右键单击表、属性)来执行此操作,但在 T-SQL 中执行此操作完全令人费解。

【问题讨论】:

    标签: sql-server


    【解决方案1】:

    sp_spaceused 表名

    其中 tableName 是您想知道的表的名称。

    【讨论】:

      【解决方案2】:

      看看这个,我知道它在 2005 年有效 (Microsoft Documentation):

      这是 pubs DB

      
      select *
      from pubs.sys.database_files
      

      返回大小和 max_size。

      【讨论】:

        【解决方案3】:
        创建表#tmpSizeChar ( 表名系统名, row_count 整数, reserved_size varchar(50), 数据大小 varchar(50), index_size varchar(50), 未使用大小 varchar(50)) 创建表#tmpSizeInt ( 表名系统名, row_count 整数, reserved_size_KB int, data_size_KB 整数, index_size_KB 整数, 未使用的大小 KB 整数) 设置无计数 插入 #tmpSizeChar EXEC sp_msforeachtable 'sp_spaceused ''?''' 插入 #tmpSizeInt ( 表名, 行数, reserved_size_KB, data_size_KB, index_size_KB, 未使用大小 KB ) 选择 [table_name], 行数, CAST(SUBSTRING(reserved_size, 0, PATINDEX('% %', reserved_size)) AS int)reserved_size, CAST(SUBSTRING(data_size, 0, PATINDEX('% %', data_size)) AS int)data_size, CAST(SUBSTRING(index_size, 0, PATINDEX('% %', index_size)) AS int)index_size, CAST(SUBSTRING(unused_size, 0, PATINDEX('% %', used_size)) AS int)unused_size 来自#tmpSizeChar /* 删除表#tmpSizeChar 删除表#tmpSizeInt */ 选择 * 从 #tmpSizeInt ORDER BY reserved_size_KB DESC

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-10
          • 1970-01-01
          • 2011-05-12
          • 2014-06-29
          • 1970-01-01
          • 2014-10-06
          • 2015-05-11
          • 2021-06-14
          相关资源
          最近更新 更多