Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 12EDB862DB3 for ; Wed, 8 Feb 2012 15:31:24 -0400 (AST) Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RvDEg-0002tL-0j for pgsql-performance@postgresql.org; Wed, 08 Feb 2012 19:31:23 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id q18JV8Dw029204; Wed, 8 Feb 2012 14:31:08 -0500 (EST) To: Kevin Traster cc: pgsql-performance@postgresql.org Subject: Re: index scan forward vs backward = speed difference of 357X slower! In-reply-to: References: Comments: In-reply-to Kevin Traster message dated "Tue, 07 Feb 2012 08:49:05 -0800" Date: Wed, 08 Feb 2012 14:31:08 -0500 Message-ID: <29203.1328729468@sss.pgh.pa.us> From: Tom Lane X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201202/54 X-Sequence-Number: 46226 Kevin Traster writes: > The query plan and estimates are exactly the same, except desc has index > scan backwards instead of index scan for changes_shareschange. > Yet, actual runtime performance is different by 357x slower for the > ascending version instead of descending. Apparently, there are some rows passing the filter condition that are close to the end of the index, but none that are close to the start. So it takes a lot longer to find the first 15 matches in one case than the other. You haven't shown us the index definition, but I gather from the fact that the scan condition is just a Filter (not an Index Cond) that the index itself doesn't offer any clue as to whether a given row meets those conditions. So this plan is going to be doing a lot of random-access heap probes until it finds a match. > Why and how do I fix it? Probably, you need an index better suited to the query condition. If you have one and the problem is that the planner's not choosing it, then this is going to take more information to resolve. regards, tom lane