DevLog ๐Ÿ˜ถ

[MySQL] ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์„ ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ง์ ‘ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ ๋ณธ๋ฌธ

โœ๏ธ/CS

[MySQL] ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์„ ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด ์ง์ ‘ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ธฐ

dolmeng2 2023. 6. 21. 17:53

๐ŸŒฑ ๋“ค์–ด๊ฐ€๊ธฐ ์ „

 ์ง€๋‚œ ๋ฆฌ๋งˆํ ํฌ์ŠคํŒ…์—์„œ ๊ทธ๋ฆผ์œผ๋กœ ์„ค๋ช…ํ–ˆ๋˜ 'CREW' ํ…Œ์ด๋ธ”์— ๋Œ€ํ•ด์„œ ์ง์ ‘ ์ฝ”๋“œ ๋ ˆ๋ฒจ๋กœ ์ž‘์„ฑํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

CREATE TABLE `CREW` (
  `id` bigint NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

 

์‚ฌ์ „ ์„ค์ •์„ ์œ„ํ•ด์„œ ์Šคํ† ๋ฆฌ์ง€ ์—”์ง„์„ innoDB๋กœ ์„ค์ •ํ•œ 'CREW' ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑํ•ด์ค€๋‹ค.

SET AUTOCOMMIT = false;

โญ๏ธ ๊ทธ๋ฆฌ๊ณ , autocommit ๋ชจ๋“œ๋Š” ๊ผญ OFF๋กœ ์„ค์ •ํ•ด๋‘๊ณ  ์ง„ํ–‰ํ•˜์ž.

๋‚˜๋Š” ์ด๋ฒˆ์— 2๊ฐœ์˜ ์„ธ์…˜์„ ์—ด์–ด์„œ ์ง„ํ–‰ํ•˜์˜€๋‹ค.

 


 

๐ŸŒฑ READ UNCOMMITTED

SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

๊ธ€๋กœ๋ฒŒ ์„ค์ •์œผ๋กœ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์„ ๋ณ€๊ฒฝํ•˜๋ฉด ์—ฌํŒŒ๊ฐ€ ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ ์„ธ์…˜์— ๋Œ€ํ•ด์„œ ์œ„์™€ ๊ฐ™์ด READ UNCOMMITTED๋ฅผ ์ง€์ •ํ•ด์ฃผ์—ˆ๋‹ค.

 

SELECT @@SESSION.transaction_isolation;

์‹ค์ œ๋กœ ์„ธ์…˜ ์ •๋ณด๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด READ-UNCOMMITTED๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

๐Ÿ’ฌ Dirty Read ํ™•์ธํ•ด๋ณด๊ธฐ - INSERT

START TRANSACTION;
INSERT INTO CREW (id, name) VALUES (1, 'hello');
commit;

๊ทธ๋ฆฌ๊ณ , ๊ทธ๋ฆผ์ด๋ž‘ ๋˜‘๊ฐ™์ด ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„  1๋ฒˆ ๋ ˆ์ฝ”๋“œ์ธ 'hello'๋ฅผ ์‚ฝ์ž…ํ•ด๋‘์ž. (์–ด๋–ค ์„ธ์…˜์ด๋“  ์ƒ๊ด€ ์—†๋‹ค)

 

# ์„ธ์…˜ 1
START TRANSACTION;
INSERT INTO CREW (id, name) VALUES (2, 'journey');

์ดํ›„, ์„ธ์…˜ 1์—์„œ Commit ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ 2๋ฒˆ ๋ ˆ์ฝ”๋“œ 'journey'๋ฅผ ์‚ฝ์ž…ํ•˜์ž.

 

# ์„ธ์…˜ 2
select * from CREW;

์„ธ์…˜ 2์—์„œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด๋ฉด, ์„ธ์…˜ 1์—์„œ ์•„์ง ์ปค๋ฐ‹์„ ํ•˜์ง€ ์•Š์•˜์Œ์—๋„ ์‚ฝ์ž…์ด ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (Dirty Read)

 

๊ทธ๋Ÿผ, ์ด ์ƒํƒœ์—์„œ ์„ธ์…˜ 1์„ ๋กค๋ฐฑ์‹œํ‚ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

# ์„ธ์…˜ 1
ROLLBACK;

๋กค๋ฐฑ ์‚ฌํ•ญ์ด ๋ฐ˜์˜๋˜์–ด ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ฆ‰, ์ด์ „์—๋Š” ๊ฐ’์ด ๋ณด์˜€๋Š”๋ฐ ๋‹ค์‹œ ์กฐํšŒํ•˜๋‹ˆ ๊ฐ’์ด ์‚ฌ๋ผ์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค.

 


 

๐Ÿ’ฌ Dirty Read ํ™•์ธํ•ด๋ณด๊ธฐ - UPDATE

โญ๏ธ ์œ„์˜ ๊ฒฝ์šฐ๋ณด๋‹ค update ์‹œ์— ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ๋“œ๋Ÿฌ๋‚˜๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ํ•œ ๊ฐ€์ง€ ๋” ํ…Œ์ŠคํŠธ๋ฅผ ํ•˜๊ณ ์ž ํ•œ๋‹ค.

# ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ์‚ฝ์ž… ํ›„ ์ปค๋ฐ‹
START TRANSACTION;
INSERT INTO CREW (id, name) VALUES (1, 'hello');
COMMIT;

# ํ•ด๋‹น ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด์„œ ์—…๋ฐ์ดํŠธ
START TRANSACTION;
UPDATE CREW SET name = 'hi' where id = 1;

 

์•„๊นŒ์ฒ˜๋Ÿผ hello๋ผ๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ์‚ฝ์ž…๋œ ๋‹ค์Œ, ์—…๋ฐ์ดํŠธ์— ๋Œ€ํ•œ ํŠธ๋žœ์žญ์…˜์„ ์‹œ์ž‘ํ•˜๊ณ  ์ปค๋ฐ‹์„ ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์ด๋‹ค.

# ์„ธ์…˜ 2
SELECT * FROM CREW;

ํ•˜์ง€๋งŒ, READ UNCOMMITTED๋กœ ์ธํ•ด์„œ ์ปค๋ฐ‹๋˜์ง€ ์•Š์€ ์‚ฌํ•ญ์— ๋Œ€ํ•ด์„œ๋„ ๋ณ€๊ฒฝ๋œ ๋ ˆ์ฝ”๋“œ ๊ฐ’์ด ์กฐํšŒ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

# ์„ธ์…˜ 1
ROLLBACK;

์ด ์ƒํƒœ์—์„œ ์„ธ์…˜ 1์— ๋Œ€ํ•ด ๋กค๋ฐฑ์„ ํ•œ๋‹ค๋ฉด hello๋ผ๋Š” ๊ฐ’์„ ๋ฐ›๊ฒŒ ๋œ๋‹ค...!

 


 

๐ŸŒฑ READ COMMITTED

 

 

SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

์ด๋ฒˆ์—๋Š” ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ๋ ˆ๋ฒจ์„ Read committed๋กœ ๋ณ€๊ฒฝํ•˜๊ณ  ์‚ดํŽด๋ณด์ž.

 


 

๐Ÿ’ฌ ํ•ด๊ฒฐ๋œ Dirty Read ํ˜„์ƒ ํ™•์ธํ•˜๊ธฐ

START TRANSACTION;
INSERT INTO CREW (id, name) VALUES (1, 'hello');
INSERT INTO CREW (id, name) VALUES (2, 'journey');
COMMIT;

๋จผ์ €, ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด 2๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•ด๋‘์ž. (์ด์ „์˜ ์‹คํ—˜ ๊ฒฐ๊ณผ๋Š” TRUNCATE๋กœ ์ œ๊ฑฐํ•˜์˜€๋‹ค.)

 

# ์„ธ์…˜ 1
START TRANSACTION;
UPDATE CREW SET name = 'hi' WHERE id = 1;

์ดํ›„, ์„ธ์…˜ 1์—์„œ 1๋ฒˆ ๋ ˆ์ฝ”๋“œ 'hello'์— ๋Œ€ํ•ด hi๋กœ ์—…๋ฐ์ดํŠธํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ๋‹จ, ์ปค๋ฐ‹ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

# ์„ธ์…˜ 2
SELECT * FROM CREW;

์ด๋•Œ ์„ธ์…˜ 2์—์„œ ์กฐํšŒํ•ด๋ณด๋ฉด ์–ธ๋‘ ์˜์—ญ์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— hi๊ฐ€ ์•„๋‹Œ hello๋กœ ๋ณด์ด๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

โญ๏ธ ์ด์ „์˜ read uncommitted ์˜ˆ์ œ์™€ ๋‹ฌ๋ผ์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. (dirty read ํ•ด๊ฒฐ)

 

์ดํ›„, ์„ธ์…˜ 1์—์„œ ์ปค๋ฐ‹์„ ํ•˜๊ณ  ๋‚˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์™„์ „ํžˆ ๋ฐ˜์˜๋œ๋‹ค.

# ์„ธ์…˜ 1
COMMIT;

# ์„ธ์…˜ 2
SELECT * FROM CREW;

 


 

๐Ÿ’ฌ NON-REPEATABLE READ ํ™•์ธํ•˜๊ธฐ

ํ•˜์ง€๋งŒ, ์—ฌ๊ธฐ์„œ๋„ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค.

์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ์„ธ์…˜ 2์˜ ์กฐํšŒ ์ฟผ๋ฆฌ 2๋ฒˆ์ด ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜์—์„œ ์ˆ˜ํ–‰๋œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ?

# ์„ธ์…˜ 1
START TRANSACTION;
UPDATE CREW SET name = 'hi' WHERE id = 1;

# ์„ธ์…˜ 2
START TRANSACTION;
SELECT * FROM CREW; # 1๋ฒˆ ๊ฒฐ๊ณผ

# ์„ธ์…˜ 1
COMMIT;

# ์„ธ์…˜ 2
SELECT * FROM CREW; # 2๋ฒˆ ๊ฒฐ๊ณผ

ํ˜„์žฌ๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ๋‚ ๋ฆฌ๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•Œ์•„๋ณด๊ธฐ ์–ด๋ ต์ง€๋งŒ, ๋งŒ์•ฝ ์Šคํ”„๋ง์—์„œ ๊ฐœ๋ฐœ์„ ์ง„ํ–‰ํ•˜๋Š” ์ƒํ™ฉ์ด๋ผ๊ณ  ํ•ด๋ณด์ž.

ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜์œผ๋กœ ๋ฌถ์—ฌ ์žˆ๊ณ , ๋™์ผํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ธ์Œ์—๋„ ์œ„์™€ ๊ฐ™์ด ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์–ด ์ฃผ์˜ํ•ด์•ผ ํ•œ๋‹ค. (non-repeatable read ๋ฐœ์ƒ)

 


 

๐ŸŒฑ REPEATABLE READ

 

SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋ฒˆ์—๋Š” ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์„ repeatable read๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

 


 

๐Ÿ’ฌ ํ•ด๊ฒฐ๋œ NON-REPEATABLE READ ํ™•์ธํ•˜๊ธฐ

START TRANSACTION;
INSERT INTO CREW (id, name) VALUES (1, 'hello');
INSERT INTO CREW (id, name) VALUES (2, 'journey');
COMMIT;

์ƒ˜ํ”Œ ๋ ˆ์ฝ”๋“œ๋„ ์‚ฝ์ž…ํ•ด๋‘”๋‹ค.

 

# ์„ธ์…˜ 2
SELECT * FROM CREW;

๋จผ์ €, ์„ธ์…˜ 2์—์„œ ์กฐํšŒ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค๋‘๋ฉด ์‚ฝ์ž…ํ•œ 2๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

# ์„ธ์…˜ 1
START TRANSACTION;
UPDATE CREW SET name = 'hi' where id = 1;

# ์„ธ์…˜ 2
SELECT * FROM CREW;

์ด ์ƒํƒœ์—์„œ ์„ธ์…˜ 1์ด 1๋ฒˆ ๋ ˆ์ฝ”๋“œ์— ๋Œ€ํ•ด ์—…๋ฐ์ดํŠธ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฌ๊ณ , ์„ธ์…˜ 2์—์„œ ์กฐํšŒํ•˜๋ฉด ๊ทธ๋Œ€๋กœ 'hello'๋ผ๋Š” ๋ ˆ์ฝ”๋“œ๊ฐ€ ๋œฌ๋‹ค.

 

# ์„ธ์…˜ 1
COMMIT;

# ์„ธ์…˜ 2
SELECT * FROM CREW;

ํ•˜์ง€๋งŒ, read committed์™€ ๋‹ค๋ฅด๊ฒŒ ์„ธ์…˜ 1์—์„œ ์ปค๋ฐ‹์„ ํ–ˆ๋”๋ผ๋„ ์„ธ์…˜ 2์˜ ๊ฒฐ๊ณผ๋Š” hi๊ฐ€ ์•„๋‹Œ hello๊ฐ€ ๊ทธ๋Œ€๋กœ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

โญ๏ธ ์ฆ‰, ์ด์ „์— ๋ฐœ์ƒํ•˜์˜€๋˜ non-repeatable read๋ฅผ ํ•ด๊ฒฐํ•œ ๊ฒƒ์ด๋‹ค.

 

# ์„ธ์…˜ 2
COMMIT;

# ๋‹ค์‹œ ์กฐํšŒ
SELECT * FROM CREW;

์ดํ›„, ์„ธ์…˜ 2๊ฐ€ ์‹œ์ž‘ํ•˜์˜€๋˜ ํŠธ๋žœ์žญ์…˜์„ ์ปค๋ฐ‹ํ•˜๊ณ  ๋‹ค์‹œ ์กฐํšŒ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค๋ณด๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ๋œ ์‚ฌํ•ญ์„ ์ฝ์–ด์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

๐Ÿ’ฌ PHANTOM READ ํ…Œ์ŠคํŠธํ•˜๊ธฐ

CREATE TABLE `ISAM_CREW` (
  `id` bigint NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM;

์šฐ์„ , innoDB์—์„œ๋Š” Phantom Read๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์ž.

 

START TRANSACTION;
INSERT INTO ISAM_CREW (id, name) VALUES (1, 'hello');
INSERT INTO ISAM_CREW (id, name) VALUES (2, 'journey');
COMMIT;

๊ทธ๋ฆฌ๊ณ , ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒ˜ํ”Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด๋‘์ž.

 

# ์„ธ์…˜ 2
START TRANSACTION;
SELECT * FROM ISAM_CREW WHERE id >= 2 FOR UPDATE;

์„ธ์…˜ 2์—์„œ SELECT FOR UPDATE ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•˜์—ฌ ๋ ˆ์ฝ”๋“œ๋ฅผ ์กฐํšŒํ•ด๋ณด์ž. ํ˜„์žฌ 'journey' ๋ ˆ์ฝ”๋“œ๋งŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

# ์„ธ์…˜ 1
START TRANSACTION;
INSERT INTO ISAM_CREW(id, name) VALUES (3, 'cat');
COMMIT;

์ดํ›„, ์„ธ์…˜ 1์—์„œ ์ƒˆ๋กœ์šด ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ณ  ์ปค๋ฐ‹์„ ํ•ด๋ณด์ž.

 

# ์„ธ์…˜ 2
SELECT * FROM ISAM_CREW WHERE id >= 2 FOR UPDATE;

์œ„์™€ ๊ฐ™์ด journey์— ์ด์–ด cat์ด๋ผ๋Š” 3๋ฒˆ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ฐ™์€ ํŠธ๋žœ์žญ์…˜ ๋‚ด์ธ๋ฐ๋„ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” 'phantom read' ํ˜„์ƒ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค.

 


 

๐Ÿ’ฌ PHANTOM READ - innoDB์—์„œ๋Š” ์ •๋ง ๋ฐœ์ƒํ•˜์ง€ ์•Š์„๊นŒ?

์•ž์„œ ์‚ฌ์šฉํ–ˆ๋˜ innoDB ์Šคํ† ๋ฆฌ์ง€ ์—”์ง„์œผ๋กœ ์ƒ์„ฑ๋œ 'CREW' ํ…Œ์ด๋ธ”์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ๊ฐ€์ ธ์˜ค์ž.

์œ„์—์„œ ์ง„ํ–‰ํ–ˆ๋˜ ์‹คํ—˜๊ณผ ๋™์ผํ•˜๊ฒŒ ์ƒ˜ํ”Œ ๋ ˆ์ฝ”๋“œ ์‚ฝ์ž… ํ›„ SELECT FOR UPDATE๋ฅผ ์ง„ํ–‰ํ•ด์ฃผ์ž.

# ์„ธ์…˜ 2
START TRANSACTION;
SELECT * FROM CREW WHERE id >= 2 FOR UPDATE;

# ์„ธ์…˜ 1
START TRANSACTION;
INSERT INTO CREW(id, name) VALUES (3, 'cat'); # ์‚ฝ์ž… ๋Œ€๊ธฐ

innoDB์—์„œ๋Š” ์„ธ์…˜ 2์˜ ํŠธ๋žœ์žญ์…˜์ด ์ปค๋ฐ‹๋˜๊ธฐ ์ „๊นŒ์ง€ โญ๏ธ ๋„ฅ์ŠคํŠธ ํ‚ค๋ฝ์ด ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฝ์ž…์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

 

# ์„ธ์…˜ 2
COMMIT;

ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•˜๊ธฐ ์ „์— ์„ธ์…˜ 2์—์„œ ์ปค๋ฐ‹์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด insert๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ง„ํ–‰๋œ๋‹ค.

 

# ์„ธ์…˜ 1 - ์ด์ „์— ์ž‘์„ฑํ•˜์˜€๋˜ insert query
START TRANSACTION;
INSERT INTO CREW(id, name) VALUES (3, 'cat');

# ์ด ํŠธ๋žœ์žญ์…˜์—์„œ ์กฐํšŒ ์ง„ํ–‰
SELECT * FROM CREW;

# ์„ธ์…˜ 3 - ์„ธ์…˜ 1์˜ ์ปค๋ฐ‹ ์ „์— ์กฐํšŒ ์ง„ํ–‰
SELECT * FROM CREW;

# ์„ธ์…˜ 1 ์ปค๋ฐ‹
COMMIT;

# ์„ธ์…˜ 3 ์žฌ์กฐํšŒ
SELECT * FROM CREW;

์—ฌ๊ธฐ์„œ insert์„ ์ง„ํ–‰ํ–ˆ๋˜ ํŠธ๋žœ์žญ์…˜์—์„œ select๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ์‚ฝ์ž…์ด ๋œ ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๋ฌผ๋ก  commit ์ด์ „์—๋Š” ๋‹ค๋ฅธ ํŠธ๋žœ์žญ์…˜์—์„œ ๊ฐ’์„ ํ™•์ธํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— commit ํ›„์— ํ™•์ธ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

 


 

๐Ÿ’ฌ PHANTOM READ - SELECT FOR UPDATE

ํ•˜์ง€๋งŒ, innoDB๋ฅผ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ select for update๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ๋ฐ์ดํ„ฐ์˜ ๋ถ€์ •ํ•ฉ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒ˜ํ”Œ ๋ ˆ์ฝ”๋“œ ์‚ฝ์ž… ํ›„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ง„ํ–‰ํ•ด์ฃผ์ž.

 

# ์„ธ์…˜ 2
START TRANSACTION;
SELECT * FROM CREW WHERE id >= 1;

# ์„ธ์…˜ 1
START TRANSACTION;
UPDATE CREW SET name = 'hi' where id = 1;
COMMIT

# ์„ธ์…˜ 2 (๊ธฐ์กด์˜ ํŠธ๋žœ์žญ์…˜์—์„œ ์ง„ํ–‰)
SELECT * FROM CREW WHERE id >= 1 FOR UPDATE;

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด ์ฒ˜์Œ์—๋Š” hello, journey๊ฐ€ ์กฐํšŒ๋˜๋‹ค๊ฐ€ insert๊ฐ€ ์ง„ํ–‰๋˜์ž undo ์˜์—ญ์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹Œ ์›๋ณธ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์„ ์ฝ์–ด hi, journey๊ฐ€ ์กฐํšŒ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

๐ŸŒฑ SERIALIZABLE

SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์œผ๋กœ serializable์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

# ์„ธ์…˜ 2
START TRANSACTION;
select * from CREW;

ํ…Œ์ด๋ธ”์— ์•„๋ฌด๊ฒƒ๋„ ์‚ฝ์ž…ํ•˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ, ์„ธ์…˜ 2๊ฐ€ ์กฐํšŒ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ธ๋‹ค. ๋‹น์—ฐํžˆ ์•„๋ฌด๊ฒƒ๋„ ์กฐํšŒ๋˜์ง€ ์•Š๋Š”๋‹ค.

# ์„ธ์…˜ 1
START TRANSACTION;
INSERT INTO CREW (id, name) VALUES (1, 'hello');

์ดํ›„, ์„ธ์…˜ 1์ด ์‚ฝ์ž… ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฌ๋ ค๊ณ  ํ•˜๋ฉด ์˜ค๋ฅธ์ชฝ๊ณผ ๊ฐ™์ด ๊ณ„์† ๋Œ€๊ธฐ ์ƒํƒœ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. (์ฟผ๋ฆฌ ์‹คํ–‰ 16s)

 

์„ธ์…˜ 2๊ฐ€ ์ปค๋ฐ‹ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ณ„์† ๋Œ€๊ธฐํ•˜๋‹ค๊ฐ€ ์œ„์™€ ๊ฐ™์ด ํƒ€์ž„์•„์›ƒ์ด ๋ฐœ์ƒํ•œ๋‹ค.

์ฆ‰, ์„ธ์…˜ 2๋Š” ๋‹จ์ˆœํžˆ ์กฐํšŒ ์ฟผ๋ฆฌ๋งŒ ๋‚ ๋ ธ๋Š”๋ฐ๋„ ์„ธ์…˜ 1์ด ์‚ฝ์ž…ํ•˜์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฝ๊ธฐ์— ๋Œ€ํ•ด์„œ๋„ ๋ฝ์ด ๊ฑธ๋ฆฐ๋‹ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 


 

์ด๋ ‡๊ฒŒ ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€์— ๋Œ€ํ•ด์„œ ์ „๋ถ€ ํ…Œ์ŠคํŠธ๋ฅผ ์™„๋ฃŒํ•˜์˜€๋‹ค.

ํ•œ ๋ฒˆ์ฏค ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ ค๋ณด๋ฉฐ ์ง์ ‘ ํ…Œ์ŠคํŠธํ•ด๋ณด๊ณ  ์‹ถ๋˜ ๋‚ด์šฉ์ด์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ์ง„ํ–‰ํ•˜๋‹ˆ๊นŒ ๋งค์šฐ ์žฌ๋ฐŒ์—ˆ๋‹ค ๐Ÿ˜Š

์ข…์ข… ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋‹ค์‹œ ๋ณต์Šตํ•ด์•ผ๊ฒ ๋‹ค.

Comments