我很想知道我们选择列的顺序是否会影响索引的性能。 假设我在 postgres 表上有一个索引
constraint domain_index unique(username, firstname, country)
选择查询的性能会有什么不同吗?
Select username, firstname, country from table
Select country, firstname, username from table
不,列的顺序对于没有
WHERE
条件或 ORDER BY
子句的仅索引扫描的性能并不重要。真正重要的是桌子被吸尘的频率(越频繁越好)。
如果您仅对最小的列进行索引并将其他列放入
INCLUDE
子句中,则可以减小索引的大小(从而提高性能):
CREATE INDEX ON tab (username) INCLUDE (country, firstname);