Re: Performance problems with prepared statements

From: "Kevin Grittner" <Kevin(dot)Grittner(at)wicourts(dot)gov>
To: <theo(at)flame(dot)co(dot)za>,<pgsql-performance(at)postgresql(dot)org>
Subject: Re: Performance problems with prepared statements
Date: 2007-10-12 19:03:50
Message-ID: 470F7EC5.EE98.0025.0@wicourts.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

>>> On Fri, Oct 12, 2007 at 9:57 AM, in message
<1192201021(dot)6170(dot)47(dot)camel(at)localhost(dot)localdomain>, Theo Kramer
<theo(at)flame(dot)co(dot)za> wrote:
>
> select * from foo where
> (a = a1 and b = b1 and c >= c1) or
> (a = a1 and b < b1) or
> (a > a1)
> order by a, b desc, c;
>
> I have, however, found that transforming the above into a union based
> query performs substantially better.

Another approach which often performs better is to rearrange the logic
so that the high-order predicate is AND instead of OR:

select * from foo where
( a >= a1
and ( a > a1
or ( b <= b1
and ( b < b1
or ( c >= c1 )))))
order by a, b desc, c;

With the right index and a limit on rows, this can do particularly well.

-Kevin

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Tom Lane 2007-10-12 19:59:16 Re: Huge amount of memory consumed during transaction
Previous Message Theo Kramer 2007-10-12 14:57:01 Re: Performance problems with prepared statements