Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions 지종권/[lecture19] mvcc (1부)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# MVCC 1

### 등장배경

lock - base 같은 경우 read, read는 허용하지만 그 외의 경우에는 허용하지 않는다.

그래서 동시에 처리할 수 있게 나온게 MVCC이다

> LOCK BASE

![img](../img/lecture19_lock-base.png)

> MVCC

![img](../img/lecture19_mvcc.png)

## MVCC

mvcc 특징

1. 데이터를 읽을 때 특정 시점 기준으로 가장 최근에 commit 된 데이터만 읽는다 !
2. 데이터 변화 이력을 관리
3. read와 write는 서로를 block 하지 않는다.

이 특정 시점은 isolation level에 따름

### isolation level

Read Uncommitted: MVCC는 committed된 데이터를 읽기 때문에 취급 x

Read Committed: read하는 시간을 기준으로 그 전에 commit된 데이터를 읽음

Repeatable Read: tx 시작 시간 기준으로 그 전에 commit된 데이터를 읽음

Serializable:

Mysql 일 때 MVCC로 동작히기 보다는 lock으로 동작한다.

PostgreSQL 일 때 SSI 기법이 적용된 MVCC로 동작한다.

> SSI (Serializable Snapshot Isolation) : 직렬 가능성 + 스냅샷 격리 장점을 모아놓은 것 찾아보세여

### 동작 ( postgre sql )

tx 1 : x -> y 40 이체

tx 2 : x에 30 입금

이라는 가정을 하고 진행

### read, read 일 때

![img](../img/lecture19_read_read.png)

tx1에서 commit 되기 전까지 write lock을 가지고 있으므로

tx2는 tx1이 commit된 이후 write를 진행

그래서 x = 80, y = 50이 결과로 나옴

따라서 lost update !

그러면 isolation level을 변경하면 ?

### read, repeat

![img](../img/lecture19_read_repeat.png)

repeat의 경우 first-update-win 특징 같은 데이터에 먼저 update 한 tx가 commit 되면 자우 tx는 rollback 되기 때문에 재시도 시 성공적으로 할 수 있다.

근데 repeat, read가 되면 ?

![img](../img/lecture19_repeat_read.png)

실패 그러면 어떻게 해야하는가?

연관된 tx는 repeat repeat으로 설정해서 해결
37 changes: 37 additions & 0 deletions 지종권/[lecture20] mvcc (2부)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# MVCC 2

## lost update 해결 ( MYSQL )

mysql 에서는 post gre 와 다르게 "같은 데이터에 먼저 update한 tx가 commit 되면 나중 tx는 rollback 된다" 라는 개념이 없다

그래서 어떻게 해결하는 가

바로 Locking Read!!

얘는 read를 하면서도 write lock을 취득할 수 있음

또 특징이 가장 최근의 commit된 데이터를 읽음

![img](../img/lecture20_lockingRead.png)

따라서 위의 사진과 같이 잘 나옴

## WRITE SKEW (repeat, repeat)

밑의 사진과 같은 가정일 때 문제가 발생할 수 있음

![img](../img/lecture20_writeSkew.png)

20, 30 이던가 30, 20 이 나와야 하는데 이상하게 나옴

그러면 어떻게 해결 ?

### MYSQL에서의 해결

얘는 그냥 locking read 쓰면 됨

### POSTGRE에서의 해결

얘도 for update를 사용할 수 있는데 이 친구는 rollback 된다

그러니 얘는 재시도 해줘야한다.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# db 설계 문제

## 1. 중복 데이터 문제

관심사가 한 테이블안에 있어서 문제가 발생!

### insertion anomalies

![img](../img/lecture21_insertion.png)

- 저장 공간 낭비
- 실수로 인한 데이터 불일치 가능성 존재
- null 값을 많이 씀
- row를 삭제하는 번거로운 작업이 있을 수 있음

### Deletion anomalies

![img](../img/lecture21_deletion.png)

- 데이터 삭제 시, 삭제와 관련없는 데이터도 삭제될 수 있음

### update anomalies

