Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1X9TMY-0008Jx-2w for pgsql-performance@arkaria.postgresql.org; Tue, 22 Jul 2014 06:15:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1X9TMX-0003yl-FL for pgsql-performance@arkaria.postgresql.org; Tue, 22 Jul 2014 06:15:45 +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 1X9TMW-0003ye-BK for pgsql-performance@postgresql.org; Tue, 22 Jul 2014 06:15:44 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1X9TMO-0006GE-5d for pgsql-performance@postgresql.org; Tue, 22 Jul 2014 06:15:43 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s6M6FTuO011613; Tue, 22 Jul 2014 02:15:29 -0400 From: Tom Lane To: johno cc: pgsql-performance@postgresql.org Subject: Re: Slow query with indexed ORDER BY and LIMIT when using OR'd conditions In-reply-to: References: <4381.1405997581@sss.pgh.pa.us> Comments: In-reply-to johno message dated "Tue, 22 Jul 2014 07:57:08 +0200" Date: Tue, 22 Jul 2014 02:15:29 -0400 Message-ID: <11612.1406009729@sss.pgh.pa.us> 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 johno writes: > On Tue, Jul 22, 2014 at 4:53 AM, Tom Lane wrote: >> johno writes: >>> The obvious query is >>> SELECT * FROM register_uz_accounting_entities >>> WHERE effective_on > '2014-07-11' OR (effective_on = '2014-07-11' AND >>> id > 1459) >>> ORDER BY effective_on, id >>> LIMIT 100 >> A more readily optimizable query is >> SELECT * FROM register_uz_accounting_entities >> WHERE (effective_on, id) > ('2014-07-11'::date, 1459) >> ORDER BY effective_on, id >> LIMIT 100 > Yes, but that query has completely different semantics - I can't change > that. No, it doesn't. Read it again ... or read up on row comparisons, if you're unfamiliar with that notation. The above queries are exactly equivalent per spec. regards, tom lane -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance