在 MSSQL 中检索列存储索引的正确磁盘空间使用情况

Posted

技术标签:

【中文标题】在 MSSQL 中检索列存储索引的正确磁盘空间使用情况【英文标题】:Retrieve the correct disk space usage of a columnstore index in MSSQL 【发布时间】:2021-07-02 14:19:50 【问题描述】:

我试图检索 MSSQL 中聚集列存储索引所需的确切字节数。 不幸的是,与对 sys.allocation_units 的简单查询相比,我从 sp_spaceused 得到的结果不同。

这里是简单的脚本:

-- create table
CREATE TABLE TestColumnStore (Id INT IDENTITY(1,1) NOT NULL, Val VARCHAR(128));
CREATE CLUSTERED COLUMNSTORE INDEX [IX_TestColumnStore] ON [TestColumnStore];
GO
-- append 1000 rows
DECLARE @i INT = 0;
WHILE @i < 1000
    BEGIN
    INSERT INTO dbo.TestColumnStore (Val) VALUES ('Jhon');
    SET @i = @i+1;
    END

这里是sp_spaceused 结果:

name             rows       reserved    data          index_size         unused
---------------- ---------- ----------- ------------- ------------------ ------------------
TestColumnStore  1000       144 KB      48 KB         16 KB              80 KB

然后我尝试使用此查询直接获取sys.allocation_units查询的占用页面数量:

SELECT 
    al.total_pages * 8                       AS TotalSpaceKB,
    al.used_pages * 8                        AS UsedSpaceKB,
    (al.total_pages - al.used_pages) * 8 AS UnusedSpaceKB,
    al.allocation_unit_id,
    p.partition_id,
    o.[name],
    al.type_desc,
    p.data_compression_desc
FROM
    sys.allocation_units al
    JOIN sys.partitions p ON al.container_id = p.partition_id
    LEFT JOIN sys.objects o ON o.object_id = p.object_id
WHERE
    o.name = 'TestColumnStore'

当然,结果显然正是 sp_spaceused 返回的一半。

TotalSpaceKB         UsedSpaceKB          UnusedSpaceKB        allocation_unit_id   partition_id         name                  type_desc          data_compression_desc
-------------------- -------------------- -------------------- -------------------- -------------------- --------------------- ------------------ ----------------------
0                    0                    0                    72057594050183168    72057594043629568    TestColumnStore       IN_ROW_DATA        COLUMNSTORE
72                   16                   56                   72057594050248704    72057594043629568    TestColumnStore       LOB_DATA           COLUMNSTORE

谁能帮我弄清楚我错过了什么?获取确切的表大小(以字节为单位)的正确方法是什么?

【问题讨论】:

我发现我还必须包含 sys.column_store_row_groups 中指定的分配单元。 delta_store_hobt_id 字段。 您应该将其发布为答案,欢迎您回答自己的问题。添加完整的查询以获得结果 【参考方案1】:

这是返回与 sp_spaceused 完全相同大小的最终查询:

SELECT 
    SUM(TotalSpaceKB) TotalSpaceKB,
    SUM(UsedSpaceKB) UsedSpaceKB,
    SUM(UnusedSpaceKB) UnusedSpaceKB
FROM (
    SELECT 
        al.total_pages * 8                       AS TotalSpaceKB,
        al.used_pages * 8                        AS UsedSpaceKB,
        (al.total_pages - al.used_pages) * 8 AS UnusedSpaceKB,
        al.allocation_unit_id,
        p.partition_id,
        o.[name],
        al.type_desc,
        p.data_compression_desc
    FROM
        sys.allocation_units al
        JOIN sys.partitions p ON al.container_id = p.partition_id
        JOIN sys.objects o ON o.object_id = p.object_id
    WHERE
        o.name = 'TestColumnStore'
    UNION
    SELECT 
        al.total_pages * 8                       AS TotalSpaceKB,
        al.used_pages * 8                        AS UsedSpaceKB,
        (al.total_pages - al.used_pages) * 8 AS UnusedSpaceKB,
        al.allocation_unit_id,
        NULL AS partition_id,
        o.[name],
        al.type_desc,
        NULL AS data_compression_desc
    FROM
        sys.column_store_row_groups csrg 
        JOIN sys.allocation_units al ON al.container_id = csrg.delta_store_hobt_id
        JOIN sys.objects o ON csrg.object_id = o.object_id
    WHERE
        o.name = 'TestColumnStore'
) a
GROUP BY a.name

【讨论】:

以上是关于在 MSSQL 中检索列存储索引的正确磁盘空间使用情况的主要内容,如果未能解决你的问题,请参考以下文章

Innodb索引

MSSQL ColumnStore 索引 - 常规索引的使用

mssql数据库在磁盘阵列运行慢

es关闭索引

数据库索引是不是与列数据占用相同数量的磁盘空间?

数据库索引 随便吹