[SQL] Workbench Exaplain, Nested-Loop Join, Block Nested-Loop Join

June·2021년 8월 3일
0

Workbench Explain

SQL은 DBMS의 Optimizer를 통해서 최적화되게 되고, Workbench는 그 실행 계획을 보여준다.

워크벤치에서 번개모양에 돋보기가 있는 것을 클릭하고, 탭을 Visual Explain으로 바꾸면 아래와 같이 어떤 순서로 실행되는지 보여준다.

MySQL Workbench의 VISUAL EXPLAIN으로 인덱스 동작 확인하기

Nested-Loop join Algorithm

MySQL 5.6 Nested Loop Join

첫 번째 테이블에서 한번에 한 행씩 읽어오고, 조인을 하기 위해 각 행들을 다음 테이블을 처리하는 방식이다.

Table   Join Type
t1      range
t2      ref
t3      ALL
for each row in t1 matching range {
  for each row in t2 matching reference key {
    for each row in t3 {
      if row satisfies join conditions, send to client
    }
  }
}

Nested-Loop Join은 바깥 루프에서 안쪽 루프로 하나씩 행들을 가져오므로, 테이블을 많이 읽게된다.

Block Nested-Loop Join Algorithm

MySQL 5.6 Block Nested-Loop Join Algorithm

BNL은 바깥 루프들의 행들을 가져올때 버퍼를 가져온다. 예를 들어, 10개의 행이 버퍼에 읽히면, 버퍼는 다음 inner loop에서 버퍼 안의 행들에 대해 비교될 수 있다. 이 것은 안쪽 테이블이 읽혀야하는 횟수를 줄인다.

for each row in t1 matching range {
  for each row in t2 matching reference key {
    store used columns from t1, t2 in join buffer
    if buffer is full {
      for each row in t3 {
        for each t1, t2 combination in join buffer {
          if row satisfies join conditions, send to client
        }
      }
      empty join buffer
    }
  }
}

if buffer is not empty {
  for each row in t3 {
    for each t1, t2 combination in join buffer {
      if row satisfies join conditions, send to client
    }
  }
}

If S is the size of each stored t1, t2 combination in the join buffer and C is the number of combinations in the buffer, the number of times table t3 is scanned is:

(S * C)/join_buffer_size + 1

The number of t3 scans decreases as the value of join_buffer_size increases, up to the point when join_buffer_size is large enough to hold all previous row combinations. At that point, no speed is gained by making it larger.

Hash Join

MySQL 8부터 도입되었다.

https://hoing.io/archives/14457

0개의 댓글