=============================================================================================================== Case 1 =============================================================================================================== select count(*) from ( SELECT * from (SELECT pbdedup_party_1.party_id from pbdedup_party_1 where pbdedup_party_1.batch_no = 93 UNION SELECT pbdedup_party_2.party_id from pbdedup_party_2 where pbdedup_party_2.batch_no = 93 ) as a , pb_person left outer join pbdedup_person_parts ON pb_person.party_id = pbdedup_person_parts.party_id where a.party_id = pb_person.party_id ) as b --------------------------------------------------------------------------------------------------------------- Aggregate (cost=4251.07..4251.07 rows=1 width=12) -> Hash Join (cost=1291.73..4251.07 rows=1 width=12) -> Hash Join (cost=1284.45..4072.80 rows=34196 width=8) -> Seq Scan on pb_person (cost=0.00..1496.96 rows=34196 width=4) -> Hash (cost=1081.96..1081.96 rows=34196 width=4) -> Seq Scan on pbdedup_person_parts (cost=0.00..1081.96 rows=34196 width=4) -> Hash (cost=7.27..7.27 rows=1 width=4) -> Subquery Scan a (cost=7.27..7.27 rows=1 width=4) -> Unique (cost=7.27..7.27 rows=1 width=4) -> Sort (cost=7.27..7.27 rows=2 width=4) -> Append (cost=0.00..7.26 rows=2 width=4) -> Subquery Scan *SELECT* 1 (cost=0.00..2.25 rows=1 width=4) -> Seq Scan on pbdedup_party_1 (cost=0.00..2.25 rows=1 width=4) -> Subquery Scan *SELECT* 2 (cost=0.00..5.01 rows=1 width=4) -> Index Scan using IDX_pbdedup_party_2__batch_no_g on pbdedup_party_2 (cost=0.00..5.01 rows=1 width=4) =============================================================================================================== Case 2 =============================================================================================================== select count(*) from ( SELECT * from (SELECT pbdedup_party_1.party_id from pbdedup_party_1 where pbdedup_party_1.batch_no = 93 UNION SELECT pbdedup_party_2.party_id from pbdedup_party_2 where pbdedup_party_2.batch_no = 93 ) as a left outer join pbdedup_person_parts ON a.party_id = pbdedup_person_parts.party_id , pb_person where a.party_id = pb_person.party_id ) as b --------------------------------------------------------------------------------------------------------------- Aggregate (cost=18.82..18.82 rows=1 width=12) -> Nested Loop (cost=7.27..18.81 rows=1 width=12) -> Nested Loop (cost=7.27..13.06 rows=1 width=8) -> Subquery Scan a (cost=7.27..7.27 rows=1 width=4) -> Unique (cost=7.27..7.27 rows=1 width=4) -> Sort (cost=7.27..7.27 rows=2 width=4) -> Append (cost=0.00..7.26 rows=2 width=4) -> Subquery Scan *SELECT* 1 (cost=0.00..2.25 rows=1 width=4) -> Seq Scan on pbdedup_party_1 (cost=0.00..2.25 rows=1 width=4) -> Subquery Scan *SELECT* 2 (cost=0.00..5.01 rows=1 width=4) -> Index Scan using IDX_pbdedup_party_2__batch_no_g on pbdedup_party_2 (cost=0.00..5.01 rows=1 width=4) -> Index Scan using pbdedup_person_parts_pkey on pbdedup_person_parts (cost=0.00..5.77 rows=1 width=4) -> Index Scan using pb_person_pkey on pb_person (cost=0.00..5.74 rows=1 width=4)