From: Vlad Arkhipov <arhipov@dc.baikal.ru>
To: David Rowley <dgrowleyml@gmail.com>
Cc: pgsql-performance <pgsql-performance@postgresql.org>
Subject: Re: Postgres does not use indexes with OR-conditions
Date: Fri, 07 Nov 2014 13:06:26 +0800
Message-ID: <545C5352.4050309@dc.baikal.ru> (raw)
In-Reply-To: <CAApHDvr0X+eYy-bZJh-ch3Kr62BJkk=ZWACyndMKJsUBYWjQpg@mail.gmail.com>
References: <545C479B.7010104@dc.baikal.ru>
<CAApHDvr0X+eYy-bZJh-ch3Kr62BJkk=ZWACyndMKJsUBYWjQpg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>
It was just a minimal example. The real query looks like this.
select *
from commons.financial_documents fd
where fd.creation_time < '2011-11-07 10:39:07.285022+08'
or (fd.creation_time = '2011-11-07 10:39:07.285022+08' and
fd.financial_document_id < 100)
order by fd.creation_time desc
limit 200
I need to rewrite it in the way below to make Postgres use the index.
select *
from commons.financial_documents fd
where fd.creation_time <= '2011-11-07 10:39:07.285022+08'
and (
fd.creation_time < '2011-11-07 10:39:07.285022+08'
or (fd.creation_time = '2011-11-07 10:39:07.285022+08' and
fd.financial_document_id < 100)
)
order by fd.creation_time desc
limit 200
On 11/07/2014 12:38 PM, David Rowley wrote:
> On Fri, Nov 7, 2014 at 5:16 PM, arhipov <arhipov@dc.baikal.ru
> <mailto:arhipov@dc.baikal.ru>> wrote:
>
> Hello,
>
> I have just came across interesting Postgres behaviour with
> OR-conditions. Are there any chances that the optimizer will
> handle this situation in the future?
>
> select *
> from commons.financial_documents fd
> where fd.creation_time <= '2011-11-07 10:39:07.285022+08'
> order by fd.creation_time desc
> limit 200
>
> select *
> from commons.financial_documents fd
> where fd.creation_time = '2011-11-07 10:39:07.285022+08'
> or fd.creation_time < '2011-11-07 10:39:07.285022+08'
> order by fd.creation_time desc
> limit 200
>
>
> It would certainly be possible, providing the constants compare
> equally, but... Question: Would you really want to pay a, say 1%
> increase in planning time for ALL queries, so that you could have this
> unique case of queries perform better at execution time?
>
> Is there a valid reason why you don't just write the query with the <=
> operator?
>
> Regards
>
> David Rowley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-performance@postgresql.org
Cc: arhipov@dc.baikal.ru, dgrowleyml@gmail.com
Subject: Re: Postgres does not use indexes with OR-conditions
In-Reply-To: <545C5352.4050309@dc.baikal.ru>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox