MySQL InnoDB ããã³ AWS Aurora ã PingCAP TiDB ã«ãããããã¯ã®ä»çµã¿ããã©ã³ã¶ã¯ã·ã§ã³ã®åä½ãå ¨ï¼ï¼åã®ã·ãªã¼ãºã§è§£èª¬ãã¾ãï¼
æåã¯ãã¼ã¹ã¨ãã¦éè¦ãª MySQL 8.0 InnoDB åæã§ã¦ã¼ã¶ã¼è¦ç¹ã§ã®ããã¯ã®ä»çµã¿ãå¦ã³ãå¾å第ï¼ï¼å以éã§ã¯ MySQL äºæ DB ã¨ãã¦äººæ°ã®é«ã AWS Aurora ã PingCAP TiDB 㨠MySQL InnoDB ã¨ã®éãã«ã¤ãã¦å¦ã³ã¾ãã
ï¼åç®ã®ä»åã¯ããã¯æ©æ§ã¨åã£ã¦ãåãé¢ããªããã©ã³ã¶ã¯ã·ã§ã³ã¨ãã®åé¢ã¬ãã«ã«ã¤ãã¦ãå®éã«æåã確ãããªãã解説ãã¾ããã©ã¤ãæã®ãã説æãç解ã«å½¹ç«ã¡ã¾ãã®ã§ã解説åç»ãä»ãã¦ã¿ã¾ãããåããã¦ã覧ãã ããï¼
â
第ï¼å ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«
â
第ï¼å ããã¯ã¢ãã¿ã¼ã®èªã¿æ¹
â
第ï¼å ããã¯èªåãã SELECT ã¯æ¢ããããªã
â
第ï¼å INSERT ãæ¢ããã¤ã³ãã³ã·ã§ã³ããã¯
â
第ï¼å WHERE æ¡ä»¶ã¨éãããã¯èªåã
â
第ï¼å performance_schema
â
第ï¼åã®ã£ããããã¯ã¨æ¶ããããã¯
â
第ï¼å ãã¯ã¹ããã¼ããã¯ã¨éé ã¤ã³ããã¯ã¹
â
第ï¼å å
±æããã¯ã§ãããããã¯
â
第ï¼ï¼å ããã¯èªã¿åã㯠READ COMMITTED
â
第ï¼ï¼å Aurora 㨠MySQL ã®éã
â
第ï¼ï¼å TiDB 㨠MySQL ã®éã
ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã¨ã¯
é¢ä¿æ¼ç®ãåºç¤ã«ãããªã¬ã¼ã·ã§ãã«ãã¼ã¿ãã¼ã¹ (RDB) ã¸ã®ã¯ã¨ãªãç°¡åã«è¨è¿°ããããã®ã¯ã¨ãªè¨èªã¨ã㦠IBM ã® System R éçºæã«èæ¡ãããåç¨è£½åã¨ãã¦ã¯ Oracle ã§åãã¦å®è£ (History of SQL) ãããå¤ãæ´å²ãæã¤ã¯ã¨ãªè¨èªã SQL ã§ããå½æãã¸ãã¹ã¢ããªã±ã¼ã·ã§ã³åãã®ããã°ã©ãã³ã°è¨èªã¨ãã¦äººæ°ã ã£ã COBOL ãæèãã¦ãã¨ã³ã¸ãã¢ä»¥å¤ã®äººã«ãçæéã®ãã¬ã¼ãã³ã°ã§ä½¿ããããã«è±åèªã®ãã¼ã¯ã¼ããå¤ç¨ãã使ãããããéè¦ããã¯ã¨ãªè¨èªã¨ãã¦è¨è¨ããã¾ããã
å®ç¨æ§ã¨å®¹æããéè¦ãã SQL ã§ãããå¤ãã®ãã¼ã¿å¦çãå¼ãåããã¨ãããã¼ã¿ãã¼ã¹ã®ç¹æ§ããåæ並è¡ã§ã®ãã©ã³ã¶ã¯ã·ã§ã³å¦çãå¿ è¦ã«ãªãã¾ãããæ´æ°ãä¼´ã並è¡å¦çãã©ã調åãããã¨ããã®ã¯ã¨ã¦ãè¤éãªåé¡ã§ããè¤éãªä¸¦è¡ãã©ã³ã¶ã¯ã·ã§ã³å¦çã®æåã製åæ¯ã«éãã¨å©ç¨è ã«ã¨ã£ã¦ã¯å¦ç¿ã³ã¹ããé«ããªãä¸ä¾¿ã§ãããããç±³å½å½å®¶è¦æ ¼åä¼ (ANSI) ã«ãã£ã¦ä¸¦è¡å¦çã®æåãã¿ã¼ã³ã¨ãã¦æ¨æºåããã®ã ANSI ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã§ãããã¨ãã¨ã®ç®çã¯ãã©ã並è¡å¦çãããããã®å®ç¾©ã§ãããæ¨æºåãé£ãããæçµçã«ã¯ãçºçãã¦æ¬²ãããªãç¾è±¡ãï¼ã¤å®ç¾©ãããã®ãã¡ã®ã©ããèµ·ãã¦ãã¾ãã®ããã¨ããæ¹æ³ã«ãã£ã¦ä»¥ä¸ã®ï¼ã¤ã®äºè±¡ã¨ï¼ã¤ã®ã¬ãã«ã«åé¡ããã¾ããã
ãã¼ãã£ãªã¼ã (Dirty Read) : ãããã©ã³ã¶ã¯ã·ã§ã³ä¸ã§ãä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ãã¾ã ã³ããããã¦ããªãæ´æ°ãèªãã¦ãã¾ããã¨
ãã³ãªãã¼ã¿ãã«ãªã¼ã (Non Repeatable Read) : ãããã©ã³ã¶ã¯ã·ã§ã³ä¸ã«ããããã¼ã¿ãè¤æ°åèªã¾ããããä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ããã®æ´æ°ã§ãã®å¤ãä¸è²«ããªããªãç¾è±¡ã§ãããã¡ã¸ã¼ãªã¼ãã¨ãå¼ã°ãã¾ãã
ãã¡ã³ãã ãªã¼ã (Phantom Read) : ãããã©ã³ã¶ã¯ã·ã§ã³ä¸ã«ããããã¼ã¿ã®ç¯å²ãè¤æ°åèªã¾ããããä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ããã®è¿½å ãåé¤ã§èªãã ãã¼ã¿ã®åæ°èªä½ãä¸è²«ãã¦ããªãç¾è±¡ã§ãã
ãã¡ã³ãã ã¯ãéä¸ã§ä»ããå¤æ´ããããã¨ããæå³ã§ã¯ãã³ãªãã¼ã¿ãã«ã¨ä¼¼ã¦ãã¾ãããåæ°ã®å¢æ¸ã«ç¹åããç¾è±¡ã¨èããã¨ç解ããããã§ãã
ï¼ã¤ã®åé¢ã¬ãã«
åé¢ã¬ãã« | ãã¼ãã£ã¼ãªã¼ã | ãã³ãªãã¼ã¿ãã«ãªã¼ã | ãã¡ã³ãã ãªã¼ã |
READ UNCOMMITTED | çºç | çºç | çºç |
READ COMMITTED | ãªã | çºç | çºç |
REPEATABLE READ | ãªã | ãªã | çºç |
SERIALIZABLE | ãªã | ãªã | ãªã |
InnoDB ã® REPEATABLE READ ã¯ç¹æ®ã§ãä»å¾è§£èª¬ããã®ã£ããããã¯ããã¯ã¹ããã¼ããã¯ã®ãããã§ãã³ãªãã¼ã¿ã¼ãã«ãªã¼ãã®ä»ã«ãã¡ã³ãã ãé²ããã¨ãåºæ¥ã¦ãã¦ããã®è¡¨ã§ããã¨ããã® SERIALIZABLE ã¨åçã®æåã«ãªã£ã¦ãã¾ããã MySQL ä¸ã®è¨å®ã¨ãã¦ã¯ REPEATABLE READ ã¨ããã¦ãã¾ãã
READ UNCOMMITTED ã¯åé¢ãå¼±ãããSERIALIZABLE ã¯å¼·ãããåé¢ã¬ãã«ã¨ãããã¨ã§ããããããã¾ã使ããã¦ãã¾ããããREAD COMMITTED 㯠ãæ¸ãè¾¼ã¿ãåç¬ãªã®ã¯ããããªããããªã¼ãã¯ä¸¦è¡ãã¦å¤æ°ã®å¦çãè¡ããããã¨ããä¸è¬çãªãã¼ãºã¨åè´ãããããå¤ãã®ãã¼ã¿ãã¼ã¹ã§ããã©ã«ãã®åé¢ã¬ãã«ã¨ãªã£ã¦ãã¾ããå ã㦠REPEATABLE READ 㯠MySQL InnoDB ã§ã®ããã©ã«ãåé¢ã¬ãã«ã§ããããããã®ï¼ã¤ã®åé¢ã¬ãã«ã®æåãç解ãã¦ãããã¨ã¯éè¦ã§ãã
READ COMMITTED ã§ãã³ãªãã¼ã¿ãã«ãªã¼ãã観測ãã¦ã¿ã
ä»å使ã t1 ãã¼ãã«ã§ããk ããã©ã¤ããªãã¼ã§ 3 è¡æ ¼ç´ããã¦ãã¾ãã
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
æåã«ã°ãã¼ãã«æå®ã§ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã READ COMMITTED ã«å¤æ´ãã¾ã
SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
æ¢åã»ãã·ã§ã³ã«ã¯é©ç¨ãããªãããä¸æ¦ mysql ã¯ã©ã¤ã¢ã³ããçµäºããåæ¥ç¶ãã¦ããã確èªãã¾ã
mysql> SELECT @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| READ-COMMITTED |
+-------------------------+
ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãã¦ãk = 10 ã®è¡ã§ v = v +1 ãã¾ãã
mysql> START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
3 rows in set (0.00 sec)
mysql> UPDATE t1 SET v = v + 1 WHERE k = 10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
ãã®ç¶æ ã§éããã¾ã¾ã«ãå¥ã®ç«¯æ«ã§ k = 100 ã®è¡ã§ãªã¼ãã³ãããã®ï¼è¡ update ãå®è¡ãã¾ã
mysql> UPDATE t1 SET v = 100 WHERE k = 100;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
k = 10 ã§å¤æ´ããã®ã¿ã®æåã®ç«¯æ«ã«æ»ã£ã¦ãå ¨è¡ select ããã¨
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 1 |
| 100 | 100 |
+-----+------+
k = 100 㧠v = 100 ã«ããæ´æ°ãå ¥ã£ã¦ãã¦ãã¾ãããã®ç¾è±¡ããã³ãªãã¼ã¿ãã«ãªã¼ãã§ããã¾ã ãã©ã³ã¶ã¯ã·ã§ã³å®è¡ä¸ã§ãããå¤ãåç §ãã度ã«ä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ã§çµäºããæ°ããå¤ã«å¤åãã¾ãã
ããã«ï¼ã¤ç®ã®ç«¯æ«ã§ k = 100 ã®è¡ãåé¤ãã¦ã¿ã¾ãã
mysql> DELETE FROM t1 WHERE k = 100;
ã¾ã ãã©ã³ã¶ã¯ã·ã§ã³å®è¡ä¸ã®æåã®ç«¯æ«ã§ååº¦å ¨è¡èªãã¨
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 1 |
+-----+------+
ãã®ããã«ããã©ã³ã¶ã¯ã·ã§ã³ä¸ã§ãä»ã® INSERT ã DELETE ã«ãã£ã¦è¡ã®æ°èªä½ãå¤åããã®ããã¡ã³ãã ãªã¼ãã§ããã³ãªãã¼ã¿ãã«ãªã¼ãã¨ãã¡ã³ãã 両æ¹ãçºçããã®ã READ COMMITTED ã®ç¹å¾´ã«ãªãã¾ãã
MySQL ã® REPEATABLE READ ã§ãã¡ã³ãã ãçºçããªãã®ã観測ãã
åè¿°ã¨åãæ¹æ³ã§ãä»åº¦ã¯åé¢ã¬ãã«ã REPEATABLE READ ã«å¤æ´ãã¦å®é¨ãã¦ã¿ã¾ãããã
ï¼æç®ã®ç«¯æ«ã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãå ¨è¡ãèªãã¨ä»¥ä¸ï¼è¡ãããã¾ãã
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
+-----+------+
k = 10 ã®è¡ã§ v = v + 1 ãã¾ã
mysql> UPDATE t1 SET v = v + 1 WHERE k = 10;
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 1 |
+-----+------+
ãã®ã¾ã¾ï¼æç®ã®ç«¯æ«ã§ k = 1 ã®è¡ã§ v = v + 1 ãã¾ã
mysql> UPDATE t1 SET v = v + 1 WHERE k = 1;
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 0 |
+-----+------+
v ã¯ä¸ãã 1, 0 ã§ãªããã§ãã¾ããï¼æç®ã®ç«¯æ«ã«æ»ã£ã¦ååº¦å ¨è¡ select ããã¨
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 1 |
+-----+------+
㨠v 㯠0, 1 ã®ã¾ã¾ã§ãããä»ã®ãã©ã³ã¶ã¯ã·ã§ã³ã®å¤ã§ããæ´æ°ãããªããREPEATABLE READ ã®æåã確èªã§ãã¾ããããã§ããã«ï¼æç®ã®ç«¯æ«ã§ k = 100 ã®è¡ã«æ¿å ¥ããã¨
mysql> INSERT INTO t1 VALUES (100, 0);
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
㧠v 㯠1,0,0 ã«ãªããæåã®ç«¯æ«ã§ã¯
mysql> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 1 |
+-----+------+
ã®ã¾ã¾ã§ããã追å ãããè¡ãå³æã«è¦³æ¸¬ãããã¨ã¯ããã¾ããã
ãã®ããã« MySQL ã® REPEATABLE READ ã«ããã¦ã¯ãã¡ã³ãã ãçºçããªãã®ãç¹å¾´ã«ãªãã¾ãã
ã¾ã¨ã
ä»åã¯ããã¯å ¥éã®ç¬¬ï¼åã¨ãããã¨ã§ããã©ã³ã¶ã¯ã·ã§ã³ã®åºæ¬ã¨ãªãåé¢ã¬ãã«ã¨ãREAD COMMITTED 㨠REPEATABLE READ ã®æåã®éããMySQL ã«ããããã¡ã³ãã ãªã¼ããçºçããªã REPEATABLE READ ã«ã¤ãã¦è§£èª¬ãã¾ããã
次å第ï¼åã¯æ¬ã·ãªã¼ãºã®éè¦ãªãããã¯ã®ä¸ã¤ã¨ãããã InnoDB ã®ããã¯ã¢ãã¿ã¼ã®èªã¿æ¹ã使ãæ¹ã«ã¤ãã¦è§£èª¬ãããã¾ãããæå¾ ãã ããï¼