pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeremy Haile <jhaile@fastmail.fm>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-performance@postgresql.org
Subject: Re: Slow inner join, but left join is fast
Date: Wed, 10 Jan 2007 13:20:18 -0500
Message-ID: <1168453218.22654.1168592237@webmail.messagingengine.com> (raw)
In-Reply-To: <10060.1168449344@sss.pgh.pa.us>
References: <1168445842.5566.1168570133@webmail.messagingengine.com>
	<10060.1168449344@sss.pgh.pa.us>

The table should have been analyzed, but to make sure I ran analyze on
the table before executing the explain analyze queries.  Well - problem
solved.  This time the inner join query runs quickly.  

I still don't understand why the inner join would be so different from
the left join prior to the analyze.  It looks like the amount of rows
expected in the original query plan for inner join was 1 (not correct
since it was really 8728)  The left join query had the exact same
subqueries but expected 77214 rows to be returned from them, which was
still not correct but resulted in a better query plan.

After the recent analyze, here's the new inner join query plan.  I won't
bother pasting the left join plan, since it is almost identical now
(including row counts)  FYI -the result of the queries is (and always
was) identical for inner join and left join.


QUERY PLAN (inner join)
Merge Join  (cost=279457.86..279479.83 rows=43 width=120) (actual
time=626.771..670.275 rows=8728 loops=1)
  Merge Cond: ((a.dcms_dim_id = b.dcms_dim_id) AND (a.merchant_dim_id =
  b.merchant_dim_id))
  ->  Sort  (cost=139717.30..139722.38 rows=2029 width=64) (actual
  time=265.669..269.878 rows=8728 loops=1)
        Sort Key: a.dcms_dim_id, a.merchant_dim_id
        ->  HashAggregate  (cost=139519.61..139585.56 rows=2029
        width=16) (actual time=211.368..247.429 rows=8728 loops=1)
              ->  Bitmap Heap Scan on transaction_facts 
              (cost=4427.62..138316.05 rows=68775 width=16) (actual
              time=21.858..100.998 rows=65789 loops=1)
                    Recheck Cond: ((transaction_date >= '2007-01-09
                    00:00:00'::timestamp without time zone) AND
                    (transaction_date < '2007-01-09 09:30:00'::timestamp
                    without time zone))
                    ->  Bitmap Index Scan on
                    transaction_facts_transaction_date_idx 
                    (cost=0.00..4410.42 rows=68775 width=0) (actual
                    time=21.430..21.430 rows=65789 loops=1)
                          Index Cond: ((transaction_date >= '2007-01-09
                          00:00:00'::timestamp without time zone) AND
                          (transaction_date < '2007-01-09
                          09:30:00'::timestamp without time zone))
  ->  Sort  (cost=139740.56..139742.67 rows=843 width=64) (actual
  time=361.083..365.418 rows=8728 loops=1)
        Sort Key: b.dcms_dim_id, b.merchant_dim_id
        ->  Subquery Scan b  (cost=139663.76..139699.59 rows=843
        width=64) (actual time=308.567..346.135 rows=8728 loops=1)
              ->  HashAggregate  (cost=139663.76..139691.16 rows=843
              width=16) (actual time=308.563..337.677 rows=8728 loops=1)
                    ->  HashAggregate  (cost=139347.68..139431.97
                    rows=8429 width=55) (actual time=198.093..246.591
                    rows=48942 loops=1)
                          ->  Bitmap Heap Scan on transaction_facts 
                          (cost=4427.62..138316.05 rows=68775 width=55)
                          (actual time=24.080..83.988 rows=65789
                          loops=1)
                                Recheck Cond: ((transaction_date >=
                                '2007-01-09 00:00:00'::timestamp without
                                time zone) AND (transaction_date <
                                '2007-01-09 09:30:00'::timestamp without
                                time zone))
                                ->  Bitmap Index Scan on
                                transaction_facts_transaction_date_idx 
                                (cost=0.00..4410.42 rows=68775 width=0)
                                (actual time=23.596..23.596 rows=65789
                                loops=1)
                                      Index Cond: ((transaction_date >=
                                      '2007-01-09 00:00:00'::timestamp
                                      without time zone) AND
                                      (transaction_date < '2007-01-09
                                      09:30:00'::timestamp without time
                                      zone))
Total runtime: 675.638 ms



On Wed, 10 Jan 2007 12:15:44 -0500, "Tom Lane" <tgl@sss.pgh.pa.us> said:
> "Jeremy Haile" <jhaile@fastmail.fm> writes:
> > I have a query made by joining two subqueries where the outer query
> > performing the join takes significantly longer to run than the two
> > subqueries.  
> 
> Please show EXPLAIN ANALYZE results, not just EXPLAIN.
> Also, have you analyzed your tables recently?
> 
> 			regards, tom lane



view thread (8+ messages)  latest in thread

Message-ID: <1168453218.22654.1168592237@webmail.messagingengine.com>
Permalink:  ../1168453218.22654.1168592237@webmail.messagingengine.com/
Also on:    postgresql.org/message-id/1168453218.22654.1168592237@webmail.messagingengine.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: jhaile@fastmail.fm, tgl@sss.pgh.pa.us
  Subject: Re: Slow inner join, but left join is fast
  In-Reply-To: <1168453218.22654.1168592237@webmail.messagingengine.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox