HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Can postgres make use of both GIN and BTREE indexes in a single query?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canpostgresquerymakeginindexesbothbtreesingleand

Problem

Let's say I have GIN index on column A that allows me to filter table fast on that column by appropriate WHERE condition which would be slow without that index. However I would like also to order filtered rows by using ORDER BY statement on, let's say column B. Ordering without index is slow, so I use BTREE index on column B. The problem is that pg don't want to use both indexes - if both WHERE cond on column A and ORDER BY B are present in query, pg uses only GIN index. When I remove WHERE cond from query, then it uses BTREE index on col B.

What I can do to force pg to use both indexes, one to filter rows and second one to order them?

Solution

To answer your title question, PostgreSQL can make use of multiple indexes for a "bitmap" scan, but only with Boolean logic. It can't use one to order and the other to filter within the same table scan. It is an interesting idea, though.

Context

StackExchange Database Administrators Q#120237, answer score: 3

Revisions (0)

No revisions yet.