Search
Duplicate

5-4. 격리 μˆ˜μ€€

격리 μˆ˜μ€€

β€’
SERIALIZABLE λ§κ³ λŠ” 크게 μ„±λŠ₯의 κ°œμ„ μ΄λ‚˜ μ €ν•˜ λ°œμƒ X
λΆ€μ •ν•© ν…Œμ΄λΈ”
DRITY READ
NON-REPEATABLE READ
PHANTOM READ
READ UNCOMMITTED
λ°œμƒ
λ°œμƒ
λ°œμƒ
READ COMMITTED
μ—†μŒ
λ°œμƒ
λ°œμƒ
REPEATABLE READ
μ—†μŒ
μ—†μŒ
λ°œμƒ(InnoDBλŠ” μ—†μŒ)
SERIALIZABLE
μ—†μŒ
μ—†μŒ
μ—†μŒ

READ UNCOMMITTED

β€’
dirty read: commit, rollback 여뢀에 상관없이 λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ—μ„œ λ³€κ²½ λ‚΄μš©μ΄ λ³΄μ΄λŠ” ν˜„μƒ
# session 1 start transaction; update morak.member set name='eden2' where id=1; insert into member; # session 2 SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; select * from morak.member; # commit μ•ˆν–ˆλŠ”λ° λ³€κ²½λœ λ‚΄μš©μ΄ 보인닀.
SQL
볡사

READ COMMITTED

β€’
μ–Έλ‘λ‘œκ·Έ 데이터 λ°˜ν™˜
β€’
non-repeatable read: ν•˜λ‚˜μ˜ νŠΈλžœμž­μ…˜ λ‚΄μ—μ„œ λ˜‘κ°™μ€ 쑰회 쿼리λ₯Ό μ‹€ν–‰ν–ˆμ„ λ•Œ 항상 같은 κ²°κ³Όλ₯Ό 가져와야 ν•œλ‹€λŠ” repeatable read 정합성에 μ–΄κΈ‹λ‚˜λŠ” ν˜„μƒ
# session 2 SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED; start transaction; select * from member where name='eden2'; # κ²°κ³Ό μ—†μŒ # session 1 start transaction; update morak.member set name='eden2' where id=1; commit; # session 2s elect * from member where name='eden2'; # κ²°κ³Ό 쑰회됨
SQL
볡사
β€’
μ˜ˆμ‹œ: ν•˜λ‚˜μ˜ νŠΈλžœμž­μ…˜μ—μ„œ μž…κΈˆκ³Ό 좜금 μ²˜λ¦¬κ°€ 계속 진행될 λ•Œ λ‹€λ₯Έ νŠΈλžœμž­μ…˜μ—μ„œ 였늘 μž…κΈˆλœ κΈˆμ•‘μ˜ 총합을 μ‘°νšŒν•  λ•Œ 문제

REPEATABLE READ

β€’
InnoDB μŠ€ν† λ¦¬μ§€ 엔진 κΈ°λ³Έ 격리 μˆ˜μ€€
β€’
λ°”μ΄λ„ˆλ¦¬ 둜그λ₯Ό 가진 MySQL μ„œλ²„μ—μ„œλŠ” μ΅œμ†Œ REPEATABLE READ 격리 μˆ˜μ€€ 이상 μ‚¬μš©
β€’
λ³€κ²½ν•˜κΈ° μ „ λ ˆμ½”λ“œλ₯Ό 언두 곡간에 백업해두고 λ ˆμ½”λ“œ κ°’ λ³€κ²½ -> MVCC 라고 함
β€’
언두 μ˜μ—­μ— λ°±μ—…λœ 이전 데이터λ₯Ό ν™œμš©ν•˜μ—¬ 동일 νŠΈλžœμž­μ…˜ λ‚΄μ—μ„œ 동일 κ²°κ³Ό 보여쀄 수 있게 보μž₯

언두 μ˜μ—­

