Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XmkM9-0002gh-65 for pgsql-performance@arkaria.postgresql.org; Fri, 07 Nov 2014 14:17:41 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XmkM8-0007UI-NA for pgsql-performance@arkaria.postgresql.org; Fri, 07 Nov 2014 14:17:40 +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 1XmkM7-0007U6-UU for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 14:17:40 +0000 Received: from nm29-vm1.bullet.mail.ne1.yahoo.com ([98.138.90.63]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1XmkM3-0002p5-FC for pgsql-performance@postgresql.org; Fri, 07 Nov 2014 14:17:38 +0000 Received: from [98.138.100.115] by nm29.bullet.mail.ne1.yahoo.com with NNFMP; 07 Nov 2014 14:17:31 -0000 Received: from [98.138.89.193] by tm106.bullet.mail.ne1.yahoo.com with NNFMP; 07 Nov 2014 14:17:31 -0000 Received: from [127.0.0.1] by omp1051.mail.ne1.yahoo.com with NNFMP; 07 Nov 2014 14:17:31 -0000 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 537185.11496.bm@omp1051.mail.ne1.yahoo.com Received: (qmail 37238 invoked by uid 60001); 7 Nov 2014 14:17:31 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1415369851; bh=YazTkmjjx4gJGQsAuAEuk/XdGP9QQQ3DXrIOLEsr97o=; h=References:Message-ID:Date:From:Reply-To:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type; b=tRduZLqMHpzao4sMFBCrTVrD1BdPomP+s16Dq/JKQG6qPVkntAitdQ5sRcDI5yN9MafY1H5139oVuUBJ3OXVC+mAMKMUFIZ8gAqneYiJ5+pmsNxIgIccsAfasNvCri3ONzxEHFzU/DliASOP2prUKgbFm+ErD0grmDjjjlkoMJI= X-YMail-OSG: 08aU7WgVM1ndkR0WCgehEOeoJVW9YnNLNvcmyFNDp7M660A T2oSISQtJ.HUJASKTzBesRxMJxub.CL7UlhNt3PLDEXl_46nttiHN_UUsCuU pO01LnF4cU3M71MfP5oQfGapmSY8FWuvqC6mi_ZkDKoibdY_BwR.oH3cZXpq JYQYU8lyyRGmczwG_oe6GYOb79dFNBOEnD4ny28rqsd8UPq2bLA340AbZ7Xb SWewVH_fQGBBJBY5UMvKlPzOxOp8vtJHmepK5EVc55qhf.yubByc3ELBFJLS b0Cfe66fMTrq_JBOJxaqyXqMZ23Dpigp0e1KeZnGvgk7jPmcpa2ZlSLINqe1 GUWW44L5MtAFJPAxObZp3IyNfOfxNT4G10bDSgYoZgzUjKRPqBzKm2rNX55A XU3.LP6iYUYunmg.wLk7eCg9AIiZ_qXk3yhiwtDz5n2eKvDYwzT9U132SSum izdl7qDGwhBvg9VtkbxrgPLx8xK8hwZrzrTd25qQDt0SwXEu1N6F0MAFDujE KpPDC9tFwGDuvSxPbTYq3ESD5zrcgoQ7hJMVT2isuvO6g.TkBhVa3seYVRJV Li3yIoOvS4emO56sbJw-- Received: from [24.178.25.147] by web122305.mail.ne1.yahoo.com via HTTP; Fri, 07 Nov 2014 06:17:31 PST X-Rocket-MIMEInfo: 002.001, QW5kcmV3IER1bnN0YW4gPGFuZHJld0BkdW5zbGFuZS5uZXQ.IHdyb3RlOgo.IE9uIDExLzA3LzIwMTQgMTI6MDYgQU0sIFZsYWQgQXJraGlwb3Ygd3JvdGU6Cgo.PiBJIG5lZWQgdG8gcmV3cml0ZSBpdCBpbiB0aGUgd2F5IGJlbG93IHRvIG1ha2UgUG9zdGdyZXMgdXNlIHRoZSBpbmRleC4KPj4KPj4gc2VsZWN0ICoKPj4gZnJvbSBjb21tb25zLmZpbmFuY2lhbF9kb2N1bWVudHMgZmQKPj4gd2hlcmUgZmQuY3JlYXRpb25fdGltZSA8PSAnMjAxMS0xMS0wNyAxMDozOTowNy4yODUwMjIrMDgnCj4.ICBhbmQgKAoBMAEBAQE- X-Mailer: YahooMailWebService/0.8.203.733 References: <545C479B.7010104@dc.baikal.ru> <545C5352.4050309@dc.baikal.ru> <545CCF54.1010205@dunslane.net> Message-ID: <1415369851.48766.YahooMailNeo@web122305.mail.ne1.yahoo.com> Date: Fri, 7 Nov 2014 06:17:31 -0800 From: Kevin Grittner Reply-To: Kevin Grittner Subject: Re: Postgres does not use indexes with OR-conditions To: Andrew Dunstan , Vlad Arkhipov , David Rowley Cc: pgsql-performance In-Reply-To: <545CCF54.1010205@dunslane.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Pg-Spam-Score: -1.9 (-) 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 Andrew Dunstan 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