postgresql - Implicit JOIN returns unexpected results -
i have 2 tables:
fccuser=# select count(*) public.fine_collection user_id = 5000; count ------- 2500 (1 row) fccuser=# select count(*) public.police_notice user_id = 5000; count ------- 1011 (1 row)
and when issue
fccuser=# select count(*) public.fine_collection, public.police_notice fine_collection.user_id = 5000 , fine_collection.user_id = police_notice.user_id;
i expecting 2500 got
count
2527500 (1 row)
i.e., cartesian product of two. , analyze is:
fccuser=# explain analyze verbose select count(*) public.fine_collection, public.police_notice fine_collection.user_id = 5000 , fine_collection.user_id = police_notice.user_id; query plan ----------------------------------------------------------------------------------------------------------------------------------------------------------------- aggregate (cost=47657.20..47657.21 rows=1 width=0) (actual time=1991.552..1991.552 rows=1 loops=1) output: count(*) -> nested loop (cost=0.86..39760.60 rows=3158640 width=0) (actual time=0.448..1462.155 rows=2527500 loops=1) -> index scan using idx_user_id on public.fine_collection (cost=0.43..265.98 rows=8774 width=8) (actual time=0.213..2.448 rows=2500 loops=1) output: fine_collection.user_id index cond: (fine_collection.user_id = 5000) heap fetches: 1771 -> materialize (cost=0.42..12.52 rows=360 width=2) (actual time=0.000..0.205 rows=1011 loops=2500) output: police_notice.user_id -> index scan using idx_pn_userid on public.police_notice (cost=0.42..10.72 rows=360 width=2) (actual time=0.217..1.101 rows=1011 loops=1) output: police_notice.user_id index cond: (police_notice.user_id = 5000) heap fetches: 751 planning time: 2.126 ms execution time: 1991.697 ms (15 rows)
and postgres documentation states when join performed on non-primary columns first creates cartesian product (cross join) , applies filter. think cartesian product have rows same user_id in case, not sure how filter can applied
same happens left join, inner join etc., subquery seems give correct result of 2500.
i reasonably sure not work way in mysql though. thoughts?
thank you
the result of join correct. join every collection user_id
5000 every police notice same user_id
. have 2500 , 1011 rows joined together, , produces 2527500 new rows.
Comments
Post a Comment