PostgreSQL复合主键 [英] PostgreSQL composite primary key

查看:289
本文介绍了PostgreSQL复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中,当我创建复合主键时,比如列 X,Y,Z ,那么所有三列都会自动成为索引。 Postgres会发生同样的情况吗?

In MySQL, when I create a composite primary key, say with columns X, Y, Z, then all three columns become indexes automatically. Does the same happen for Postgres?

推荐答案

如果您创建复合主键,则在(x, y,z),PostgreSQL在(x,y)上的一个 UNIQUE 多列btree索引的帮助下实现了这一点,z)。此外,所有三列都必须是 NOT NULL ,当然,这是 PRIMARY KEY 之间的主要区别和 UNIQUE INDEX

If you create a composite primary key, on (x, y, z), PostgreSQL implements this with the help of one UNIQUE multi-column btree index on (x, y, z). In addition, all three columns have to be NOT NULL, of course, which is the main difference between a PRIMARY KEY and a UNIQUE INDEX.

除了对数据的明显限制外,多列索引对查询性能的影响也比三个人有所不同 x y z 的索引。

Besides the obvious restrictions on your data, the multi-column index also has a somewhat different effect on the performance of queries than three individual indexes on x, y and z.

我们对最近关于dba.SE的相关内容进行了非常详尽的讨论。问题。有关 即将推出的功能的示例,基准,讨论和展望仅版本9.2中的仅索引扫描

特别是上的主键(x,y,z )将加速查询条件 x (x,y)(x,y,z)最佳。它还有助于查询 y z (y,z)(x,z),但程度要小得多。

In particular, a primary key on (x, y, z) will speed up queries with conditions on x, (x,y) or (x,y,z) optimally. It will also help with queries on y, z, (y,z) or (x,z) but to a far lesser extent.

如果你需要加快对后一种组合的查询,您可能希望更改PK约束中的列顺序或创建一个或多个其他索引。

If you need to speed up queries on the latter combinations, you may want to change the order of column in your PK constraint or create one or more additional indexes.

这篇关于PostgreSQL复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