-
Notifications
You must be signed in to change notification settings - Fork 6
/
join_inner_outer_semi_anti.sql
56 lines (52 loc) · 6.21 KB
/
join_inner_outer_semi_anti.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
## Nested loop left join
explain analyze
select e.emp_no, first_name, last_name
from employees e
left join salaries s on e.emp_no = s.emp_no;
# | EXPLAIN |
# +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# | -> Nested loop left join (cost=472050.92 rows=2780761) (actual time=1.223..1631.085 rows=2844047 loops=1)
# -> Table scan on e (cost=31007.31 rows=299512) (actual time=0.032..77.046 rows=300024 loops=1)
# -> Index lookup on s using PRIMARY (emp_no=e.emp_no) (cost=0.54 rows=9) (actual time=0.003..0.004 rows=9 loops=300024)
# |
# +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# 1 row in set (1.83 sec)
# Nested loop inner join
explain analyze
select e.emp_no, first_name, last_name
from employees e
inner join salaries s on e.emp_no = s.emp_no;
# | EXPLAIN |
# +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# | -> Nested loop inner join (cost=469802.06 rows=2780761) (actual time=1.746..1515.296 rows=2844047 loops=1)
# -> Table scan on e (cost=31007.31 rows=299512) (actual time=0.047..74.875 rows=300024 loops=1)
# -> Index lookup on s using PRIMARY (emp_no=e.emp_no) (cost=0.54 rows=9) (actual time=0.003..0.004 rows=9 loops=300024)
# |
# +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# 1 row in set (1.72 sec)
## Nested loop semi join
explain analyze
select emp_no, first_name, last_name
from employees
where emp_no in (select emp_no from salaries);
# | EXPLAIN |
# +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# | -> Nested loop semijoin (cost=469689.61 rows=2780761) (actual time=1.311..975.581 rows=300024 loops=1)
# -> Table scan on employees (cost=31007.31 rows=299512) (actual time=0.037..76.961 rows=300024 loops=1)
# -> Index lookup on salaries using PRIMARY (emp_no=employees.emp_no) (cost=4.98 rows=9) (actual time=0.003..0.003 rows=1 loops=300024)
# |
# +-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# 1 row in set (1.01 sec)
## Nested loop anti join
explain analyze
select emp_no, first_name, last_name
from employees
where emp_no not in (select emp_no from salaries);
# | EXPLAIN |
# +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# | -> Nested loop antijoin (cost=469689.61 rows=2780761) (actual time=945.101..945.101 rows=0 loops=1)
# -> Table scan on employees (cost=31007.31 rows=299512) (actual time=0.033..75.872 rows=300024 loops=1)
# -> Index lookup on salaries using PRIMARY (emp_no=employees.emp_no) (cost=4.98 rows=9) (actual time=0.003..0.003 rows=1 loops=300024)
# |
# +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
# 1 row in set (0.95 sec)