pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Kevin Grittner <kgrittn@ymail.com>
To: Andrew Dunstan <andrew@dunslane.net>
To: 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, 7 Nov 2014 06:17:31 -0800
Message-ID: <1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com> (raw)
In-Reply-To: <545CCF54.1010205@dunslane.net>
References: <545C479B.7010104@dc.baikal.ru>
	<CAApHDvr0X+eYy-bZJh-ch3Kr62BJkk=ZWACyndMKJsUBYWjQpg@mail.gmail.com>
	<545C5352.4050309@dc.baikal.ru>
	<545CCF54.1010205@dunslane.net>
List-Unsubscribe:  <mailto:majordomo@postgresql.org?body=unsub%20pgsql-performance>

Andrew Dunstan <andrew@dunslane.net> wrote:
> On 11/07/2014 12:06 AM, Vlad Arkhipov wrote:

>> 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
>
> Could you not rewrite it as something this?:
>
>     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.financial_document_id < 100)

Yeah, when there are two ways to write a query that are logically
equivalent, it is better to put the AND at the higher level than
the OR.  On the other hand, why not simply write it as?:

select *
  from commons.financial_documents fd
  where (fd.creation_time, fd.financial_document_id)
      < ('2011-11-07 10:39:07.285022+08', 100)
  order by fd.creation_time desc
  limit 200

--
Kevin Grittner
EDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-performance



view thread (7+ messages)  latest in thread

Message-ID: <1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com>
Permalink:  ../1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com/
Also on:    postgresql.org/message-id/1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com

 · 

reply

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: kgrittn@ymail.com, andrew@dunslane.net, arhipov@dc.baikal.ru, dgrowleyml@gmail.com
  Subject: Re: Postgres does not use indexes with OR-conditions
  In-Reply-To: <1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com>

* 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