Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 03C481337B39 for ; Sun, 6 Feb 2011 18:03:33 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 82003-01 for ; Sun, 6 Feb 2011 22:03:25 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail.tala.dk (1503ds2-aroe.0.fullrate.dk [89.150.132.239]) by mail.postgresql.org (Postfix) with ESMTP id A11B41337B8D for ; Sun, 6 Feb 2011 18:03:21 -0400 (AST) Received: from localhost.localdomain (server [127.0.0.1]) by mail.tala.dk (Postfix) with ESMTP id 05C19DF40 for ; Sun, 6 Feb 2011 23:03:18 +0100 (CET) Received: (from apache@localhost) by localhost.localdomain (8.14.4/8.14.4/Submit) id p16M3INw023609; Sun, 6 Feb 2011 23:03:18 +0100 X-Authentication-Warning: localhost.localdomain: apache set sender to renard@tala.dk using -f To: Subject: Re: Different execution plans for semantically equivalent queries X-PHP-Originating-Script: 0:func.inc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Date: Sun, 06 Feb 2011 23:03:18 +0100 From: Mikkel Lauritsen In-Reply-To: <15642.1297027741@sss.pgh.pa.us> References: <15642.1297027741@sss.pgh.pa.us> Message-ID: X-Sender: renard@tala.dk User-Agent: RoundCube Webmail/0.3.1 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201102/280 X-Sequence-Number: 42418 Hi Tom et al, Many thanks for your prompt reply - you wrote: >> SELECT * FROM table t1 WHERE 0 = (SELECT COUNT(*) FROM table t2 WHERE >> t2.type = t1.type AND t2.timestamp > t1.timestamp) > > I suspect that *any* database is going to have trouble optimizing that. Okay, I expected that much. Just out of curiosity I've been looking a bit at the optimizer code in PostgreSQL, and it seems as if it would be at least theoretically possible to add support for things like transforming the query at hand into the NOT EXISTS form; a bit like how = NULL is converted to IS NULL. Would a change like that be accepted, or would you rather try to indirectly educate people into writing better SQL? > You'd be well advised to lobby the persistence framework's authors to > produce less brain-dead SQL. The NOT EXISTS formulation seems to > express what's wanted much less indirectly. Will do :-) For now I guess I'll hack it by wrapping a proxy around the JDBC driver and rewriting the SQL on the fly; I encounter other bugs in the persistence layer that are probably best handled that way as well. Best regards & thanks, Mikkel