DataBase
-
불친절 SQL 2장 정리 (인덱스 기본)DataBase/튜닝 2024. 8. 19. 21:21
1. 인덱스 튜닝의 핵심 요소 - 스캔 과정 비효율 줄이기 -> 인덱스 구성 및 순서에 영향 - 랜덤 엑세스 최소화 튜닝 -> 인덱스 구성에 영향 2. 인덱스 탐색 과정 -> 수직적 탐색을 통해 리프 블록을 찾는다 (루트에서 브랜치를 통해 리프까지) -> 이때 키에는 하위 브랜치의 범위가 담긴다. -> 비교해야하는 값과 키를 탐색하며, 자신의 값보다 같거나 큰 값을 만나면 그 옆의 브랜치로 이동한다. -> 수평적 탐색을 통해 값을 찾는다. (리프 사이 사이를 다니며 탐색) -> 리프 블록의 값을 비교하며 필요한 값을 찾는다. -> 인덱스의 값으로 충분하지 않으면, ROWID를 사용해서 테이블에 접근해서 필요..
-
불친절 SQL 1장 정리DataBase/튜닝 2024. 8. 19. 19:55
1. SQL이 느린 이유 -> 디스크 I/O 때문임! (I/O 처리하는 과정에서 프로세스가 잠을 자기 때문) 여러 프로세스가 하나의 CPU를 공유할 수 있지만, 특정 순간에는 하나의 프로세스만 CPU를 사용할 수 있다. 디스크에서 데이터를 읽어야 할 때 CPU를 OS에 반환하고 수면 상태에서 I/O 완료를 기다림 2. 데이터베이스 저장 구조(오라클) -> 테이블 스페이스 : 여러 세그먼트를 담음 세그먼트는 여러 카테고리가 있음 (테이블, 인덱스, 파티션 등) *세그먼트, 익스텐트,블록은 하나의 테이블에 대한 정보임 -> 세그먼트 -> 세그먼트 : 여러 익스텐트를 담는 저장 공간 -> 익스텐트 ..
-
조인 튜닝 - NL 조인DataBase/튜닝 2024. 8. 7. 18:14
1. NL 조인의 기본 메커니즘 NL조인은 Nested Loop 방식을 사용한다. 중첩 루프문으로 수행 된다는 뜻이다.NL 조인은 중첩 루프문과 같은 수행 구조를 사용한다. 일반적으로 NL 조인은 Outer와 Inner 양쪽 테이블 모두의 인덱스를 사용한다. Outer쪽 테이블 사이즈가 크지 않으면 인덱스를 이용하지 않을 수 있다. Table Full Scan 하더라도 한 번에 그치기 때문이다. 반면 Inner쪽 테이블은 인덱스를 사용해야 한다. 사원 X1 인덱스에서 입사일자 >= '19960101'인 첫번째 레코드를 찾는다.인덱스에서 읽은 ROWID로 사원 테이블 레코드를 찾아간다.사원 테이블에서 읽은 사원번호 0006으로 고객 X1 인덱스를 탐색한다.고객_X1 인덱스에서 읽은 ROWID로 고객 ..
-
인덱스 기본(1) - 인덱스 구조 및 탐색DataBase/튜닝 2024. 7. 17. 19:56
초등학교에 방문해 홍길동을 찾는 두 가지 방법 1. 1~6학년 모든 교실에서 홍길동 찾기 -> 홍길동이 많을 때 빠름 (테이블 풀)2. 교무실에서 명부 조회해서 홍길동이 있는 교실만 찾기 -> 홍길동이 별로 없으면 빠름 (인덱스) 이름으로 학생을 찾는 경우가 많다면, 학생명부를 아예 이름순으로 정렬해두면 편하다 이것이 인덱스다.이름에 딸린 학년-반-번호 컬럼이 인덱스 ROWID에 해당한다. 수십 년에 걸쳐 DBMS가 발전해 왔지만, 조회 방법은 위 두가지 방법에서 크게 벗어나지 못하고 있다.따라서 SQL 튜닝을 위해 인덱스를 공부하는 것은 매우 중요하다. 1. 인덱스 튜닝의 두 가지 핵심요소 인덱스는 큰 테이블에서 소량 데이터를 검색할 때 사용한다. 인덱스 튜닝의 핵심요소는 크게 두 가지이다...
-
SQL BOOSTER - 분석함수DataBase/SQL 2024. 7. 10. 19:57
1. 분석함수란?테이블의 데이터를 특정 컬럼을 기준으로 행들을 그룹화하여 결과를 조회하는 함수GROUP BY와 함께 사용하는 집계함수와 비슷해 보이지만, 실제로는 조금 다른 기능을 수행한다. 2. OVER절 분석함수에서 분석 대상을 지정하는 역할을 수행한다. 기본 개념은 다음과 같다.분석함수의 분석 대상을 정하는 역할대부분의 분석함수는 OVER절과 함께 사용된다.()안에 아무런 옵션이 없다면, 조회된 결과 전체가 분석 대상이다.2.1 OVER절의 기본 사용법 위 SQL의 결과는 3월 1일의 주문 로우 데이터를 보여주면서, 마지막 컬럼에 주문 총 건수를 추가했다.3월 1일의 총 주문건수는 5개이므로, 모두 5가 나타난다.SELECT T1.ORD_SEQ, T1.CUS_ID, T1.ORD_DT, COUNT(..
-
SQL BOOSTER - HASH JOIN과 성능DataBase/SQL 2024. 6. 24. 14:37
1. 대량의 데이터 처리 NL 조인은 많은 양의 데이터를 조인 처리하기에는 적합하지 않다. 그러므로 많은 양의 데이터를 조인하려면 머지 조인을 사용해야한다. 하지만, 머지 조인도 데이터를 정렬해야 하는 부담이 있다.이와 같은 단점을 해결할 수 있는 것이 해시 조인이다. 해시 조인은 해시 함수를 사용하기 때문에 CPU와 메모리에 추가적인 부하가 발생한다. 하지만 머지 조인에 비하면 월등한 성능을 가지고 있다. 대용량 데이터 조인은 해시 조인이 필수다. SELECT /*+ GATHER_PLAN_STATISTICS LEADING(T1) USE_MERGE(T2) */T1.CUS_ID, MAX(T1.CUS_NM) CUS_NM, MAX(T1.CUS_GD) CUS_GD, COUNT(*) ORD_CNT, SUM(T2..
-
SQL BOOSTER - MERGE 조인과 성능DataBase/SQL 2024. 6. 24. 11:18
1. 대량의 데이터 처리 머지 조인은 대량의 데이터를 조인할 때 적합하다. NL 조인과의 비교를 통해 머지 조인이 과연 대량 데이터 조인에 유리한지 살펴보자 여기서는 머지 조인의 성능 테스트를 위해 T_ORD_BIG 테이블을 사용한다.M_CUS와 T_ORD_BIG테이블을 NL조인하는 SQL이다. SELECT /*+ GATHER_PLAN_STATISTICS LEADING(T2) USE_NL(T1) FULL(T2) */ T1.CUS_ID, MAX(T1.CUS_NM) CUS_NM, MAX(T1.CUS_GD) CUS_GD, COUNT(*) ORD_CNT ,SUM(T2.ORD_AMT) ORD_AMT ,SUM(SUM(T2.ORD_AMT)) OVER() TTL_ORD_AMTFRO..
-
SQL BOOSTER - MERGEDataBase/SQL 2024. 6. 23. 16:10
1. MERGE란? 데이터 존재 여부에 따라 데이터를 INSERT하거나 UPDATE하는 경우가 많다. 이때 유용하게 사용할 수 있는 것이 MERGE이다. MERGE는 한 문장으로 INSERT와 UPDATE를 동시에 처리할 수 있다. 한 건의 데이터가 동시에 INSERT와 UPDATE되는 것은 아니다. 한 건의 데이터는 INSERT와 UPDATE 중 하나만 수행된다.MERGE 대상이 이미 존재하면 UPDATE, 대상이 존재하지 않으면 INSERT를 수행하는 방식이다. 예를 들어 고객 ID와 고객 정보를 외부에서 입력 변수로 받는다. 이때 같은 고객 ID가 이미 있으면, 고객 정보를 업데이트하고, 같은고객 ID가 없으면 신규 고객으로 등록한다. MERGE INTO M_CUS_CUD T1USING( S..