EXPLAIN
SELECT *
FROM employees
WHERE first_name = 'Matt' -- 233 건
AND hire_date BETWEEN '1995-01-01' AND '2000-01-01' -- 34028 건
;
+------+-------------+-----------+------+--------------------------+--------------+---------+-------+------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+------+--------------------------+--------------+---------+-------+------+------------------------------------+
| 1 | SIMPLE | employees | ref | ix_firstname,ix_hiredate | ix_firstname | 44 | const | 233 | Using index condition; Using where |
+------+-------------+-----------+------+--------------------------+--------------+---------+-------+------+------------------------------------+
EXPLAIN
SELECT *
FROM employees
WHERE first_name BETWEEN 'A' AND 'B' -- 22039 건
AND hire_date BETWEEN '1995-01-01' AND '2000-01-01' -- 34028 건
;
+------+-------------+-----------+-------+--------------------------+--------------+---------+------+-------+------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+-------+--------------------------+--------------+---------+------+-------+------------------------------------+
| 1 | SIMPLE | employees | range | ix_firstname,ix_hiredate | ix_firstname | 44 | NULL | 43234 | Using index condition; Using where |
+------+-------------+-----------+-------+--------------------------+--------------+---------+------+-------+------------------------------------+
MariaDB [employees]> SET optimizer_switch = 'index_merge_sort_intersection = on'; -- 공백 때문에 에러 발생
ERROR 1231 (42000): Variable 'optimizer_switch' can't be set to the value of 'index_merge_sort_intersection = on'
MariaDB [employees]> SET optimizer_switch = 'index_merge_sort_intersection=on'; -- 공백 지우고 정상 수행
Query OK, 0 rows affected (0.00 sec)
EXPLAIN
SELECT *
FROM employees
WHERE first_name BETWEEN 'A' AND 'B' -- 22039 건
AND hire_date BETWEEN '1995-01-01' AND '2000-01-01' -- 34028 건
;
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+-------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+-------------------------------------------------------------+
| 1 | SIMPLE | employees | index_merge | ix_firstname,ix_hiredate | ix_firstname,ix_hiredate | 44,3 | NULL | 9912 | Using sort_intersect(ix_firstname,ix_hiredate); Using where |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+-------------------------------------------------------------+
EXPLAIN
SELECT *
FROM employees
WHERE first_name = 'Matt' -- 233 건
OR hire_date = '1987-03-31' -- 111 건
;
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+----------------------------------------------------+
| 1 | SIMPLE | employees | index_merge | ix_firstname,ix_hiredate | ix_firstname,ix_hiredate | 44,3 | NULL | 344 | Using union(ix_firstname,ix_hiredate); Using where |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+----------------------------------------------------+
EXPLAIN
SELECT *
FROM employees
WHERE first_name LIKE '%Matt' -- 233 건
OR hire_date = '1987-03-31' -- 111 건
;
+------+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | employees | ALL | ix_hiredate | NULL | NULL | NULL | 299423 | Using where |
+------+-------------+-----------+------+---------------+------+---------+------+--------+-------------+
SELECT *
FROM employees
WHERE first_name = 'Matt' -- 233 건
OR hire_date = '1987-03-31' -- 111 건
LIMIT 10
;
+--------+------------+------------+-----------+--------+------------+
| emp_no | birth_date | first_name | last_name | gender | hire_date |
+--------+------------+------------+-----------+--------+------------+
| 10083 | 1959-07-23 | Vishv | Zockler | M | 1987-03-31 |
| 10690 | 1962-09-06 | Matt | Jumpertz | F | 1989-08-22 |
| 12302 | 1962-02-14 | Matt | Plessier | M | 1987-01-28 |
| 13163 | 1963-12-11 | Matt | Heping | F | 1987-03-31 |
| 13233 | 1954-08-11 | Alselm | Chvatal | M | 1987-03-31 |
| 13507 | 1959-09-05 | Matt | Wallrath | M | 1985-06-28 |
| 14004 | 1959-04-10 | Arto | Wendorf | M | 1987-03-31 |
| 15456 | 1957-09-27 | Matt | Granlund | M | 1987-08-17 |
| 15577 | 1961-02-15 | Matt | Menhardt | M | 1985-10-26 |
| 15696 | 1959-05-06 | Matt | Zhang | F | 1987-08-21 |
+--------+------------+------------+-----------+--------+------------+
EXPLAIN
SELECT *
FROM employees
WHERE first_name = 'Matt' -- 233 건
OR hire_date BETWEEN '1987-03-01' AND '1987-03-31' -- 2964 건
;
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+---------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+---------------------------------------------------------+
| 1 | SIMPLE | employees | index_merge | ix_firstname,ix_hiredate | ix_firstname,ix_hiredate | 44,3 | NULL | 3196 | Using sort_union(ix_firstname,ix_hiredate); Using where |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+---------------------------------------------------------+
EXPLAIN
SELECT COUNT(*)
FROM employees
WHERE first_name = 'Matt' -- 233 건
AND hire_date = '1991-07-22' -- 47 건
;
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+---------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+---------------------------------------------------------------------+
| 1 | SIMPLE | employees | index_merge | ix_firstname,ix_hiredate | ix_hiredate,ix_firstname | 3,44 | NULL | 1 | Using intersect(ix_hiredate,ix_firstname); Using where; Using index |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+---------------------------------------------------------------------+
EXPLAIN
SELECT COUNT(*)
FROM employees
WHERE first_name LIKE 'Matt%' -- 233 건
AND hire_date = '1991-07-22' -- 47 건
;
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+-------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+-------------------------------------------------------------+
| 1 | SIMPLE | employees | index_merge | ix_firstname,ix_hiredate | ix_hiredate,ix_firstname | 3,44 | NULL | 1 | Using sort_intersect(ix_hiredate,ix_firstname); Using where |
+------+-------------+-----------+-------------+--------------------------+--------------------------+---------+------+------+-------------------------------------------------------------+
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 | |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+--------+-------------+
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT de.emp_no FROM dept_emp de WHERE de.dept_no = 'd009')
;
+------+--------------------+-------+--------+---------+------------+--------+--------------------------+
| id | select_type | table | type | key | ref | rows | Extra |
+------+--------------------+-------+--------+---------+------------+--------+--------------------------+
| 1 | PRIMARY | e | ALL | NULL | NULL | 300252 | Using where |
| 2 | DEPENDENT SUBQUERY | de | eq_ref | PRIMARY | const,func | 1 | Using where; Using index |
+------+--------------------+-------+--------+---------+------------+--------+--------------------------+
-- 책 내용 옮겨 적은 거
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT de.emp_no FROM dept_emp de WHERE de.dept_no = 'd009')
;
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+-------+--------------------------+
| 1 | PRIMARY | de | ref | PRIMARY,ix_empno_fromdate | PRIMARY | 12 | const | 46914 | Using where; Using index |
| 1 | PRIMARY | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.de.emp_no | 1 | |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+-------+--------------------------+
EXPLAIN EXTENDED
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT de.emp_no FROM dept_emp de WHERE de.dept_no = 'd009')
;
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+-------+----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+-------+----------+--------------------------+
| 1 | PRIMARY | de | ref | PRIMARY,ix_empno_fromdate | PRIMARY | 12 | const | 46914 | 100.00 | Using where; Using index |
| 1 | PRIMARY | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.de.emp_no | 1 | 100.00 | |
+------+-------------+-------+--------+---------------------------+---------+---------+---------------------+-------+----------+--------------------------+
2 rows in set, 1 warning (0.00 sec)
SHOW warnings;
+-------+------+-----------------------------------------------------------------+
| Level | Code | Message |
+-------+------+-----------------------------------------------------------------+
| Note | 1003 | select `employees`.`e`.`emp_no` AS `emp_no` |
| | | , `employees`.`e`.`birth_date` AS `birth_date` |
| | | , `employees`.`e`.`first_name` AS `first_name` |
| | | , `employees`.`e`.`last_name` AS `last_name` |
| | | , `employees`.`e`.`gender` AS `gender` |
| | | , `employees`.`e`.`hire_date` AS `hire_date` |
| | | from `employees`.`dept_emp` `de` |
| | | join `employees`.`employees` `e` |
| | | where ( (`employees`.`e`.`emp_no` = `employees`.`de`.`emp_no`) |
| | | and (`employees`.`de`.`dept_no` = 'd009') ) |
+-------+------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [employees]>
EXPLAIN
SELECT *
FROM employees e
WHERE e.first_name = 'Matt'
AND e.emp_no IN (SELECT t.emp_no
FROM titles t
WHERE t.from_date BETWEEN '1995-01-01' AND '1995-01-30'
)
;
+------+-------------+-------+------+----------------------+--------------+---------+--------------------+------+-----------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+------+----------------------+--------------+---------+--------------------+------+-----------------------------------------+
| 1 | PRIMARY | e | ref | PRIMARY,ix_firstname | ix_firstname | 44 | const | 233 | Using index condition |
| 1 | PRIMARY | t | ref | PRIMARY | PRIMARY | 4 | employees.e.emp_no | 1 | Using where; Using index; FirstMatch(e) |
+------+-------------+-------+------+----------------------+--------------+---------+--------------------+------+-----------------------------------------+
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT t.emp_no
FROM titles t
WHERE t.from_date BETWEEN '1995-01-01' AND '1995-01-30'
)
;
+------+--------------+-------------+--------+---------------+--------------+---------+------+--------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+---------------+--------------+---------+------+--------+--------------------------+
| 1 | PRIMARY | e | ALL | PRIMARY | NULL | NULL | NULL | 299423 | |
| 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | |
| 2 | MATERIALIZED | t | index | PRIMARY | PRIMARY | 159 | NULL | 442189 | Using where; Using index |
+------+--------------+-------------+--------+---------------+--------------+---------+------+--------+--------------------------+
ALTER TABLE titles ADD INDEX ix_fromdate(from_date);
Query OK, 0 rows affected (48.29 sec)
Records: 0 Duplicates: 0 Warnings: 0
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT t.emp_no
FROM titles t
WHERE t.from_date BETWEEN '1995-01-01' AND '1995-01-30'
)
;
+------+--------------+-------------+--------+---------------------+-------------+---------+--------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+---------------------+-------------+---------+--------------------+------+--------------------------+
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 2689 | |
| 1 | PRIMARY | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.t.emp_no | 1 | |
| 2 | MATERIALIZED | t | range | PRIMARY,ix_fromdate | ix_fromdate | 3 | NULL | 2689 | Using where; Using index |
+------+--------------+-------------+--------+---------------------+-------------+---------+--------------------+------+--------------------------+
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT t.emp_no
FROM titles t
WHERE t.from_date BETWEEN '1995-01-01' AND '1995-01-30'
GROUP BY t.title
)
;
+------+--------------+-------------+--------+---------------------+-------------+---------+--------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+---------------------+-------------+---------+--------------------+------+--------------------------+
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 2689 | |
| 1 | PRIMARY | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.t.emp_no | 1 | |
| 2 | MATERIALIZED | t | range | PRIMARY,ix_fromdate | ix_fromdate | 3 | NULL | 2689 | Using where; Using index |
+------+--------------+-------------+--------+---------------------+-------------+---------+--------------------+------+--------------------------+
EXPLAIN
SELECT *
FROM departments d -- 9건
WHERE d.dept_no IN (SELECT de.dept_no
FROM dept_emp de -- 33만건
)
;
+------+--------------+-------------+--------+---------------+--------------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+---------------+--------------+---------+------+--------+-------------+
| 1 | PRIMARY | d | index | PRIMARY | ux_deptname | 122 | NULL | 9 | Using index |
| 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 12 | func | 1 | |
| 2 | MATERIALIZED | de | index | PRIMARY | ix_fromdate | 3 | NULL | 331143 | Using index |
+------+--------------+-------------+--------+---------------+--------------+---------+------+--------+-------------+
SET optimizer_switch='materialization=off';
EXPLAIN
SELECT *
FROM departments d -- 9건
WHERE d.dept_no IN (SELECT de.dept_no
FROM dept_emp de -- 33만건
)
;
+------+-------------+-------+-------+---------------+-------------+---------+---------------------+-------+----------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+-------+---------------+-------------+---------+---------------------+-------+----------------------------+
| 1 | PRIMARY | d | index | PRIMARY | ux_deptname | 122 | NULL | 9 | Using index |
| 1 | PRIMARY | de | ref | PRIMARY | PRIMARY | 12 | employees.d.dept_no | 20696 | Using index; FirstMatch(d) |
+------+-------------+-------+-------+---------------+-------------+---------+---------------------+-------+----------------------------+
SET optimizer_switch='FirstMatch=off';
EXPLAIN
SELECT *
FROM departments d -- 9건
WHERE d.dept_no IN (SELECT de.dept_no
FROM dept_emp de -- 33만건
)
;
+------+-------------+-------+--------+---------------+---------+---------+----------------------+--------+------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+---------------+---------+---------+----------------------+--------+------------------------+
| 1 | PRIMARY | de | index | PRIMARY | PRIMARY | 16 | NULL | 331143 | Using index; LooseScan |
| 1 | PRIMARY | d | eq_ref | PRIMARY | PRIMARY | 12 | employees.de.dept_no | 1 | |
+------+-------------+-------+--------+---------------+---------+---------+----------------------+--------+------------------------+
SELECT * FROM t1 WHERE c IN (SELECT key1 FROM t2 WHERE ...);
SELECT * FROM t1 WHERE c IN (SELECT key2 FROM t2 WHERE key1 = '상수' AND ...);
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT s.emp_no
FROM salaries s
WHERE s.salary > 150000
)
;
+------+--------------+-------------+--------+-------------------+-----------+---------+--------------------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------+-------------+--------+-------------------+-----------+---------+--------------------+------+--------------------------+
| 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 36 | |
| 1 | PRIMARY | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.s.emp_no | 1 | |
| 2 | MATERIALIZED | s | range | PRIMARY,ix_salary | ix_salary | 4 | NULL | 36 | Using where; Using index |
+------+--------------+-------------+--------+-------------------+-----------+---------+--------------------+------+--------------------------+
SET optimizer_switch='materialization=off';
EXPLAIN
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT s.emp_no
FROM salaries s
WHERE s.salary > 150000
)
;
+------+-------------+-------+--------+-------------------+-----------+---------+--------------------+------+-------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+-------+--------+-------------------+-----------+---------+--------------------+------+-------------------------------------------+
| 1 | PRIMARY | s | range | PRIMARY,ix_salary | ix_salary | 4 | NULL | 36 | Using where; Using index; Start temporary |
| 1 | PRIMARY | e | eq_ref | PRIMARY | PRIMARY | 4 | employees.s.emp_no | 1 | End temporary |
+------+-------------+-------+--------+-------------------+-----------+---------+--------------------+------+-------------------------------------------+
SELECT *
FROM t
WHERE expr1 IN (SELECT ...)
OR expr2
;
SELECT *
FROM t
WHERE expr1 NOT IN (SELECT ...)
;
SELECT (SELECT ...)
FROM t
;
SELECT *
FROM t
HAVING expr1 IN (SELECT ...)
;
SELECT *
FROM t
WHERE expr1 IN (SELECT ... UNION SELECT ...)
EXPLAIN EXTENDED
SELECT *
FROM employees e
WHERE e.emp_no IN (SELECT de.emp_no
FROM dept_emp de
WHERE de.dept_no = 'd009'
)
OR e.first_name = 'Matt'
;
+------+--------------------+-------+--------+---------------------------------------+---------+---------+------------+--------+----------+------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+------+--------------------+-------+--------+---------------------------------------+---------+---------+------------+--------+----------+------------------------+
| 1 | PRIMARY | e | ALL | ix_firstname | NULL | NULL | NULL | 299423 | 100.00 | Using where |
| 2 | DEPENDENT SUBQUERY | de | eq_ref | PRIMARY,ix_fromdate,ix_empno_fromdate | PRIMARY | 16 | const,func | 1 | 100.00 | Using where; Usingndex |
+------+--------------------+-------+--------+---------------------------------------+---------+---------+------------+--------+----------+------------------------+
2 rows in set, 1 warning (0.00 sec)
MariaDB [employees]> SHOW WARNINGS;
+-------+------+---------------------------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+---------------------------------------------------------------------------------------+
| Note | 1003 | select `employees`.`e`.`emp_no` AS `emp_no` |
| | | , `employees`.`e`.`birth_date` AS `birth_date` |
| | | , `employees`.`e`.`first_name` AS `first_name` |
| | | , `employs`.`e`.`last_name` AS `last_name` |
| | | , `employees`.`e`.`gender` AS `gender` |
| | | , `employees`.`e`.`hire_date` AS `hire_date` |
| | | from `employees`.`employees` `e` |
| | | where (<expcache><`employees`.`e`.`emp_no`>(<in_optimizer>(`employees`.`e`.`emp_no`, |
| | | <exists>(select `employees`.`de`.`emp_no` |
| | | from `employees`.`dept_emp` `de` |
| | | where ((`eloyees`.`de`.`dept_no` = 'd009') |
| | | and (<cache>(`employees`.`e`.`emp_no`) = `employees`.`de`.`emp_no`))))) |
| | | or (`employees`.`e`.`first_name` = 'Matt')) |
+-------+------+---------------------------------------------------------------------------------------+