集群键上升到 SQL 服务器中具有非集群索引的树

Clustering key goes up to tree with non-clustered index in SQL Server

似乎在 SQL 2019 版本之前的服务器中,聚类 key/keys 上升到树结构,没有唯一的非聚簇索引。使用更大的多聚类key/keys,你会得到更宽更高的树,这会花费更多的存储空间和内存。

因此,我们曾经将 PK 与聚集键分开,我的问题是

  1. SQL Server 2019 和 Azure 在非聚集索引方面有没有变化?
  2. Heaps根本没有聚簇key/keys,heaps的索引方式是什么?

Have SQL Server 2019 and Azure changed in non-clustered indexing or not

这种行为比这个网站上的许多人都要老。

Because of that we used to separate PK from clustered

这是一个几乎总是不必要的微优化。

Heaps do not have clustering key/keys at all, what's the way of indexing in heaps

非聚集非唯一索引始终将行定位器作为索引键。对于堆,行定位符是 ROWID(FileNo、PageNo、SlotNo)。

如果您想要将行从宽 PK 的叶级别移出,通常用于非常大的 table。因此,将行移动到聚簇列存储索引可能是一个不错的选择。为此,只需删除集群 PK(这会将叶级别保留为堆),创建 CCI,然后将 PK 重新创建为非集群 PK。例如

drop table if exists t
go
create table t(id int not null, a int, b int)

alter table t 
  add constraint pk_t 
  primary key clustered(id)

go

alter table t drop constraint pk_t

create clustered columnstore index cci_t on t

alter table t 
  add constraint pk_t 
  primary key nonclustered (id)

如果您有其他非聚集索引,请先删除它们,只有在确实需要时才重新创建它们。 IE 唯一索引、支持外键的索引或需要支持特定查询的索引。 CCI 通常不需要很多索引,因为它的扫描效率很高。