Nested Loop 조인

CJB_ny·2022년 4월 29일
1

DataBase

목록 보기
28/29
post-thumbnail

JOIN 문에 대해서 깊게 알아 볼 것이다.

조인이 내부적으로 처리되는 방식이 여러가지가있다.

이것을 이해를 해야지 실행할때 분석이 가능하다.

조인 원리

1) Nested Loop (NL)조인

2) Merge(병합) 조인

3) Hash(해시) 조인

이렇게 세가지 상태중 하나를 골라서 조인이 일어나게 된다.



이경우 Merge 조인 사용하고

이 경우 처음으로 NL 중첩 루프 사용함.

이런식인데

MERGE, NL, HAHS에 대한 지식이 없으면 이것을 봐도 뭔지 모른다.

일단 NL부터 오늘 보도록 하자

이것을 옵션으로 강제할 수도있다.

그리고 이것을 직관적으로 이해하려면 C#으로 보면됨.

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Player
    {
        public int playerID;
    }
    class Salary
    {
        public int playerID;
    }

    class Program
    {
        static void main(string[] args)
        {
            Random rand = new Random();

            List<Player> players = new List<Player>();

            for (int i = 0; i < 1000; i++)
            {
                if (rand.Next(0, 2) == 0)
                    continue;

                players.Add(new Player() { playerID = i }) ;
            }

            List<Salary> salaries = new List<Salary>();

            for (int i = 0; i < 1000; i++)
            {
                if (rand.Next(0, 2) == 0)
                    continue;

                salaries.Add(new Salary() { playerID = i });
            }
        }
    }
}

이렇게 있을 때

공통적으로 ID가 Player에도 있고 Salary에도 있는거 찾으라고 하면은

같은 playerID를 추출 하라고 하면 여러가지 방법이 있을 텐데

보통은 이중 for을 사용할 것이다.

 List<int> result = new List<int>();
 foreach (Player p in players)
 {
   foreach(Salary s in salaries)
   {
   	if (s.playerID == p.playerID)
   	{
   		result.Add(p.playerID);
   		break;
   	}
   }
 }

뭐 이렇게 찾으면 될 것이다.

이렇게 찾는것이 Nested Loop의 의미라고 보면된다.

하나씩 돌면서 두번쨰 집합에서 뭔가를 찾는것.

using System;
using System.Collections.Generic;

namespace ConsoleApp1
{
    class Player
    {
        public int playerID;
    }
    class Salary
    {
        public int playerID;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();

            List<Player> players = new List<Player>();

            for (int i = 0; i < 1000; i++)
            {
                if (rand.Next(0, 2) == 0)
                    continue;

                players.Add(new Player() { playerID = i }) ;
            }

            // List<Salary> salaries = new List<Salary>();
            Dictionary<int, Salary> salaries = new Dictionary<int, Salary>();
            for (int i = 0; i < 1000; i++)
            {
                if (rand.Next(0, 2) == 0)
                    continue;

                salaries.Add(i, new Salary() { playerID = i });
            }

            List<int> result = new List<int>();
            foreach (Player p in players)
            {
                //foreach(Salary s in salaries)
                //{
                //    if (s.playerID == p.playerID)
                //    {
                //        result.Add(p.playerID);
                //        break;
                //    }
                //}

                Salary s = null;
                if (salaries.TryGetValue(p.playerID, out s))
                {
                    Console.WriteLine(s.playerID);
                    result.Add(p.playerID);
                }
            }

        }
    }
}

이렇게 외부는 중요하지 않고 내부에서 딕셔너리와 같은거로 얼마나 빠르게 찾는지가 핵심이 될 것이다 == NL의 핵심

오늘의 결론

NL의 특징

먼저 엑세스한 (OUTER)테이블에 로우를 차례차례 -> (INNER)테이블에 랜덤 엑세스
(INNER)테이블에 인덱스가 없으면 노답
부분 범위 처리에 좋다.(ex TOP 5)

profile
공부 일기장으로 변해버린 블로그 (https://cjbworld.tistory.com/ <- 이사중)

0개의 댓글