From: | Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
---|---|
To: | David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> |
Cc: | James Parks <james(dot)parks(at)meraki(dot)net>, postgres performance list <pgsql-performance(at)postgresql(dot)org> |
Subject: | Re: Merge joins on index scans |
Date: | 2016-03-01 01:22:28 |
Message-ID: | 6638.1456795348@sss.pgh.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-performance |
David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> writes:
> On 27 February 2016 at 11:07, James Parks <james(dot)parks(at)meraki(dot)net> wrote:
>> If you force the query planner to use a merge join on the above query, it
>> takes 10+ minutes to complete using the data as per below. If you force the
>> query planner to use a hash join on the same data, it takes ~200
>> milliseconds.
> I believe I know what is going on here, but can you please test;
> SELECT b.* FROM b WHERE EXISTS (SELECT 1 FROM a ON b.a_id = a.id AND
> a.nonce = ?) ORDER BY b.id ASC;
> using the merge join plan.
> If this performs much better then the problem is due to the merge join
> mark/restore causing the join to have to transition through many
> tuples which don't match the a.nonce = ? predicate.
Clearly we are rescanning an awful lot of the "a" table:
-> Index Scan using a_pkey on a (cost=0.00..26163.20 rows=843 width=8) (actual time=5.706..751385.306 rows=83658 loops=1)
Filter: (nonce = 64)
Rows Removed by Filter: 2201063696
Buffers: shared hit=2151024418 read=340
I/O Timings: read=1.015
The other explain shows a scan of "a" reading about 490k rows and
returning 395 of them, so there's a factor of about 200 re-read here.
I wonder if the planner should have inserted a materialize node to
reduce that.
However, I think the real problem is upstream of that: if that indexscan
was estimated at 26163.20 units, how'd the mergejoin above it get costed
at only 7850.13 units? The answer has to be that the planner thought the
merge would stop before reading most of "a", as a result of limited range
of b.a_id. It would be interesting to look into what the actual maximum
b.a_id value is.
regards, tom lane
From | Date | Subject | |
---|---|---|---|
Next Message | James Parks | 2016-03-02 07:36:20 | Re: Merge joins on index scans |
Previous Message | Tom Lane | 2016-02-29 18:47:12 | Re: Odd behavior with indices |