Re: Index not being used properly - Mailing list pgsql-general

From Tom Lane
Subject Re: Index not being used properly
Date
Msg-id 24255.1085119387@sss.pgh.pa.us
Whole thread Raw
In response to Index not being used properly  (Waldo Nell <pwnell@telkomsa.net>)
List pgsql-general
Waldo Nell <pwnell@telkomsa.net> writes:
> I have a *huge* problem.  I have a table with indexes on but the moment
> I have an OR in my SELECT query it seems to not use the appropriate
> index.

7.5 will be smarter about this, but in 7.4 and before you need to fool
with the column order of your indexes.  The query structure is basically

    WHERE col1 = const1 AND (col2 = const2 OR col2 = const3)

7.4 can turn this into a 2-column indexscan given an index on (col2,col1)
but not one on (col1,col2).

Your concern about the 3-column index can likewise be explained by
thinking about column order and how much of the index has to be scanned
for the given constraints.  Generally you want equalities for the
leftmost index columns and ranges for only the rightmost columns in
order to keep the index scan range reasonable.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Unable to run testlibpq.c program
Next
From: Tom Lane
Date:
Subject: Re: pg_temp_N temp schema buildup