Re: Why does the query planner use two full indexes, when a dedicated partial index exists?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Richard Neill <rn214(at)richardneill(dot)org>
Cc: PostgreSQL Performance <pgsql-performance(at)postgresql(dot)org>
Subject: Re: Why does the query planner use two full indexes, when a dedicated partial index exists?
Date: 2012-12-27 17:05:18
Message-ID: 14671.1356627918@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

Richard Neill <rn214(at)richardneill(dot)org> writes:
> So, at the moment, I have 3 indexes:
> full: parcel_id_code
> full: exit_state
> full: parcel_id_code where exit state is null

> Am I right that when you suggest just a single, joint index
> (parcel_id_code,exit_state)
> instead of all 3 of the others,

I think he was just recommending replacing the first and third indexes.

> it will allow me to optimally run all of the following? :
> 1. SELECT * from tbl_tracker WHERE parcel_id_code=22345 AND exit_state
> IS NULL
> 2. SELECT * from tbl_tracker where parcel_id_code=44533
> 3. SELECT * from tbl_tracker where exit_code = 2

You will need an index with exit_state as the first column to make #3
perform well --- at least, assuming that an index is going to help at
all anyway. The rule of thumb is that if a query is going to fetch
more than a few percent of a table, an index is not useful because
it's going to be touching most table pages anyway, so a seqscan will
win. I've forgotten now what you said the stats for exit_code values
other than null were, but it's quite possible that an index is useless
for #3.

These considerations are mostly covered in the manual:
http://www.postgresql.org/docs/9.2/static/indexes.html

regards, tom lane

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Nikolas Everett 2012-12-27 17:10:11 explain analyze reports that my queries are fast but they run very slowly
Previous Message Richard Neill 2012-12-27 16:43:38 Re: Why does the query planner use two full indexes, when a dedicated partial index exists?