X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 452CAD1CC25 for ; Wed, 28 Jul 2004 00:22:25 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 98499-06 for ; Wed, 28 Jul 2004 03:22:21 +0000 (GMT) Received: from www.postgresql.com (www.postgresql.com [200.46.204.209]) by svr1.postgresql.org (Postfix) with ESMTP id 99833D1BACD for ; Wed, 28 Jul 2004 00:21:24 -0300 (ADT) Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by www.postgresql.com (Postfix) with ESMTP id 0E96DCF68C1 for ; Tue, 27 Jul 2004 12:15:16 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.12.11/8.12.11) with ESMTP id i6RFE1wM028701; Tue, 27 Jul 2004 11:14:01 -0400 (EDT) To: "Merlin Moncure" Cc: pgsql-performance@postgresql.org Subject: Re: best way to fetch next/prev record based on index In-reply-to: <6EE64EF3AB31D5448D0007DD34EEB34101AEF5@Herge.rcsinc.local> References: <6EE64EF3AB31D5448D0007DD34EEB34101AEF5@Herge.rcsinc.local> Comments: In-reply-to "Merlin Moncure" message dated "Tue, 27 Jul 2004 09:07:02 -0400" Date: Tue, 27 Jul 2004 11:14:01 -0400 Message-ID: <28700.1090941241@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200407/191 X-Sequence-Number: 7582 "Merlin Moncure" writes: > So, for a table t with a three part key over columns a,b,c, the query to > read the next value from t for given values a1, b1, c1 is > select * from t where > a >= a1 and > (a > a1 or b >= b1) and > (a > a1 or b > b1 or c > c1) > In about 95% of cases, the planner correctly selects the index t(a,b,c) > and uses it. I'm surprised it's that good. Why not do select * from t where a >= a1 and b >= b1 and c >= c1 order by a,b,c limit 1 offset 1; which has a much more obvious translation to an indexscan. regards, tom lane