【问题标题】:How to find where all of the space in a SQLServer database is being used如何查找 SQL Server 数据库中所有空间的使用位置
【发布时间】:2008-12-30 17:53:52
【问题描述】:

我们有一个 SQL Server 数据库,根据 Microsoft SQL Server Management Studio 的说法,在 6436Mb 中只有 119Mb 可用。

然而命令: EXEC sp_msforeachtable 'sp_spaceused ''?'''
显示总保留空间小于 2Gb

我们如何才能知道剩余空间的使用情况?

【问题讨论】:

    标签: sql-server sql-server-2005


    【解决方案1】:

    尝试运行我在我们的数据库上使用的这个脚本。这可能会为您提供更多信息。注意索引空间:

    CREATE TABLE #temp(
        rec_id      int IDENTITY (1, 1),
        table_name  varchar(128),
        nbr_of_rows int,
        data_space  decimal(15,2),
        index_space decimal(15,2),
        total_size  decimal(15,2),
        percent_of_db   decimal(15,12),
        db_size     decimal(15,2))
    
        -- Get all tables, names, and sizes
        EXEC sp_msforeachtable @command1="insert into #temp(nbr_of_rows, data_space, index_space) exec sp_mstablespace '?'",
                    @command2="update #temp set table_name = '?' where rec_id = (select max(rec_id) from #temp)"
    
        -- Set the total_size and total database size fields
        UPDATE #temp
        SET total_size = (data_space + index_space), db_size = (SELECT SUM(data_space + index_space) FROM #temp)
    
        -- Set the percent of the total database size
        UPDATE #temp
        SET percent_of_db = (total_size/db_size) * 100
    
        -- Get the data
        SELECT *
        FROM #temp
        ORDER BY nbr_of_rows DESC
    
        --select sum(nbr_of_rows) from #temp 
        --for xml auto
    
        -- Comment out the following line if you want to do further querying
        DROP TABLE #temp
    

    【讨论】:

      【解决方案2】:

      如果您使用安装了 Reporting Services 的 SQL Server 2005,您可以尝试运行名为“按表列出的磁盘使用情况”的内置报告。您可以在 Management Studio 中通过右键单击您的数据库并选择:Reports->Standard Reports->Disk Usage by Table 来访问它。

      此报告将为您提供以下信息: 表名, 记录数, 保留 (KB), 数据(KB), 索引 (KB), 未使用 (KB)

      【讨论】:

      • 看起来即使没有安装 SSRS,您也可以在 SSMS 2008 中运行这些报告 - 我只是针对非 SSRS 2005 服务器运行它,它们都运行良好。 +1 给你,先生!
      【解决方案3】:

      检查此脚本是否有 getting the free space for each database

      SELECT
              DB.name,
              MF.physical_name,
              MF.type_desc AS FileType,
              MF.size * 8 / 1024 AS FileSizeMB,
              fileproperty(MF.name, 'SpaceUsed') * 8/ 1024 AS UsedSpaceMB
      FROM
              sys.master_files MF
              JOIN sys.databases DB ON DB.database_id = MF.database_id
      ORDER BY
              DB.name,
              FileSizeMB DESC
      

      【讨论】:

        猜你喜欢
        • 2011-02-13
        • 2018-07-24
        • 1970-01-01
        • 2011-04-10
        • 1970-01-01
        • 2010-10-26
        • 1970-01-01
        • 2019-02-03
        • 1970-01-01
        相关资源
        最近更新 更多