Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 커버링 인덱스 적용으로 장소 검색 쿼리 성능 최적화 #24

Merged
merged 4 commits into from
Sep 29, 2023

Conversation

yxxnghwan
Copy link
Contributor

Place 검색 쿼리 최적화를 위해 인덱스 적용 방안 고민

  1. 최소한의 조건절 인덱스만 걸고 ICP(index condition pushdown) 사용
  2. 커버링 인덱스 사용

결과 2번 채택
이유: 수많은 실험 결과 조건이 구체적이고 결과 row 수가 적을 수록 1번이 빠르지만, 검색 범위가 넓고 결과 갯수가 많을 수록 점점 느려짐. 반면에 2번의 경우 꾸준하게 속도가 빠름. 어쨌든 랜덤 I/O가 발생하지 않는 커버링 인덱스가 조금 더 안정적으로 빠르다고 판단.
인덱스에 잡히는 컬럼이 많을 수록 쓰기 속도에 지연이 걱정될 수 있지만, 해당 테이블에 데이터가 삽입되는 일이 그리 많지 않기 때문에 조회성능의 안정성을 우선으로 선택. 2번의 경우 꾸준히 100ms대로 조회가 되지만, 1번의 경우 100ms ~ 400ms 대로 쿼리와 결과에 따라 응답속도가 불안정.

실험

실험 DB에 row 105만 개 적재 후 테스트.

인덱스 미적용 결과

select
    id, name, address, latitude, longitude, phone, thumbnail_image_url
from place
where (latitude between 90 and 90)
and (longitude between 60 and 60)
and name like '%테스트1113%'
limit 500;

image

5403ms

인덱스 생성

create index ix__place__for_search on place(latitude, longitude, name, phone, address, thumbnail_image_url); # 커버링 인덱스
create index ix__place__for_search2 on place(latitude, longitude, name); # ICP 활용

ICP 활용

select
    id, name, address, latitude, longitude, phone, thumbnail_image_url
from place use index(ix__place__for_search2)
where (latitude between 90 and 90)
and (longitude between 60 and 60)
and name like '%테스트1113%'
limit 500;

스크린샷 2023-09-29 오후 11 11 54

실행계획

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "2936.34"
    },
    "table": {
      "table_name": "place",
      "access_type": "ref",
      "possible_keys": [
        "ix__place__for_search2"
      ],
      "key": "ix__place__for_search2",
      "used_key_parts": [
        "latitude",
        "longitude"
      ],
      "key_length": "18",
      "ref": [
        "const",
        "const"
      ],
      "rows_examined_per_scan": 2909,
      "rows_produced_per_join": 323,
      "filtered": "11.11",
      "index_condition": "((`chess`.`place`.`latitude` between 90 and 90) and (`chess`.`place`.`longitude` between 60 and 60) and (`chess`.`place`.`name` like '%테스트1113%'))",
      "cost_info": {
        "read_cost": "2645.45",
        "eval_cost": "32.32",
        "prefix_cost": "2936.35",
        "data_read_per_join": "1M"
      },
      "used_columns": [
        "id",
        "name",
        "address",
        "latitude",
        "longitude",
        "phone",
        "thumbnail_image_url"
      ]
    }
  }
}

조건절 범위 넓혀서 실행

select
    id, name, address, latitude, longitude, phone, thumbnail_image_url
from place use index(ix__place__for_search2)
where (latitude between 90 and 92)
and (longitude between 60 and 62)
and name like '%테스트11%'
limit 500;

스크린샷 2023-09-29 오후 11 14 29

실행 계획

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "37033.34"
    },
    "table": {
      "table_name": "place",
      "access_type": "range",
      "possible_keys": [
        "ix__place__for_search2"
      ],
      "key": "ix__place__for_search2",
      "used_key_parts": [
        "latitude"
      ],
      "key_length": "18",
      "rows_examined_per_scan": 39792,
      "rows_produced_per_join": 491,
      "filtered": "1.23",
      "index_condition": "((`chess`.`place`.`latitude` between 90 and 92) and (`chess`.`place`.`longitude` between 60 and 62) and (`chess`.`place`.`name` like '%테스트11%'))",
      "using_MRR": true,
      "cost_info": {
        "read_cost": "36984.23",
        "eval_cost": "49.12",
        "prefix_cost": "37033.34",
        "data_read_per_join": "1M"
      },
      "used_columns": [
        "id",
        "name",
        "address",
        "latitude",
        "longitude",
        "phone",
        "thumbnail_image_url"
      ]
    }
  }
}

커버링 인덱스 사용

select
    id, name, address, latitude, longitude, phone, thumbnail_image_url
from place use index(ix__place__for_search)
where (latitude between 90 and 90)
and (longitude between 60 and 60)
and name like '%테스트1113%'
limit 500;

스크린샷 2023-09-29 오후 11 17 15

실행계획

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "2465.47"
    },
    "table": {
      "table_name": "place",
      "access_type": "ref",
      "possible_keys": [
        "ix__place__for_search"
      ],
      "key": "ix__place__for_search",
      "used_key_parts": [
        "latitude",
        "longitude"
      ],
      "key_length": "18",
      "ref": [
        "const",
        "const"
      ],
      "rows_examined_per_scan": 5688,
      "rows_produced_per_join": 631,
      "filtered": "11.11",
      "using_index": true,
      "cost_info": {
        "read_cost": "1896.67",
        "eval_cost": "63.19",
        "prefix_cost": "2465.47",
        "data_read_per_join": "2M"
      },
      "used_columns": [
        "id",
        "name",
        "address",
        "latitude",
        "longitude",
        "phone",
        "thumbnail_image_url"
      ],
      "attached_condition": "((`chess`.`place`.`latitude` between 90 and 90) and (`chess`.`place`.`longitude` between 60 and 60) and (`chess`.`place`.`name` like '%테스트1113%'))"
    }
  }
}

범위 넓혀서 진행

select
    id, name, address, latitude, longitude, phone, thumbnail_image_url
from place use index(ix__place__for_search)
where (latitude between 90 and 92)
and (longitude between 60 and 62)
and name like '%테스트1%'
limit 500;

스크린샷 2023-09-29 오후 11 20 05

실행 시간에 유의미한 차이가 없음

실행계획

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "21616.68"
    },
    "table": {
      "table_name": "place",
      "access_type": "range",
      "possible_keys": [
        "ix__place__for_search"
      ],
      "key": "ix__place__for_search",
      "used_key_parts": [
        "latitude"
      ],
      "key_length": "18",
      "rows_examined_per_scan": 40530,
      "rows_produced_per_join": 500,
      "filtered": "1.23",
      "using_index": true,
      "cost_info": {
        "read_cost": "21566.65",
        "eval_cost": "50.03",
        "prefix_cost": "21616.68",
        "data_read_per_join": "1M"
      },
      "used_columns": [
        "id",
        "name",
        "address",
        "latitude",
        "longitude",
        "phone",
        "thumbnail_image_url"
      ],
      "attached_condition": "((`chess`.`place`.`latitude` between 90 and 92) and (`chess`.`place`.`longitude` between 60 and 62) and (`chess`.`place`.`name` like '%테스트1%'))"
    }
  }
}

@yxxnghwan yxxnghwan self-assigned this Sep 29, 2023
@github-actions
Copy link

github-actions bot commented Sep 29, 2023

Unit Test Results

152 tests   152 ✔️  15s ⏱️
  46 suites      0 💤
  46 files        0

Results for commit 4c0673c.

♻️ This comment has been updated with latest results.

@yxxnghwan yxxnghwan merged commit 4ebfe4b into develop Sep 29, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant