如何确定是否使用压缩创建了 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 中没有看到任何关于该压缩的记录?
- 我在对象资源管理器的上下文菜单的 Table、列或键属性对话框中找不到它。
- 如果我要求 SSMS 编写脚本 => 创建密钥,则它不包括
DATA_COMPRESSION
设置。
我得到:
/****** 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.indexes
和 sys.partitions
对象中找到:
Metadata
The following system views contain information about data compression for clustered indexes:
- sys.indexes (Transact-SQL) - The type and type_desc columns include CLUSTERED COLUMNSTORE and NONCLUSTERED COLUMNSTORE.
- sys.partitions (Transact-SQL) - The data_compression and data_compression_desc columns include COLUMNSTORE and COLUMNSTORE_ARCHIVE.
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
考虑以下 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 中没有看到任何关于该压缩的记录?
- 我在对象资源管理器的上下文菜单的 Table、列或键属性对话框中找不到它。
- 如果我要求 SSMS 编写脚本 => 创建密钥,则它不包括
DATA_COMPRESSION
设置。 我得到:
/****** 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.indexes
和 sys.partitions
对象中找到:
Metadata
The following system views contain information about data compression for clustered indexes:
- sys.indexes (Transact-SQL) - The type and type_desc columns include CLUSTERED COLUMNSTORE and NONCLUSTERED COLUMNSTORE.
- sys.partitions (Transact-SQL) - The data_compression and data_compression_desc columns include COLUMNSTORE and COLUMNSTORE_ARCHIVE.
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