SELECT *
FROM employees e
LEFT OUTER JOIN salaries s
ON e.emp_no = s.emp_no
WHERE s.salary > 5000
;
SELECT *
FROM employees e
LEFT OUTER JOIN salaries s
ON e.emp_no = s.emp_no
AND s.salary > 5000
;
SELECT *
FROM employees e
INNER OUTER JOIN salaries s
ON e.emp_no = s.emp_no
WHERE s.salary > 5000
;
SELECT *
FROM employees e
, JOIN salaries s
WHERE e.emp_no = s.emp_no
;
SELECT *
FROM employees e
INNER JOIN salaries s
ON e.emp_no = s.emp_no
;
SELECT *
FROM employees e
INNER JOIN salaries s
USING (emp_no)
;
SELECT *
FROM employees e
NATURAL JOIN salaries s
;
블럭 기반 조인 알고리즘의 종류 8가지 | join_cache_Incremental=off | join_cache_hashed=off | join_cache_bka=off | join_cache_level(0~8) |
---|---|---|---|---|
1.Block Nested Loop - Flat) | on | on | on | on:1~8, off=0~0 |
2.Block Nested Loop - Incremental) | off | on | on | on:2~8, off=0~1 |
3.Block Nested Loop Hash - Flat) | on | off | on | on:3~8, off=0~2 |
4.Block Nested Loop Hash - Incremental) | off | off | on | on:4~8, off=0~3 |
5.Batched Key Access - Flat) | on | on | off | on:5~8, off=0~4 |
6.Batched Key Access - Incremental) | off | on | off | on:6~8, off=0~5 |
7.Batched Key Access Hash - Flat) | on | off | off | on:7~8, off=0~6 |
8.Batched Key Access Hash - Incremental) | off | off | off | on:8~8, off=0~7 |
SET optimizer_switch='optimizer_join_buffer_size=on';
SET join_cache_space_limit=5*1024*1024;
EXPLAIN
SELECT d.dept_name
, e.first_name
FROM departments d
, employees e
, dept_emp de
WHERE d.dept_no = de.dept_no
AND de.emp_no = e.emp_no
;
+------+-------------+-------+--------+---------------------------+-------------+---------+---------------------+-------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------------------+-------------+---------+---------------------+-------+-------------+
| 1 | SIMPLE | d | index | PRIMARY | ux_deptname | 122 | NULL | 9 | Using index |
| 1 | SIMPLE | de | ref | PRIMARY,ix_empno_fromdate | PRIMARY | 12 | employees.d.dept_no | 20696 | Using index |
| 1 | SIMPLE | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.de.emp_no | 1 | |
+------+-------------+-------+--------+---------------------------+-------------+---------+---------------------+-------+-------------+
SET join_cache_level=0;
EXPLAIN
SELECT *
FROM dept_emp de
, employees e
WHERE de.from_date > '1995-01-01'
AND e.emp_no > 109004
;
+------+-------------+-------+-------+---------------+---------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+---------+---------+------+--------+-------------+
| 1 | SIMPLE | e | range | PRIMARY | PRIMARY | 4 | NULL | 149711 | Using where |
| 1 | SIMPLE | de | ALL | ix_fromdate | NULL | NULL | NULL | 331143 | Using where |
+------+-------------+-------+-------+---------------+---------+---------+------+--------+-------------+
SET join_cache_level=8;
EXPLAIN
SELECT *
FROM dept_emp de
, employees e
WHERE de.from_date > '1995-01-01'
AND e.emp_no > 109004
;
+------+-------------+-------+-------+---------------+---------+---------+------+--------+-------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+---------+---------+------+--------+-------------------------------------------------+
| 1 | SIMPLE | e | range | PRIMARY | PRIMARY | 4 | NULL | 149711 | Using where |
| 1 | SIMPLE | de | ALL | ix_fromdate | NULL | NULL | NULL | 331143 | Using where; Using join buffer (flat, BNL join) |
+------+-------------+-------+-------+---------------+---------+---------+------+--------+-------------------------------------------------+
EXPLAIN
SELECT *
FROM dept_emp de
, employees e
WHERE de.from_date > '2001-01-01'
AND e.emp_no > 109004
;
+------+-------------+-------+-------+---------------+-------------+---------+------+--------+-------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+-------------+---------+------+--------+-------------------------------------------------+
| 1 | SIMPLE | de | range | ix_fromdate | ix_fromdate | 3 | NULL | 5324 | Using index condition |
| 1 | SIMPLE | e | range | PRIMARY | PRIMARY | 4 | NULL | 149711 | Using where; Using join buffer (flat, BNL join) |
+------+-------------+-------+-------+---------------+-------------+---------+------+--------+-------------------------------------------------+
SET join_cache_level=8;
SET optimizer_switch='mrr=on';
SET optimizer_switch='mrr_sort_keys=on';
EXPLAIN
SELECT *
FROM dept_emp de
, departments d
WHERE de.dept_no = d.dept_no
;
+------+-------------+-------+-------+---------------+-------------+---------+---------------------+-------+-------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+-------------+---------+---------------------+-------+-------------------------------------------------------+
| 1 | SIMPLE | d | index | PRIMARY | ux_deptname | 122 | NULL | 9 | Using index |
| 1 | SIMPLE | de | ref | PRIMARY | PRIMARY | 12 | employees.d.dept_no | 20696 | Using join buffer (flat, BKAH join); Key-ordered scan |
+------+-------------+-------+-------+---------------+-------------+---------+---------------------+-------+-------------------------------------------------------+
EXPLAIN
SELECT *
FROM dept_emp de
, employees e
WHERE de.emp_no = e.emp_no
AND de.dept_no = 'd005'
;
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+--------+-------------+
| 1 | SIMPLE | de | ref | PRIMARY,ix_empno_fromdate | PRIMARY | 12 | const | 165571 | Using where |
| 1 | SIMPLE | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.de.emp_no | 1 | |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+--------+-------------+
- 강좌 URL : http://www.gurubee.net/lecture/4198
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.