PostgreSQL - 键索引

PostgreSQL - index on key

我想知道,如果您在 is/are 已经被唯一约束覆盖的 column/columns 上创建索引,会有什么(副作用)影响。

drop table if exists person;
create table person
(
    a   integer     not null,
    b   integer     not null,

    unique(a,b)
);

这里,在a,b上设置了唯一键约束。我知道这在内部产生了两个索引:一个在 a 上,另一个在 a,b.

现在我创建两个索引:

create index on person(a);
create index on person(a,b);

这个有什么作用?

I understood that this internally makes two indices: one on a and another on a,b.

不,创建了 (a,b) 上的唯一索引。创建两个索引后,您将拥有三个索引:

"person_a_b_key" UNIQUE CONSTRAINT, btree (a, b)
"person_a_b_idx" btree (a, b)
"person_a_idx" btree (a)

第一个和第二个重叠,其中一个不会被使用。

I understood that this internally makes two indices: one on a and another on a,b

不,这个 (unique(a,b)) 在内部只创建一个索引 - (a,b) 上的第二个索引

create index on person(a,b)的副作用而言。 "only" 冗余将:

  • 降低维护索引的操作的性能,例如 INSERTUPDATEREINDEXVACUUM FULL
  • 增加实体使用的磁盘space(索引是必须存储在磁盘上的数据结构)
  • 实际上没有任何收获,因为查询规划器可能会根据每次执行使用一个或另一个索引