Re: limit 1 and functional indexes

From: Greg Stark <gsstark(at)mit(dot)edu>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Alexandra Birch <alexandra(at)trymedia(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: limit 1 and functional indexes
Date: 2004-01-30 06:07:39
Message-ID: 874queyno4.fsf@stark.xeocode.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance pgsql-sql


Bruno Wolff III <bruno(at)wolff(dot)to> writes:

> > QUERY PLAN
> > ------------------------------------------------------------------------------------------------------------------------------------
> > Sort (cost=11824.16..11831.55 rows=2956 width=33) (actual time=248.17..248.17 rows=0 loops=1)
> > Sort Key: order_date
> > -> Index Scan using transactions_pop_i on transactions
> > (cost=0.00..11653.79 rows=2956 width=33)
> > (actual time=126.13..126.13 rows=0 loops=1)
> > Index Cond: (upper((pop)::text) = '79BCDC8A4A4F99E7C111111111111111'::text)
> > Total runtime: 248.25 msec

Yeah, the problem with functional indexes is that the optimizer doesn't have
any clue how the records are distributed since it only has statistics for
columns, not your expression. Notice it's estimating 2956 rows where in fact
there are 0.

I think someone was actually working on this so it may be improved in 7.5 but
I'm not sure.

Given the type of data you're storing, which looks like hex strings, are you
sure you need to do a case-insensitive search here? Can't you just uppercase
it when you store it?

The other option would be to use a subquery and force the planner not to pull
it up, something like:

select code
from (
select code
from transactions
where UPPER(pop) = UPPER('79bcdc8a4a4f99e7c111111111111111')
offset 0
)
order by order_date DESC;

The offset 0 prevents the optimizer from pulling the subquery into the outer
query. I think this will prevent it from even considering the order_date index
scan, but you'll have to try to be sure.

--
greg

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Shridhar Daithankar 2004-01-30 07:03:45 Re: On the performance of views
Previous Message Tom Lane 2004-01-30 05:26:47 Re: query optimization differs between view and explicit

Browse pgsql-sql by date

  From Date Subject
Next Message Shridhar Daithankar 2004-01-30 07:03:45 Re: On the performance of views
Previous Message Chris Travers 2004-01-30 03:03:49 Re: