如何确定是否使用压缩创建了 SQL 键列?

How do I determine whether a SQL Key column was created with compression or not?

考虑以下 DDL:

ALTER TABLE dbo.MyTable
ADD CONSTRAINT [PK_MyKey] PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (STATISTICS_NORECOMPUTE=OFF, IGNORE_DUP_KEY=OFF, DATA_COMPRESSION=PAGE)
GO

这是一个压缩的主键。多田!

困扰我的是我在 SSMS 中没有看到任何关于该压缩的记录?

/****** Object:  Index [PK_MyKey]    Script Date: 07/09/2020 11:01:16 ******/
ALTER TABLE [dbo].[MyTable] ADD  CONSTRAINT [PK_MyKey] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF) ON [PRIMARY]
GO

所以...是否可以判断 现有 密钥(我没有创建,谁的创建不受版本控制 :叹息:) 被压缩了吗?

根据 documentation 这可以在 sys.indexessys.partitions 对象中找到:

Metadata

The following system views contain information about data compression for clustered indexes:

The procedure sp_estimate_data_compression_savings (Transact-SQL) can also apply to columnstore indexes.

对于以上,具体来说,你想看看专栏data_compression and/or data_compression_desc:

CREATE TABLE dbo.MyTable_Comp (ID int NOT NULL);
ALTER TABLE dbo.MyTable_Comp
ADD CONSTRAINT [PK_MyKey_C] PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (STATISTICS_NORECOMPUTE=OFF, IGNORE_DUP_KEY=OFF, DATA_COMPRESSION=PAGE)
GO

CREATE TABLE dbo.MyTable_NoComp (ID int NOT NULL);
ALTER TABLE dbo.MyTable_NoComp
ADD CONSTRAINT [PK_MyKey_NC] PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (STATISTICS_NORECOMPUTE=OFF, IGNORE_DUP_KEY=OFF);

GO

SELECT i.[name], p.[data_compression], p.data_compression_desc
FROM sys.indexes i
     JOIN sys.partitions p ON i.object_id = p.object_id
WHERE i.[name] IN ('PK_MyKey_C','PK_MyKey_NC');

GO
DROP TABLE dbo.MyTable_Comp;
DROP TABLE dbo.MyTable_NoComp

对于上面的2个表,这个returns如下:

name        | data_compression | data_compression_desc
------------|------------------|----------------------
PK_MyKey_C  | 2                | PAGE
PK_MyKey_NC | 0                | NONE