Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id A38379F9F08 for ; Tue, 19 Dec 2006 15:35:39 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-new, port 10024) with ESMTP id 67465-08 for ; Tue, 19 Dec 2006 15:34:56 -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 0F88F9F9386 for ; Tue, 19 Dec 2006 15:34:56 -0400 (AST) Received: from out1.internal (unknown [10.202.2.149]) by out1.messagingengine.com (Postfix) with ESMTP id 15D14567F3 for ; Tue, 19 Dec 2006 14:34:51 -0500 (EST) Received: from web1.messagingengine.com ([10.202.2.210]) by out1.internal (MEProxy); Tue, 19 Dec 2006 14:34:51 -0500 Received: by web1.messagingengine.com (Postfix, from userid 99) id EFC86CE67; Tue, 19 Dec 2006 14:34:50 -0500 (EST) Message-Id: <1166556890.1882.281227661@webmail.messagingengine.com> X-Sasl-Enc: M4gd6HAkTpauQ0bOvJ57FG5yFJevx2Rw6vS9n07sZoVS 1166556890 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 References: <1166538737.31684.281175363@webmail.messagingengine.com> <458813ED.50108@archonet.com> <1166549705.21302.281208465@webmail.messagingengine.com> <45882E0A.8070807@archonet.com> Subject: Re: Inner join vs where-clause subquery In-Reply-To: <45882E0A.8070807@archonet.com> Date: Tue, 19 Dec 2006 14:34:50 -0500 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/479 X-Sequence-Number: 22535 Here's the query and explain analyze using the result of the sub-query substituted: QUERY explain analyze 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 > 478287801 and dtCreateDate < '2006-12-18 9:10' EXPLAIN ANALYZE Aggregate (cost=657.37..657.38 rows=1 width=12) (actual time=0.018..0.019 rows=1 loops=1) -> Index Scan using activity_log_facts_nlogid_idx on activity_log_facts (cost=0.00..652.64 rows=472 width=12) (actual time=0.014..0.014 rows=0 loops=1) Index Cond: (nlogid > 478287801) Filter: (dtcreatedate < '2006-12-18 09:10:00'::timestamp without time zone) Total runtime: 0.076 ms Sorry if the reason should be obvious, but I'm not the best at interpreting the explains. Why is this explain so much simpler than the other query plan (with the subquery)? On Tue, 19 Dec 2006 18:23:06 +0000, "Richard Huxton" said: > Jeremy Haile wrote: > > Here is the explain analyze output: > > Well, the row estimates are about as far out as you can get: > > > -> Index Scan using activity_log_facts_pkey on > > activity_log_facts (cost=0.00..1831613.82 rows=1539298 > > width=12) (actual time=0.050..0.050 rows=0 loops=1) > > > -> Index Scan Backward using activity_log_facts_pkey on > > activity_log_facts (cost=0.00..1831613.82 rows=1539298 > > width=12) (actual time=0.004..0.004 rows=0 loops=1) > > > -> Index Scan using activity_log_facts_dtcreatedate_idx on > > activity_log_facts (cost=0.00..5406927.50 rows=1539298 > > width=12) (actual time=100221.953..100221.953 rows=0 loops=1) > > > -> Index Scan Backward using > > activity_log_facts_dtcreatedate_idx on activity_log_facts > > (cost=0.00..5406927.50 rows=1539298 width=12) (actual > > time=56367.364..56367.364 rows=0 loops=1) > > Hmm - it's using the indexes on dtCreateDate and nlogid which seems > broadly sensible, and then plans to limit the results for min()/max(). > However, it's clearly wrong about how many rows will satisfy > nlogid > (select max(a.end_nlogid) from activity_log_import_history a) > > >>> 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 you run explain on the other forms of your query, I'd guess it's much > more accurate. There's a simple way to see if that is the issue. Run the > sub-query and substitute the actual value returned into the query above. > Then, try the same but with a prepared query. If it's down to nlogid > estimates then the first should be fast and the second slow. > > -- > Richard Huxton > Archonet Ltd