Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 45EF29FA0EF for ; Tue, 19 Dec 2006 10:32:28 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 96483-04 for ; Tue, 19 Dec 2006 10:32:16 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.4 Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [66.111.4.28]) by postgresql.org (Postfix) with ESMTP id 9A7E99F9386 for ; Tue, 19 Dec 2006 10:32:22 -0400 (AST) Received: from out1.internal (unknown [10.202.2.149]) by out1.messagingengine.com (Postfix) with ESMTP id 7D266564E5 for ; Tue, 19 Dec 2006 09:32:17 -0500 (EST) Received: from web1.messagingengine.com ([10.202.2.210]) by out1.internal (MEProxy); Tue, 19 Dec 2006 09:32:17 -0500 Received: by web1.messagingengine.com (Postfix, from userid 99) id 4B9A8D65C; Tue, 19 Dec 2006 09:32:17 -0500 (EST) Message-Id: <1166538737.31684.281175363@webmail.messagingengine.com> X-Sasl-Enc: xhAt1RHi0ThvnWT+XhA+tKn2Z1CjimknP/3/VXFztwvv 1166538737 From: "Jeremy Haile" To: pgsql-performance@postgresql.org Content-Disposition: inline Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="ISO-8859-1" MIME-Version: 1.0 X-Mailer: MessagingEngine.com Webmail Interface Subject: Inner join vs where-clause subquery Date: Tue, 19 Dec 2006 09:32:17 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/473 X-Sequence-Number: 22529 I have the following query which performs extremely slow: select min(nlogid) as start_nlogid, max(nlogid) as end_nlogid, min(dtCreateDate) as start_transaction_timestamp, max(dtCreateDate) as end_transaction_timestamp from activity_log_facts where nlogid > ( select max(a.end_nlogid) from activity_log_import_history a) and dtCreateDate < '2006-12-18 9:10' If I change the where clause to have the return value of the subquery it runs very fast: select min(nlogid) as start_nlogid, max(nlogid) as end_nlogid, min(dtCreateDate) as start_transaction_timestamp, max(dtCreateDate) as end_transaction_timestamp from activity_log_facts where nlogid > 402123456 and dtCreateDate < '2006-12-18 9:10' If I change the query to the following, it runs fast: select min(nlogid) as start_nlogid, max(nlogid) as end_nlogid, min(dtCreateDate) as start_transaction_timestamp, max(dtCreateDate) as end_transaction_timestamp from activity_log_facts inner join ( select max(end_nlogid) as previous_nlogid from activity_log_import_history) as a on activity_log_facts.nlogid > a.previous_nlogid where dtCreateDate < ${IMPORT_TIMESTAMP} I am running PG 8.2. Why is that this the case? Shouldn't the query planner be smart enough to know that the first query is the same as the second and third? The inner query does not refer to any columns outside of itself. I personally find the first query easiest to read and wish it performed well. Jeremy Haile