Study/Real-MySQL

10.2 실행 계획 확인

hongeeii 2024. 2. 13. 22:37
728x90
반응형

실행 계획은 DESC 또는 EXPLAIN 명령으로 확인 가능.

8.0 버전 부터는 EXPLAIN 명령에 옵션이 추가되어 출력 포맷과 실제 쿼리의 실행 결과까지 확인 할 수 있음.

10.2.1 실행 계획 출력 포맷

이전 버전에 있던 EXPLAIN EXTENDED 와 EXPLAIN PARTITIONS 명령이 8.0 버전 부터는 제거됐다.

8.0 부터는 FORMAT 옵션을 통해 출력 포맷을 정의할 수 있음.

-- 기본
explain
select *
from employees e
join salaries s 
    on s.emp_no = e.emp_no
where first_name = 'ABC';

image

-- 트리 포맷
explain format=tree
select *
from employees e
join salaries s 
    on s.emp_no = e.emp_no
where first_name = 'ABC';

-> Nested loop inner join  (cost=3.08 rows=10)
     -> Index lookup on e using ix_firstname (first_name='ABC')  (cost=1.10 rows=1)
     -> Index lookup on s using PRIMARY (emp_no=e.emp_no)  (cost=1.98 rows=10)
-- json 포맷
explain format=json
select *
from employees e
join salaries s 
    on s.emp_no = e.emp_no
where first_name = 'ABC';

{
   "query_block": {
     "select_id": 1,
     "cost_info": {
       "query_cost": "3.08"
     },
     "nested_loop": [
       {
         "table": {
           "table_name": "e",
           "access_type": "ref",
           "possible_keys": [
             "PRIMARY",...


10.2.2 쿼리의 실행 시간 확인

8.0.18 버전부터 쿼리의 실행 계획가 단계별 소요된 시간 정보를 확인할 수 있는 EXPLAIN ANALYZE 기능이 추가

SHOW PROFILE 명령으로 어떤 부분에서 시간이 많이 소요되는지 확인 할 수 있지만

실행 계획의 단계별로 소요된 시간 정보는 없다.

EXPLAIN ANALYZE 는 항상 TREE 형태로 출력되기 때문에 FORMAT 옵션을 붙일 수 없음

explain analyze
select e.emp_no, avg(s.salary)
from employees e
join salaries s 
    on s.emp_no = e.emp_no
    and s.salary > 50000
    and s.from_date <= '1990-01-01'
    and s.to_date > '1990-01-01'
where first_name = 'Matt'
group by e.hire_date;

-> Table scan on <temporary>  (actual time=0.001..0.003 rows=48 loops=1)
    -> Aggregate using temporary table  (actual time=2.218..2.223 rows=48 loops=1)
        -> Nested loop inner join  (cost=669.43 rows=126) (actual time=0.289..2.165 rows=48 loops=1)
            -> Index lookup on e using ix_firstname (first_name='Matt')  (cost=215.08 rows=233) (actual time=0.273..0.735 rows=233 loops=1)
            -> Filter: ((s.salary > 50000) and (s.from_date <= DATE'1990-01-01') and (s.to_date > DATE'1990-01-01'))  (cost=0.98 rows=1) (actual time=0.005..0.006 rows=0 loops=233)
                -> Index lookup on s using PRIMARY (emp_no=e.emp_no)  (cost=0.98 rows=10) (actual time=0.004..0.005 rows=10 loops=233)

Tree 포맷의 실행계획에서 들여쓰기는 호출순서를 의미

들여쓰기가 같은 레벨에서는 상단에 위치한 라인이 먼저 실행

들여쓰기가 다른 레벨에서는 가장 안쪽에 위치한 라인이 먼저 실행

EXPLAIN ANALYZE 명령의 결과에는 단계별로 소요된 시간(actual time)

처리한 레코드 건수(rows), 반복 횟수(loops) 가 표시

rows와 actual time은 평균 을 나타내는데 loops 가 1 이상이기 때문이다.

728x90
반응형