join query 질문입니다. 0 1 1,434

by playlyun [SQL Query] postgresql query join hash join [2020.08.13 13:12:44]


SELECT * FROM table_a a, table_b b WHERE a.id = b.id;

 

로 했을때 query plan을 보면 hash join을 했다고 나오는데, 

left,rigt join 으로 보면 어떻게 구분되는건가요? 

by pajama [2020.08.13 13:39:31]

강제로 hash join을 사용하도록 해보니 쿼리 플랜이 각각 아래처럼 나오네요.

postgresql은 9.6에서 실행한 내용입니다. 데이터가 없어서인지 Hash Left로만 풀리네요.

postgres=# create table table_a (id int);
CREATE TABLE
postgres=# create table table_b (id int);
CREATE TABLE
postgres=# set enable_mergejoin to off;
SET
postgres=# set enable_nestloop to off;
SET
postgres=# explain analyze SELECT * FROM table_a a inner join table_b b ON a.id = b.id;
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Hash Join  (cost=67.38..1247.18 rows=32512 width=8) (actual time=0.009..0.009 rows=0 loops=1)
   Hash Cond: (a.id = b.id)
   ->  Seq Scan on table_a a  (cost=0.00..35.50 rows=2550 width=4) (actual time=0.005..0.005 rows=0 loops=1)
   ->  Hash  (cost=35.50..35.50 rows=2550 width=4) (never executed)
         ->  Seq Scan on table_b b  (cost=0.00..35.50 rows=2550 width=4) (never executed)
 Planning time: 0.109 ms
 Execution time: 0.049 ms
(7 rows)

postgres=# explain analyze SELECT * FROM table_a a left outer join table_b b ON a.id = b.id;
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Hash Left Join  (cost=67.38..1247.18 rows=32512 width=8) (actual time=0.005..0.005 rows=0 loops=1)
   Hash Cond: (a.id = b.id)
   ->  Seq Scan on table_a a  (cost=0.00..35.50 rows=2550 width=4) (actual time=0.004..0.004 rows=0 loops=1)
   ->  Hash  (cost=35.50..35.50 rows=2550 width=4) (never executed)
         ->  Seq Scan on table_b b  (cost=0.00..35.50 rows=2550 width=4) (never executed)
 Planning time: 0.109 ms
 Execution time: 0.036 ms
(7 rows)

postgres=# explain analyze SELECT * FROM table_a a right outer join table_b b ON a.id = b.id;
                                                 QUERY PLAN
-------------------------------------------------------------------------------------------------------------
 Hash Left Join  (cost=67.38..1247.18 rows=32512 width=8) (actual time=0.006..0.006 rows=0 loops=1)
   Hash Cond: (b.id = a.id)
   ->  Seq Scan on table_b b  (cost=0.00..35.50 rows=2550 width=4) (actual time=0.004..0.005 rows=0 loops=1)
   ->  Hash  (cost=35.50..35.50 rows=2550 width=4) (never executed)
         ->  Seq Scan on table_a a  (cost=0.00..35.50 rows=2550 width=4) (never executed)
 Planning time: 0.104 ms
 Execution time: 0.036 ms
(7 rows)
댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입