同一個表的同一列上的2個PostgreSQL索引 - 多余?

[英]2 PostgreSQL indices on the same column of the same table - redundant?


I have a PostgreSQL table with 2 indices. One of the indices is covers the website_id and tweet_id columns and is an unique B-tree index. The 2nd index only covers the website_id column, and is a non-unique index.

我有一個帶有2個索引的PostgreSQL表。其中一個索引涵蓋了website_id和tweet_id列,是一個獨特的B樹索引。第二個索引僅涵蓋website_id列,並且是非唯一索引。

Is the 2nd index redundant if the first index exists? In other words, will there be no advantages to having the 2nd index?

如果第一個索引存在,第二個索引是多余的嗎?換句話說,擁有第二個指數是沒有優勢的嗎?

3 个解决方案

#1


9  

postgres multicolumn indexes can be used to search on the first columns only,so in practise it is redundant.

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.

多列B樹索引可以與涉及索引列的任何子集的查詢條件一起使用,但是當前導(最左側)列存在約束時,索引最有效。確切的規則是對前導列的等式約束以及第一列上沒有等式約束的任何不等式約束將用於限制掃描的索引部分。

Postgres 9.2 documentation

Postgres 9.2文檔

there is a remote case where the other index might be useful (see below for more detailed stuff) ie. If you do most of your queries on the first index and have a very small cache available for the indexes. In this case the combined index might not fit the cache , but the smaller single column one would.

有一個遠程情況,其他索引可能是有用的(見下面更詳細的東西),即。如果您對第一個索引執行大多數查詢,並且索引的可用緩存非常小。在這種情況下,組合索引可能不適合緩存,但是單個列會更小。

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

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

#2


5  

It depends.

這取決於。

Assuming we are talking about default B-Tree indexes only. If other index types like GIN or GiST are involved, things are not as simple.

假設我們只討論默認的B-Tree索引。如果涉及其他索引類型,如GIN或GiST,事情並不那么簡單。

In principal an index on (a,b) is good for searches on just a and another index on just (a) is not needed. (But an additional index on just (b) generally makes sense!)
It may still be a good idea if the column b is big, so that an index on just (a) is substantially smaller.

原則上,(a,b)上的索引對於僅在a上的搜索是有利的,而在(a)上不需要另一個索引。 (但是僅僅(b)的附加索引通常是有意義的!)如果列b很大,那么可能仍然是一個好主意,因此只有(a)的索引要小得多。

You would have to consider the size of the table, available RAM, typical queries, the involved data types, the size of the index, overhead per tuple and size of data, data alignment and padding ... or just run tests with your actual data and queries (but careful what you are testing really).

您將不得不考慮表的大小,可用RAM,典型查詢,涉及的數據類型,索引的大小,每個元組的開銷和數據大小,數據對齊和填充......或者只是用您的實際運行測試數據和查詢(但要小心你正在測試的內容)。

For example if a and b are no bigger than 4 bytes (integer, smallint, date, ...) the index on (a,b) is exactly as big as the one on just (a) and there is no point whatsoever to keep the second.

例如,如果a和b不大於4個字節(整數,小寫,日期......),(a,b)上的索引與(a)上的索引一樣大,並且沒有任何意義保持第二。

A more detailed answer on dba.SE for this case exactly.

關於dba.SE的更詳細的答案。

The manual for the current version of Postgres is always a good source for more detailed information.

Postgres的當前版本的手冊始終是更詳細信息的良好來源。

#3


1  

Yes, it is (redundant).

是的,它是(冗余的)。

The compound index behavior is common not only to Postgres but to pretty much any other RDBMS.

復合索引行為不僅對Postgres很常見,而且幾乎與其他任何RDBMS一樣。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/07/04/725d5a1a3c99eef83b25265065ec467d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com