Re: Query optimization and indexes - Mailing list pgsql-general

From Tom Lane
Subject Re: Query optimization and indexes
Date
Msg-id 26398.1155946547@sss.pgh.pa.us
Whole thread Raw
In response to Query optimization and indexes  (felix@crowfix.com)
Responses Re: Query optimization and indexes  (Gregory Stark <gsstark@mit.edu>)
List pgsql-general
felix@crowfix.com writes:
> Suppose I have an index on 5 columns (A, B, C, D, E).
> If my WHERE clause is not in that order, will the optimizer reorder
> them as necessary and possible?

Yes, the optimizer understands about commutativity/associativity of
AND and OR ;-)

> If I don't specify columns in the WHERE clause, how much can it use
> the index?

Before (if memory serves) 8.1, the planner would only consider leading
index columns as potential indexscan qualifiers.  So given

    where a = 5 and c = 4;

only the a = 5 clause would be used with the index.  As of 8.1 it will
consider using nonconsecutive index columns, but if you think for a bit
about the storage order of a btree, you'll realize that you really need
leading columns to keep down the amount of the index that gets scanned.
A lot of the time, such a plan will be rejected as apparently more
expensive than a seqscan.

(This is for btrees, I don't recall the state of play for GIST indexes
exactly.)

            regards, tom lane

pgsql-general by date:

Previous
From: felix@crowfix.com
Date:
Subject: Query optimization and indexes
Next
From: "Merlin Moncure"
Date:
Subject: Re: Inserting Data