Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use hash or btree for a foreign key index in postgresql 9.3?

Which index will perform better for foreign keys of type integer in postgresql 9.3?

I would assume a hash index, because foreign key comparisson are always made with =

Or does a btree compare as fast as a hash when used for JOINS on foreign keys?

Because in postgresql primary keys use btree's that would suggest they are also better for foreign keys.

like image 211
frasen Avatar asked Oct 10 '14 10:10

frasen


People also ask

Is hash index faster than btree?

In this run we have changed hash to btree index in pgbench read-only tests. We can see here that the PostgreSQL hash index performs better than the btree index and the performance difference is in the range of 10% to 22%.

What is the advantage of a hash index over a B-tree index in PostgreSQL?

A PostgreSQL Hash index can perform a faster lookup than a B-Tree index. However, the key downside of the Hash index is that its use is limited to equality operators that will perform matching operations.

Should I create index on foreign key Postgres?

While it's usually a good idea to create an index on (or including) your referencing-side foreign key columns, it isn't required. Each index you add slows DML operations down slightly, so you pay a performance cost on every INSERT , UPDATE or DELETE . If the index is rarely used it may not be worth having.

What is Btree index in PostgreSQL?

PostgreSQL B-Tree indexes are multi-level tree structures, where each level of the tree can be used as a doubly-linked list of pages. A single metapage is stored in a fixed position at the start of the first segment file of the index. All other pages are either leaf pages or internal pages.


2 Answers

From the manual On PostgreSQL 9.3:

Caution Hash index operations are not presently WAL-logged, so hash indexes might need to be rebuilt with REINDEX after a database crash if there were unwritten changes. Also, changes to hash indexes are not replicated over streaming or file-based replication after the initial base backup, so they give wrong answers to queries that subsequently use them. For these reasons, hash index use is presently discouraged.

There is also no proof that an hash index has any performance benefits over a btree.

like image 131
Frank Heikens Avatar answered Oct 01 '22 22:10

Frank Heikens


It depends.

If you're not doing any queries using the foreign key, then you don't need any index. The referential integrity is enforced using the index of the referenced primary key.

The question of which index type to use (if any) is thus the same for any column(s). If your queries would benefit from an index for a = comparison, and you're using PostgreSQL 10 or newer, then a HASH index is a reasonable choice. If the same column is involved in any ordering operations (ORDER BY, <, >=, etc.) then you may as well use a BTREE.

If you are concerned about the relative performance, then you'll need to test them yourself, using your own data distribution and query load. A tree index may still perform better than a hash, due to data access locality (sequential vs. random).

like image 42
OrangeDog Avatar answered Oct 02 '22 00:10

OrangeDog