Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Xmmk8-000883-Og for pgsql-performance@arkaria.postgresql.org; Fri, 07 Nov 2014 16:50:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Xmmk7-0002yZ-2M for pgsql-performance@arkaria.postgresql.org; Fri, 07 Nov 2014 16:50:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Xmmk6-0002yT-9T for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 16:50:34 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Xmmk1-0005jP-NT for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 16:50:32 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1Xmmjy-0005Tf-2s for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 08:50:26 -0800 Date: Fri, 7 Nov 2014 08:50:26 -0800 (PST) From: David G Johnston To: pgsql-performance@postgresql.org Message-ID: <1415379026083-5826065.post@n5.nabble.com> In-Reply-To: <1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com> References: <545C479B.7010104@dc.baikal.ru> <545C5352.4050309@dc.baikal.ru> <545CCF54.1010205@dunslane.net> <1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com> Subject: Re: Postgres does not use indexes with OR-conditions MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 0.8 (/) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org Kevin Grittner-5 wrote > Andrew Dunstan < > andrew@ > > 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