I repeated the same query with a work_mem of 2000MB. It is faster than the one with two batches but still slower than hashing the smaller table. So in this case It makes more sense to hash the smaller table (less execution time and reduce hash table size).
Explain analyze with higher work_mem (2 GB)
tpch=# explain analyze select b from tab2 left join tab1 on a = b;
Yeah, explain analyze distorted the time, while the actual query is twice faster.
Still, 2GB work_mem is not a feasible idea. Is there a way to avoid these plan (especially avoiding larger table on hash side) or any work around available ?
Thanks,
Pradeep
---- On Thu, 15 Mar 2018 00:40:27 +0530 Jeff Janes <jeff.janes@gmail.com> wrote ----
Thanks everyone for your suggestions. I would like to add explain analyze of both the plans so that we can have broader picture.
I have a work_mem of 1000 MB.
Is it possible to repeat with 2000MB or 3000MB? It would be interesting to see what the estimated cost and what the actual time would be if there were only 1 batch rather than 2.
Also, can you repeat all of these with EXPLAIN (ANALYZE, TIMING OFF) ? Sometimes the act of measuring the times can distort the times by quite a bit. (It will still give an overall execution time, it just won't try to attribute that time to the individual steps)