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

Retrieve the correct disk space usage of a columnstore index in MSSQL

我试图检索 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

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

这是 returns 与 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