Received: from localhost (maia-5.hub.org [200.46.204.182]) by postgresql.org (Postfix) with ESMTP id B82C59FA3DA for ; Mon, 15 Jan 2007 06:36:00 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.182]) (amavisd-new, port 10024) with ESMTP id 55874-03 for ; Mon, 15 Jan 2007 06:35:48 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from lists.commandprompt.com (host-254.commandprompt.net [207.173.203.254]) by postgresql.org (Postfix) with ESMTP id B36C29FA3B3 for ; Mon, 15 Jan 2007 06:35:51 -0400 (AST) Received: from perhan.alvh.no-ip.org (200-126-100-202.bk7-dsl.surnet.cl [200.126.100.202]) (authenticated bits=0) by lists.commandprompt.com (8.13.7/8.13.6) with ESMTP id l0FAZbOT022721; Mon, 15 Jan 2007 02:35:38 -0800 Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 8D7D232A05; Mon, 15 Jan 2007 07:35:36 -0300 (CLST) Date: Mon, 15 Jan 2007 07:35:36 -0300 From: Alvaro Herrera To: Adam Rich Cc: "'Joshua D. Drake'" , "'Tom Lane'" , "'Craig A. James'" , "'PostgreSQL Performance'" Subject: Re: max() versus order/limit (WAS: High update activity, PostgreSQL vs BigDBMS) Message-ID: <20070115103536.GH7233@alvh.no-ip.org> Mail-Followup-To: Adam Rich , "'Joshua D. Drake'" , 'Tom Lane' , "'Craig A. James'" , 'PostgreSQL Performance' References: <069301c73860$ee779a20$6400a8c0@dualcore> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <069301c73860$ee779a20$6400a8c0@dualcore> User-Agent: Mutt/1.5.13 (2006-08-11) X-Virus-Scanned: ClamAV version 0.88.5, clamav-milter version 0.88.5 on projects.commandprompt.com X-Virus-Status: Clean X-Greylist: Sender succeded SMTP AUTH authentication, not delayed by milter-greylist-1.6 (lists.commandprompt.com [192.168.2.159]); Mon, 15 Jan 2007 02:35:42 -0800 (PST) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200701/231 X-Sequence-Number: 22832 Adam Rich wrote: > > Did anybody get a chance to look at this? Is it expected behavior? > Everyone seemed so incredulous, I hoped maybe this exposed a bug > that would be fixed in a near release. Actually, the planner is only able to do the min()/max() transformation into order by/limit in the case of a single table being scanned. Since you have a join here, the optimization is obviously not used: > select max(item_id) > from events e, receipts r, receipt_items ri > where e.event_id=r.event_id and r.receipt_id=ri.receipt_id plan/planagg.c says /* * We also restrict the query to reference exactly one table, since join * conditions can't be handled reasonably. (We could perhaps handle a * query containing cartesian-product joins, but it hardly seems worth the * trouble.) */ so you should keep using your hand-written order by/limit query. -- Alvaro Herrera http://www.CommandPrompt.com/ PostgreSQL Replication, Consulting, Custom Development, 24x7 support