-
뉴딜 일자리 사업 3일차교육/뉴딜 일자리 사업 2021. 5. 2. 21:21
1. SubQuery
SQL 기본 문법
SELECT
FROM
WHERE
GROUP BY
ORDER BY
HAVING
→ GROUP BY와 쌍으로 움직이며 그룹 함수 결과에 조건을 걸 때 사용함group by
에 사용한 column은 반드시select
에 작성해주어야 한다.- subquery는 위의 모든 상황에 사용할 수 있지만 추천은
from
이다.
- △ : 건수가 많을수록 X임
- 다만 from을 제외한 절에 작성하면 쿼리 퍼포먼스가 굉장히 낮아진다.
- select in select 절에는 데이터 건수가 많으면 사용 x
- where절에는 연산자에 따라 다르지만(ex. In) 웬만하면 사용 x
- 아무튼 건수가 적다면 사용이 가능하다.
- 만약 select in select를 사용해야 할 경우에는 Join문으로 넘기자.
SQL query 작성 순서
from
>join
>where
>select
보통 프로젝트 당 30~50줄의 SQL query를 씀.
- 겁먹지 말고 꾸준한 연습이 답이다.
2. MAX + 1
- 보통 신규 글이나 새로운 데이터에 넘버링을 편이하게 하기 위해 사용.
- 문제점: 여러 유저가 동시에 request 보내면 transaction 발생. 한 명만 가능함.
- Max+1 참고 블로그
- sequence를 사용하면 위의 문제를 피할 수 있다. (waiting 기능이 있음)
- 오라클에는 시퀀스가 존재하지만 MySQL은 존재하지 않기에 시퀀스 기능을 하는 테이블을 따로 작성해야 한다.
- Sequence 참고 블로그
3. Optimizer / Plan / Hint
Optimizer
- 사용자가 실행한 SQL을 해석하고 데이터를 추출하기 위한 실행계획(Plan)을 수립하는 프로세스
- 옵티마이저 설명 링크
Plan
- SQL 문의 액세스 경로를 확인하고 튜닝할 수 있도록 SQL문을 분석하고 해석하여 어떻게 실행할 것인가에 대한 계획
- DBMS가 쿼리문을 파싱 해서 어떻게 값을 가져올지에 대한 계획
Hint
- Plan이 실행 경로였다면 Hint는 실행 경로를 사용자가 직접 설정하는 것이다.
- 요즘엔 하드웨어가 좋아져서 많이 사용하고 있지는 않다.
- 만약 힌트에 오타가 있다면 그 문은 무시되고 넘어가게 된다.
- 오라클 힌트(hint)의 개념/사용법/자주 쓰이는 힌트
- Hint를 인덱스에 활용하는 방법
+ 그러나 SQL문을 몇천 줄까지 써야 한다면 Plan과 Hint를 사용해 '튜닝'하는 것을 추천한다.
4. PK, FK, UK
PK (Primary Key)
- 식별자 역할
- Not Null
- 중복 X -> Unique
- 고유 인덱스 자동 생성
- 여러 column들을 PK로 지정할 수 있고, 또한 PK 끼리 묶을 수도 있음
FK (Foreign Key)
- SQL Constraint 참고 블로그
- 만약 PK와 일반 칼럼의 FK로 연결되어 있다면 비식별 관계라고 한다.
- 표시는 점선이다.
- 둘 다 PK와 PK라면 식별 관계이다.
- 표시는 실선이다.
- 참고 블로그
- 만약 PK와 일반 칼럼의 FK로 연결되어 있다면 비식별 관계라고 한다.
UK (Unique Key)
- 예전에는 많이 써먹었는데 요즘엔 잘 쓰지 않는다.
- Unique 하지만 Null을 허용한다.
인덱스를 쓰면 검색 속도가 빨라진다.
하지만 insert, delete, update를 할 시에 매번 인덱스를 재생성하기 때문에 자주 데이터가 바뀌는 테이블은 인덱스 사용을 고려해봐야 한다.
+ 추후 프로젝트에 사용될 기술 스택
- 프로젝트 환경
JDK : 1.8
Spring : 4.1.2
DBMS : Mysql 5.5
jquery : 1.11.2- 사용할 프레임워크 등
JSTL
MyBatis3
Vue.JS3
Html5
Ajax728x90반응형LIST'교육 > 뉴딜 일자리 사업' 카테고리의 다른 글
뉴딜 일자리 사업 5일차 (0) 2021.05.02 뉴딜 일자리 사업 4일차 (0) 2021.05.02 뉴딜 일자리 사업 2일차 (0) 2021.05.02 뉴딜 일자리 사업 1일차 (0) 2021.05.02 뉴딜 일자리 사업 2차 면접 (0) 2021.04.22