본문 바로가기

use-the-index-luke

A.2-3 (MySQL) Access vs. filter predicates

use-the-index-luke 저자 Markus Winand


Markus Winand는 SQL에 대한 통찰력을 제공하고 다양한 시스템이 SQL을 지원하는 방법을 modern-sql.com 에서 보여줍니다. 이전에 그는 use-the-index-luke.com 을 만들었는데, 지금도 활발하게 유지되고 있습니다. Markus는 winand.at 를 통해 강사, 연사 및 컨설턴트로 고용될 수 있습니다.


You can upload a Korean translation of use-the-index-luke.com on your blog

Thank you from the bottom of my heart to author Makus Winand for allowing me.

These are translations that I use for studying by using a papago (google translate)

The translations may not be correct or there may be a typo.

I'd appreciate it if you could point it out in the comments.



use-the-index-luke.com 의 한글번역본을 블로그에 업로드 해도 된다고

허락해주신 Makus Winand 저자님께 진심으로 감사합니다.

이 번역본들은 제가 공부용도로 번역기(papago, google transrate)를 돌려서

번역한 내용들이라 맞지 않거나, 오타가 있을수 있습니다.

댓글로 지적해주시면 감사하겠습니다.


https://use-the-index-luke.com/sql/explain-plan/mysql/access-filter-predicates

ㄴaccess 및 filter prdicate 구분

MySQL 데이터베이스는 세 가지 방법으로 where clauses (predicates)를 평가합니다.

Access predicate ("key_len","ref" 열)

access predicate는 leaf node travelsal의 시작및 중지 조건을 나타냅니다.

Index filter predicate("Using index condition", since MySQL 5.6)

Index filter predicate는 leaf node travelsal중에만 적용됩니다. 시작 및 중지 조건에 영향을 주지 않으며 스캔 범위를 좁히지 않습니다.

Table level filter predicate("Extra"열에서 "Using where")

인덱스의 일부가 아닌 column의 predicate는 테이블 수준에서 평가됩니다. 이렇게 하려면 데이터베이스가 먼저 테이블에서 행을 로드해야합니다.

MySQL 실행 계획은 각 조건에 사용되는 predicate 유형을 표시하지 않고 사용 중인 predicate 유형만 나열합니다.

다음 예제에서는 전체 where 절이 accesspredicate로 사용됩니다.


CREATE TABLE demo ( id1 NUMERIC, id2 NUMERIC, id3 NUMERIC, val NUMERIC)

INSERT INTO demo VALUES (1,1,1,1)

INSERT INTO demo VALUES (2,2,2,2)

CREATE INDEX demo_idx ON demo (id1, id2, id3)

EXPLAINSELECT * FROM demo WHERE id1=1 AND id2=1

+------+----------+---------+-------------+------+-------+ | type | key | key_len | ref | rows | Extra | +------+----------+---------+-------------+------+-------+ | ref | demo_idx | 12 | const,const | 1 | | +------+----------+---------+-------------+------+-------+


"Extra"열에 "Using where" or "Using index condition" 이 표시되지 않습니다. 그러나 인덱스는 사용되므로(type=ref, key=ref_idx) where 절 전체가 access predicate로 적합하다고 가정할 수 있습니다.'

또한 참조 열은 인덱스에서 두 개의 열이 사용되었음을 나타냅니다(이 예제에서는 둘 다 쿼리 상수입니다).

인덱스의 어떤 부분이 사용되는지 확인하는 또 다른 방법은 key_len 값입니다: 쿼리가 인덱스 정의의 처음 12 바이트를 사용한다는 것을 보여줍니다. 이를 열 이름에 매핑하려면 각 열에 필요한 스토리지 공간을 "단순히" 알아야 합니다(MySQL 문서의 "데이터 유형 스토리지 요구 사항" 참조). NOT NULL 제약 조건이 없는 경우 MySQL 에는 각 열에 대해 추가 바이트가 필요합니다.

결국 예제에서는 각 숫자 열에 6바이트가 필요합니다.

따라서 키 길이 12는 처음 두 개의 인덱스 열이 access predicate로 사용됨을 확인합니다.

ID2 대신 ID3 열로 필터링 하는 경우

MySQL 5.6 이상에서 Index filter predicate("인덱스 조건 사용")를 사용합니다:


EXPLAINSELECT * FROM demo WHERE id1=1 AND id3=1

+------+----------+---------+-------+------+-----------------------+ | type | key | key_len | ref | rows | Extra | +------+----------+---------+-------+------+-----------------------+ | ref | demo_idx | 6 | const | 1 | Using index condition | +------+----------+---------+-------+------+-----------------------+


이 경우 ken_len =6 과 ref 열에 하나의 const만 있으면 하나의 인덱스 열만 access predicate로 사용됩니다.

이전 버전의 MySQL에서는 이 쿼리에 대해 테이블 수준 filter predicate를 사용했습니다.

이 predicate는 "Extra" 열에서 "Using where"으로 식별됩니다:


+------+----------+---------+-------+------+-------------+ | type | key | key_len | ref | rows | Extra | +------+----------+---------+-------+------+-------------+ | ref | demo_idx | 6 | const | 1 | Using where | +------+----------+---------+-------+------+-------------



Tip

Chapter3, “Performance and Scalability”에서는 filter predicate가 수행하는 성능 차이를 보여줍니다.


'use-the-index-luke' 카테고리의 다른 글

A.3-1 (Oracle) Getting  (1) 2023.12.15
A.3 Oracle  (0) 2023.12.13
A.2-2 (MySQL) Operations  (0) 2023.12.10
A.2-1 (MySQL) Getting  (0) 2023.12.06
A.2 MYSQL  (0) 2023.12.04