gpt4 book ai didi

sql - 同一张表的同一列上的 2 个 PostgreSQL 索引 - 多余?

转载 作者:太空狗 更新时间:2023-10-30 01:48:09 27 4
gpt4 key购买 nike

我有一个带有 2 个索引的 PostgreSQL 表。其中一个索引涵盖了 website_idtweet_id 列,是一个唯一的 B 树索引。第二个索引只覆盖 website_id 列,是一个非唯一索引。

如果第一个索引存在,第二个索引是否冗余?换句话说,有第二个索引就没有优势了吗?

最佳答案

postgres 多列索引只能用于搜索第一列,所以在实践中它是多余的。

A multicolumn B-tree index can be used with query conditions that involve any subset of the index's columns, but the index is most efficient when there are constraints on the leading (leftmost) columns. The exact rule is that equality constraints on leading columns, plus any inequality constraints on the first column that does not have an equality constraint, will be used to limit the portion of the index that is scanned.

Postgres 9.2 documentation

有一个遥远的情况,其他索引可能有用(请参阅下面的更多详细信息),即。如果您在第一个索引上执行大部分查询并且索引可用的缓存非常小。在这种情况下,组合索引可能不适合缓存,但较小的单列可以。

https://dba.stackexchange.com/questions/27481/is-a-composite-index-also-good-for-queries-on-the-first-field/27493#27493

关于sql - 同一张表的同一列上的 2 个 PostgreSQL 索引 - 多余?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17479357/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com