β€’
사싀 READ COMMITTED 도 언두 둜그 쑰회
β€’
REPEATABLE READ 와 READ COMMITTED 의 μ°¨μ΄λŠ” 언두 μ˜μ—­μ— λ°±μ—…λœ λ ˆμ½”λ“œμ˜ μ—¬λŸ¬ 버전 쀑 λͺ‡ 번째 이전 λ²„μ „κΉŒμ§€ μ°Ύμ•„ λ“€μ–΄κ°€λŠ”μ§€
β€’
언두 μ˜μ—­μ— λ°±μ—…λœ λͺ¨λ“  λ ˆμ½”λ“œμ—λŠ” 변경을 λ°œμƒμ‹œν‚¨ νŠΈλžœμž­μ…˜ 번호 포함
β€’
언두 μ˜μ—­μ— λ°±μ—…λœ λ°μ΄ν„°λŠ” InnoDB μŠ€ν† λ¦¬μ§€ 엔진이 λΆˆν•„μš”ν•˜λ‹€κ³  νŒλ‹¨ν•˜λŠ” μ‹œμ μ— 주기적으둜 μ‚­μ œ
β€’
REPEATABLE READ 격리 μˆ˜μ€€μ—μ„œλŠ” MVCC λ₯Ό 보μž₯ν•˜κΈ° μœ„ν•΄ μ‹€ν–‰ 쀑인 νŠΈλžœμž­μ…˜ 쀑 κ°€μž₯ 였래된 νŠΈλžœμž­μ…˜ λ²ˆν˜Έλ³΄λ‹€ νŠΈλžœμž­μ…˜ λ²ˆν˜Έκ°€ μ•žμ„  언두 μ˜μ—­μ˜ 데이터 μ‚­μ œ λΆˆκ°€
β€’
begin 으둜 νŠΈλžœμž­μ…˜μ„ μ‹œμž‘ν•˜κ³  μž₯μ‹œκ°„ νŠΈλžœμž­μ…˜μ„ μ’…λ£Œν•˜μ§€ μ•ŠμœΌλ©΄ 언두 μ˜μ—­μ— 데이터가 λ¬΄μ œν•œ μŒ“μΌ 수 μžˆλ‹€.
β€’
언두 μ˜μ—­ λ ˆμ½”λ“œμ—λŠ” μž κΈˆμ„ κ±Έ 수 μ—†λ‹€
# session 2 SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; start transaction; select * from member; # 6개의 row # start transaction λ’€ μ–΄λ– ν•œ 쿼리도 μ—†μœΌλ©΄ transaction 이 λ“±λ‘λ˜μ§€ μ•ŠλŠ”λ‹€. κ·Έλž˜μ„œ νŒ¬ν…€ λ¦¬λ“œ ν…ŒμŠ€νŠΈ λΆˆκ°€λŠ₯ # session 1 start transaction; insert into member; commit; # session 2 select * from member; # 6개의 row select * from member for update; # 7개의 row # 언두 μ˜μ—­μ—λŠ” μž κΈˆμ„ λͺ» κ±Έμ–΄μ„œ μ‹€μ œ ν…Œμ΄λΈ”μ—μ„œ κ°€μ Έ μ™€μ•Όν•΄μ„œ κ·ΈλŸ°λ“―? select * from member; # 6개의 row delete from member where id=7; # Query OK, 1 row affected (0.01 sec)
SQL
볡사
session 1 에 select * from member where name='eden' for updateΒ λ₯Ό κ±Έμ—ˆλ‹€.

SERIALIZABLE

β€’
κ°€μž₯ μ—„κ²©ν•œ 격리 μˆ˜μ€€
β€’
λ™μ‹œ 처리 μ„±λŠ₯ μ΅œν•˜
β€’
읽기 μž‘μ—…λ„ 곡유 μž κΈˆμ„ νšλ“ν•΄μ•Ό ν•œλ‹€
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; start transaction; select * from morak.member where id=1; # member 의 id=1 인 λ ˆμ½”λ“œμ— S,REC_NOT_GAP 락 κ±Έλ¦Ό select * from morak.member; # λͺ¨λ“  member λ ˆμ½”λ“œμ— S 락 κ±Έλ¦Ό update member where id=1; # member 의 id=1 인 λ ˆμ½”λ“œμ— X,REC_NOT_GAP 락 κ±Έλ¦Ό delete from member where name='zz'; # λͺ¨λ“  member λ ˆμ½”λ“œμ— X 락 κ±Έλ¦Ό
SQL
볡사