索引中的列顺序在选择过程中重要吗?

问题描述 投票:0回答:1

我很想知道我们选择列的顺序是否会影响索引的性能。 假设我在 postgres 表上有一个索引

constraint domain_index unique(username, firstname, country)

选择查询的性能会有什么不同吗?

  1. Select username, firstname, country from table
  2. Select country, firstname, username from table
postgresql select indexing
1个回答
0
投票

不,列的顺序对于没有

WHERE
条件或
ORDER BY
子句的仅索引扫描的性能并不重要。真正重要的是桌子被吸尘的频率(越频繁越好)。

如果您仅对最小的列进行索引并将其他列放入

INCLUDE
子句中,则可以减小索引的大小(从而提高性能):

CREATE INDEX ON tab (username) INCLUDE (country, firstname);
© www.soinside.com 2019 - 2024. All rights reserved.