![img](../img/lecture21_update.png)

- 데이터 불일치가 발생할 수 있음

## 2. Suprious Tuples

natural join 하기 전 테이블

![img](../img/lecture21_join전테이블.png)

natural join 후 테이블

![img](../img/lecture21_join후테이블.png)

4개의 row가 나와야 하는데 6개가 나옴 가짜 튜블 발생

왜냐하면 jeju에 관한 부서이름이 2개가 있기 때문

## 3. null 값이 많아짐으로 인한 문제점

- null 값이 있는 column으로 join 하는 경우 상황에 따라 예상과 다른 결과 발생
- null 값이 column에 aggreagate function을 사용했을 때 주의 필요 ex ( count(\*) )
- 공간 낭비

## 결론

1. 의미적으로 관련있는 속성들끼리 테이블을 구성
2. 중복 데이터를 최대한 허용하지 않도록 설계
3. join 수행 시 가짜 데이터가 생기지 않도록 설계
4. 되도록 Null 값을 줄일 수 있는 방향으로 설계
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# functional dependency

functional dependency : 한 테이블에 있는 두 개의 attribute 집합 사이의 제약

### 예시

![img](../img/lecture22_functional-dependecny-예시.png)

x 의 값에 따라 y 값이 유일하게 결정 될 때

'x가 y를 함수적으로 결정한다'

'y가 x를 함수적으로 의존한다'

라고 말할 수 있고, 이러한 제약관계를 " functional dependency " 라고 부른다

근데 유니크 하지 않을 때 문제가 있을 수 있다

그래서 구축하려는 DB의 attributes가 관계적으로 어떤 의미를 지닐지에 따라 FD들이 달라진다.

### FD 특징

X -> Y 가 Y -> X는 아니다

{} -> Y : Y 값은 언제나 하나의 값을 가짐

### Trivial fucntional dependency

x -> y를 결정할 때, y가 x의 부분집할일 때, Trivial fucntional dependency 라고 함

{a, b, c} -> { c }

### Non-Trivial fucntional dependency

반대의 경우

{a, b, c} -> {b, c, d}

{a, b, c} -> {d, e} 인 경우는 non-trivial fd & completely non-trivial fd

###

proper subset : x라는 집합이 있을 때, x의 부분집합이지만 x와 동일하지 않은 집합을 의미

![img](../img/lecture22_partial-function-dependency.png)

### Full functional dependency

partial functional dependency 와 반대

![img](../img/lecture22_full-function-dependency.png)
21 changes: 21 additions & 0 deletions 지종권/[lecture23] db 정규화 (1NF, 2NF)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 정규화

## 정규화 과정

예 뭐 정처기 공부할 때 많이 외웠죠

![img](../img/lecture23_정규화.png)

도부이결다조

원자값이 아닌 도메인을 분해 > 부분 함수 종속제거 > 이행 함수 종속제거 > 결정자가 후보키가 아닌 함수종속 제거 > 함수종속이 아닌 다치종속 제거 > 후보키를 통하지 않은 조인종속 제거

보통은 3nf까지 진행 혹은 BCNF까지 진행

### 1NF

attribute의 value는 반드시 나눠질 수 없는 단일한 값이어야 한다

### 2NF

모든 non-prime attribute 모든 key에 full functionally dependent 해야한다
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 정규화

## 3NF

모든 non-prime attribute는 어떤 key에도 transitively dependent 하면 안된다.

non-prime attribute와 non-prime attribute 사이에는 fd가 있으면 안된다.

## BCNF

모든 유효한 non-travial FD x -> y 는 x가 super key여야 한다.

## denormalization

성능을 고려햐여 정규화 반대과정을 진행하는 것
Binary file added 지종권/img/lecture19_lock-base.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture19_mvcc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture19_read_read.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture19_read_repeat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture19_repeat_read.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture20_lockingRead.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture20_writeSkew.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture21_deletion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture21_insertion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture21_join전테이블.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture21_join후테이블.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture21_update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 지종권/img/lecture23_정규화.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.