如何查找 SQL Server 数据库中所有空间的使用情况

2024-05-08

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

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

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


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

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何查找 SQL Server 数据库中所有空间的使用情况 的相关文章

随机推荐