MySQL에서 Alias 사용 시에 불필요한 sorting 부하를 발생 시키지 않으려면
SELECT list에 있는 가공된 컬럼명과 구분하여 기존 테이블의 컬럼명 앞에 테이블 Alias를 사용하자
[튜닝 전]
order by first_name
order by 절에 Select list의 가공된 컬럼명 사용으로 불필요한 Sort 발생
explain analyze
select /*+INDEX(a employees_ix1) */
a.emp_no, a.birth_date, substr(a.first_name,1,3) first_name
from employees a
where hire_date = '1986-01-01'
order by first_name
limit 10
;
-> Limit: 10 row(s) (cost=28 rows=10) (actual time=0.25..0.251 rows=10 loops=1)
-> Sort: first_name, limit input to 10 row(s) per chunk (cost=28 rows=80) (actual time=0.249..0.25 rows=10 loops=1)
-> Index lookup on a using employees_ix1 (hire_date = DATE'1986-01-01') (cost=28 rows=80) (actual time=0.103..0.22 rows=80 loops=1)
[튜닝 후]
order by a.first_name
order by 절에 컬럼명 앞에 테이블 alias 사용으로 정렬 발생하지 않음
explain analyze
select /*+INDEX(a employees_ix1) */
a.emp_no, a.birth_date, substr(a.first_name,1,3) first_name
from employees a
where hire_date = '1986-01-01'
order by a.first_name
limit 10
;
-> Limit: 10 row(s) (cost=21 rows=10) (actual time=0.0981..0.1 rows=10 loops=1)
-> Index lookup on a using employees_ix1 (hire_date = DATE'1986-01-01') (cost=21 rows=60) (actual time=0.0975..0.0991 rows=10 loops=1)'MySQL > 튜닝' 카테고리의 다른 글
| MySQL ONLY_FULL_GROUP_BY (0) | 2024.12.27 |
|---|---|
| MySQL 조인절에 사용한 함수 > inline view 이용 (0) | 2024.12.26 |
| MySQL IFNULL 조건절에 사용 시 주의 (0) | 2024.12.24 |
| MySQL UNNESTING 불가 > INLINE View (0) | 2024.12.24 |
| MySQL Sub Query > Window Function (1) | 2024.12.24 |