Received: from localhost (unknown [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 4EA4D2E00F3 for ; Wed, 13 Feb 2008 16:52:13 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 05799-03-8 for ; Wed, 13 Feb 2008 16:52:02 -0400 (AST) Received: from oxford.xeocode.com (client-81-107-195-115.glfd.adsl.virgin.net [81.107.195.115]) by postgresql.org (Postfix) with ESMTP id 85C222E0352 for ; Wed, 13 Feb 2008 16:51:46 -0400 (AST) Received: from localhost ([127.0.0.1] helo=oxford.xeocode.com) by oxford.xeocode.com with esmtp (Exim 4.69) (envelope-from ) id 1JPOZq-0001q0-G7; Wed, 13 Feb 2008 20:51:37 +0000 To: "Pallav Kalva" Cc: "Richard Huxton" , "Dean Gibson \(DB Administrator\)" , Subject: Re: Optimizing No matching record Queries In-Reply-To: <47B34921.1040507@livedatagroup.com> (Pallav Kalva's message of "Wed\, 13 Feb 2008 14\:46\:41 -0500") User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) X-Draft-From: ("nnimap+mail01.enterprisedb.com:INBOX.performance" 4252) References: <47B2112C.5070707@livedatagroup.com> <47B234C1.1090707@ultimeth.com> <47B2B058.7020804@archonet.com> <47B34921.1040507@livedatagroup.com> From: Gregory Stark Organization: EnterpriseDB Date: Wed, 13 Feb 2008 20:51:34 +0000 Message-ID: <877ih8bmw9.fsf@oxford.xeocode.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200802/150 X-Sequence-Number: 28692 "Pallav Kalva" writes: > This index would fix this problem but in general I would like to know what if > there are queries where it does "index scan backwards" and there is no "order > by clause" and the query is still bad ? Would there be a case like that or the > planner uses index scan backwards only when use order by desc also. I think you're oversimplifying. Basically you were asking the planner for the most recent record for a given user. The planner had the choice either of a) going through all the records for a given user and picking the most recent, or b) scanning the records from most recent to oldest and looking for the given user. It was a choice between two evils. If there are a lot of records for the user then a) will be bad since it has to scan all of them to find the most recent and if there are no records for the user then b) will be bad because it'll have to go through all of the records to the beginning of time. The suggested index lets it scan the records for the given user from most recent to oldest without seeing any records for any other user. -- Gregory Stark EnterpriseDB http://www.enterprisedb.com Ask me about EnterpriseDB's Slony Replication support!