Engine Atlas

MySQL 조인 알고리즘

Indexed Nested Loop와 Hash Join의 plan, row flow, evidence
MySQL 8.4.10captured
English
학습 안내

이 Lab에서 답할 질문

같은 join 결과를 만들 때 nested-loop와 hash join은 어느 input을 반복 탐색하고 어느 input을 build/probe하나요?

실행 엔진: MySQL 8.4.10
누가 실행하나요?

하나의 Session이 같은 join SELECT를 nested-loop와 hash join capture variant에서 실행합니다.

무엇을 고정하나요?

동일한 두 input table, SQL, 반환 rows와 checksum을 고정하되 indexed variant에만 idx_join_orders_customer_status가 있는 capture condition을 비교합니다.

실행 SQL
SELECT
  c.id AS customer_id,
  c.segment,
  o.id AS order_id,
  o.status,
  o.total_cents
FROM join_customers AS c
JOIN join_orders AS o
  ON o.customer_code = c.customer_code
WHERE c.region_code = 'KR'
  AND o.status = 'PAID';
무엇이 달라지나요?

idx_join_orders_customer_status availability, operator 구조, outer/inner 반복, hash build/probe 단계와 관련 statement counters가 달라집니다.

무엇이 그대로인가요?

join predicate와 matched result rows 및 checksum은 두 algorithm variant에서 동일합니다.

먼저 예측해 보세요

hash join을 선택한 variant에서 새로 추적할 두 역할은 무엇인가요?

hash join을 선택한 variant에서 새로 추적할 두 역할은 무엇인가요?

증거 미션

hash join variant의 match flow 단계를 열어 어느 input이 build고 어느 input이 probe인지 확인하세요.

어디를 보나요?
Join lens에서 build side와 probe side 표시를, plan 쪽에서는 Hash operator와 Inner hash join operator를 함께 봅니다.
이동할 화면
Hash Join · match flow
무엇이 보여야 하나요?
확인할 관찰

hash-join capture에는 Hash operator가 join의 직접 child로 있고 index lookup operator는 없습니다. idx_join_orders_customer_status는 indexed-nested-loop capture 조건에만 존재하며, 두 variant 모두 join output 257행을 기록합니다.

근거 경계
Captured
hash-join plan의 Hash child와 Inner hash join operator 구조, 그리고 parent join output 257행
Derived · fixture
customer_code로 연결한 JOIN_PAIRS membership
Checkpoint · evidence로 설명하기두 variant의 captured completion time만으로 한 algorithm이 일반적으로 우월하다고 결론내릴 수 있나요?답과 근거 확인

아닙니다. 이 capture는 해당 fixture와 server state의 실행 evidence입니다. algorithm 구조와 row flow를 설명할 수 있지만 모든 workload의 성능 순위를 일반화할 수는 없습니다.

근거 경계
Captured
plan 구조, row flow, counters와 completion time
이번 Lab의 핵심

join algorithm 비교는 이름이 아니라 어떤 input이 반복되고, build되며, probe되는지와 동일 결과가 어떻게 조립되는지로 읽어야 합니다.

다음 연결

다음 Lab에서는 join이나 aggregation 뒤의 중간 rows가 internal temporary table과 sort storage로 어떻게 이어지는지 봅니다.

용어 확인
nested-loop join처음 다루는 Lab 04
outer input의 row마다 inner input에서 match를 찾는 join 실행 방식입니다.
hash build / probe처음 다루는 Lab 04
한쪽 join input으로 hash table을 build하고 다른 input의 join key로 probe해 match를 찾는 두 단계입니다.

01 · SOURCE

두 variant가 공유하는 SQL

1 active fragmentMySQLJOIN · no ORDER BY
SELECT c.id AS customer_id, c.segment, o.id AS order_id, o.status, o.total_cents
FROM join_customers AS c
JOIN join_orders AS o ON o.customer_code = c.customer_code
WHERE c.region_code = 'KR'
  AND o.status = 'PAID';
02 · FLOW

Native plan과 actual iterator

01 / 12
01/ 12