employees
테이블에 (last_name, first_name)
인덱스가 생성되어 있을 때 아래 쿼리가 인덱스 컨디션 푸시 다운 유무에 따라 어떻게 작동하는지 살펴보자.SELECT * FROM employees
WHERE last_name='Action' AND first_name LIKE '%SAL';
id | table | type | key | key_len | Extra |
---|---|---|---|---|---|
1 | SIMPLE | employees | ix_lastname_firstname | 66 | Using where |
last_name=’Action’
인 레코드를 스토리지 엔진이 읽어서 반환first_name LIKE ‘%SAL’
조건(인덱스를 사용할 수 없는 조건)은 MySQL이 last_name=’Action’
인 레코드 중에서 검색first_name
의 조건은 인덱스를 사용할 수 없는 조건이지만 (last_name, first_name)
인덱스에는 first_name
이 존재하기 때문에 스토리지 엔진에서부터 조건에 맞는 레코드를 반환할 수 있지만, 그러지 않고 인덱스를 사용할 수 있는 조건에서만 검색 후 더 많은 레코드를 읽어 반환하는 문제가 있다.
last_name=’Action’
인 레코드가 10만건이고 그 중 1건만 first_name LIKE ‘%SAL’
조건을 만족한다면 99,999건의 불필요한 읽기가 발생하는 상황이다.id | table | type | key | key_len | Extra |
---|---|---|---|---|---|
1 | SIMPLE | employees | ix_lastname_firstname | 66 | Using index condition |
first_name LIKE ‘%SAL’
조건이 인덱스를 사용하지 못하는 조건이지만 MySQL 엔진은 스토리지 엔진으로 이 조건까지 전달한다.use_index_extensions
옵션은 InnoDB 스토리지 엔진에서 세컨더리 인덱스에 존재하는 프라이머리 키를 활용할 수 있게 할지 결정하는 옵션이다.