代码之家  ›  专栏  ›  技术社区  ›  Paul Hollingsworth

如何找到SQLServer数据库中所有空间的使用位置

  •  2
  • Paul Hollingsworth  · 技术社区  · 17 年前

    我们有一个SQL server数据库,根据Microsoft SQL server Management Studio,该数据库在6436Mb可用空间中只有119Mb。


    显示小于2Gb的总保留空间

    我们怎样才能知道剩余的空间在哪里被使用?

    3 回复  |  直到 17 年前
        1
  •  2
  •   nick_alot    17 年前

    试着运行我在数据库上使用的这个脚本。这可能会给你更多的信息。注意索引空间:

    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
  •  1
  •   Joe L.    17 年前

    如果您使用的是安装了Reporting Services的SQL Server 2005,则可以尝试运行名为“磁盘使用情况表”的内置报告。您可以在ManagementStudio中通过右键单击数据库并选择:Reports->标准报告->按表列出的磁盘使用情况。

    本报告将为您提供以下信息: 表名, 数据(KB), 索引(KB),

        3
  •  0
  •   bjnr    12 年前

    检查此脚本是否存在错误 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
    
    推荐文章