MySQL ããã³ MySQL äºæ DB ã®ããã¯ã«é¢ãã¦è§£èª¬ããã·ãªã¼ãºã第ï¼ï¼åã®ä»åã¯ãããã AWS Aurora 㨠MySQL ã®éãã«ã¤ãã¦è§£èª¬ãã¾ãã
â
第ï¼å ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«
â
第ï¼å ããã¯ã¢ãã¿ã¼ã®èªã¿æ¹
â
第ï¼å ããã¯èªåãã SELECT ã¯æ¢ããããªã
â
第ï¼å INSERT ãæ¢ããã¤ã³ãã³ã·ã§ã³ããã¯
â
第ï¼å WHERE æ¡ä»¶ã¨éãããã¯èªåã
â
第ï¼å performance_schema
â
第ï¼åã®ã£ããããã¯ã¨æ¶ããããã¯
â
第ï¼å ãã¯ã¹ããã¼ããã¯ã¨éé ã¤ã³ããã¯ã¹
â
第ï¼å å
±æããã¯ã§ãããããã¯
â
第ï¼ï¼å ããã¯èªã¿åã㯠READ COMMITTED
â
第ï¼ï¼å Aurora 㨠MySQL ã®éã
â
第ï¼ï¼å TiDB 㨠MySQL ã®éã
åæã¨ãªã Aurora ã®æ§æ
Aurora ã¯å¤æ§ãªæ§æãè¨å®ãããã¾ããä»å㯠ãAurora ServerlessãããAurora Global Databaseããããªã¼ãã¬ããªã«ã®ãã¼ã«ã«æ¸ãè¾¼ã¿è»¢éããªã©ã¯ä½¿ãããªã¼ã½ããã¯ã¹ãªã©ã¤ã¿ã¼ (Writer) ã¤ã³ã¹ã¿ã³ã¹ã¨ãªã¼ãã¬ããªã«ã¨ãå¼ã°ãããªã¼ãã¼ (Reader) ã¤ã³ã¹ã¿ã³ã¹ããããï¼å°ã¥ã¤ã®ã¯ã©ã¹ã¿ãæ³å®ãã¾ããã
+--------+ replication +--------+
| Writer +------------------->| Reader |
+--------+ +--------+
- transaction - select
- insert
- update
- select (locking read)
^ ^
| |
+--------------+-------------+
|
+------+------+
| Proxy/App |
+-------------+
ãã©ã¡ã¼ã¿ã¼ã°ã«ã¼ãã¯ããã©ã«ãããã¼ã¹ã«ãperformance_schema ã®ã¿æå¹ã«å¤æ´ãã¦ããã¾ãã
ã©ã¤ã¿ã¼ï¼ãªã¼ãã¼ã§ã® READ COMMITTED è¨å®
ä»åã¯ãã©ã³ã¶ã¯ã·ã§ã³ããã®ããã¯ã®æåã«ããã MySQL ã¨ã®éããæ確åãããããã¾ã㯠READ COMMITTED ã§ã©ã®ãããªéããããã確èªãã¾ãã
Aurora ã¯ããã©ã«ãã® REPEATABLE READ ã®ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã§ä½¿ãããããã·ãããã¯ã¢ããªã±ã¼ã·ã§ã³ã«ã¦ãã©ã³ã¶ã¯ã·ã§ã³ãä¼´ãæ´æ°ã¯ã©ã¤ã¿ã¼ã¸ãSELECT ã ãã®ã¯ã¨ãªããªã¼ãã¼ã¸ã¨ Read Write ã¹ããªãã ãã¦ã¢ã¯ã»ã¹å ã使ãããã®ãé常ã®ä½¿ãæ¹ã§ãããä»åã¯æ¤è¨¼ã®ãããªã¼ãã¼ã§ããã©ã³ã¶ã¯ã·ã§ã³ã使ã£ã¦ãã®æåã確èªãããã¨ã«ãã¾ãã
Aurora ã RDS ã§ã¯ admin ã¨ããæºç¹æ¨©ãªã¦ã¼ã¶ã¼ãä»ä¸ããã¾ãããroot ã¯ä»ä¸ããã¾ããããã® admin ã§è¨å®å¯è½ãªç¯å²ã MySQL ã¨ã¯ç°ãªããã°ãã¼ãã«ãªãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã¯è¨å®åºæ¥ã¾ããã
ä»å㯠WRITER1, READER ã®2端æ«ãç¨æããããããã® MySQL ã¯ã©ã¤ã¢ã³ãã§ã»ãã·ã§ã³ã¬ãã«ã®ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã§ READ COMMITTED ãè¨å®ãã¾ãã
[ WRITER1 ]> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
Aurora ã®ãªã¼ãã¬ããªã«ã§ã¯ transaction_isolation ã®è¨å®ã¯ç¡è¦ãã REPEATABLE READ ã§åä½ãã¾ãããaurora_read_replica_read_committed ãè¨å®ãããã¨ã§ãªã¼ãã¼ã§ READ COMMITTED åä½ãããããã¨ãå¯è½ã§ãã
[ READER ]> SET SESSION aurora_read_replica_read_committed = ON;
[ READER ]> SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
使ããã¼ãã«ã¯ k = 1,10,100 ã®ï¼è¡ãæ ¼ç´ããã¦ãã t1 ãã¼ãã«ã§ãã
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
ãªã¼ãã¼ã§ã®ãã©ã³ã¶ã¯ã·ã§ã³ã®å¶ç´
ãªã¼ãã¼ã§æ´æ°ããã¦ã¿ã¾ã
[ READER ]> UPDATE t1 SET v = 2;
ERROR 1792 (25006): Cannot execute statement in a READ ONLY transaction.
ãªã¼ãã¼ã§ã¯ Read Only ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¨ãªããUPDATE ãªã©ãå®è¡ãããã¨ã¯åºæ¥ã¾ããããã©ã³ã¶ã¯ã·ã§ã³ãéå§ãã¦æä»ããã¯èªåããã¦ã¿ã¾ãã
[ READER ]> START TRANSACTION;
Query OK, 0 rows affected (0.08 sec)
[ READER ]> SELECT * FROM t1 FOR UPDATE;
ERROR 1792 (25006): Cannot execute statement in a READ ONLY transaction.
ãã©ã³ã¶ã¯ã·ã§ã³ã®ä¸ã§ã® FOR UPDATE ã«ããæä»ããã¯èªåããå®è¡ã§ãã¾ããã§ãããLOCK IN SHARE MODE ã®å ±æããã¯ã¯å®è¡åºæ¥ã¾ãã
[ READER ]> SELECT * FROM t1 LOCK IN SHARE MODE;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
ã©ã¤ã¿ã¼ï¼ãªã¼ãã¼ã§ã® READ COMMITED åä½
ãªã¼ãã¼ã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãå ¨è¡ SELECT ãã¾ãã
[ READER ]> START TRANSACTION;
Query OK, 0 rows affected (0.09 sec)
[ READER ]> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
3 rows in set (0.10 sec)
ã©ã¤ã¿ã¼ã§ v = 1 ã« UPDATE ãã¾ãã
[ WRITER1 ]> UPDATE t1 SET v = 1;
Query OK, 3 rows affected (0.08 sec)
Rows matched: 3 Changed: 3 Warnings: 0
ãªã¼ãã¼ã§å度 SELECT ãã¾ããããã§ãã¡ã¸ã¼ãªã¼ãã«ãªãã確èªåºæ¥ã¾ãã
[ READER ]> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 1 |
| 100 | 1 |
+-----+------+
3 rows in set (0.07 sec)
ãã¡ã¸ãªã¼ããåç¾ãã¾ããã次ã«ã©ã¤ã¿ã¼ã§ INSERT ãã¾ãã
[ WRITER1 ]> INSERT INTO t1 VALUES (200,0);
Query OK, 1 row affected (0.21 sec)
ãªã¼ãã¼ã§ SELECT ããã¨è¡ãåºç¾ãã¦ãã¾ãã®ã§ããã¡ã³ãã ãªã¼ãã確èªåºæ¥ã¾ããã
[ READER ]> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 1 |
| 100 | 1 |
| 200 | 0 |
+-----+------+
ãã®ããã« Aurora ã§ãç¹æ®ãªè¨å®ãæ¡ç¨ãããã¨ã§åºæ¬ç㪠READ COMMITED åä½ã確èªåºæ¥ã¾ããããAurora ã® READ COMMITTED ã¯å¤§éãã¼ã¿ãå¦çãå³å¯æ§ãè¦æ±ããã¦ããªãå ´åãæ³å®ããæ§è½éè¦ã®è¨å®ã§ãåãã¯ã¨ãªã§ãã©ã¤ã¿ã¼ãæ®éã® MySQL ã® READ COMMITED ã¨ã¯éã£ãçµæãè¿ãå ´åãããã¾ãã
詳ãã㯠AWS ã® Aurora ã¬ããªã«ã§ã® READ COMMITTED ã®åä½ã®éã ã§è©³ç´°ã解説ããã¦ãã¾ãã®ã§æ¬çªç°å¢ã§æ¡ç¨ããéã«ã¯äºåã«ä¸èªãã¦ãã ããã
REPEATABLE READ ã§ã®ããã¯èªåã
次ã«ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«ã®è¨å®ãããã©ã«ãã® REPEATABLE READ ã«æ»ãã¦ãªã¼ãã¼ã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ãå ¨è¡ SELECT ãã¾ãã
[ READER ]> START TRANSACTION;
Query OK, 0 rows affected (0.12 sec)
[ READER ]> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 1 |
| 100 | 1 |
| 200 | 0 |
+-----+------+
4 rows in set (0.57 sec)
ã©ã¤ã¿ã¼ã§ k = 200 ã®è¡ã v = 200 ã«ãã¾ãã
[ WRITER1 ]> UPDATE t1 SET v = 200 WHERE k = 200;
Query OK, 1 row affected (0.12 sec)
Rows matched: 1 Changed: 1 Warnings: 0
ãªã¼ãã¼ã§ååº¦å ¨è¡èªãã§ã¿ã¾ãã
[ READER ]> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 1 |
| 100 | 1 |
| 200 | 0 |
+-----+------+
4 rows in set (0.10 sec)
200 ã«å¤ãã£ã¦ããªããããã¡ã¼ã¸ã¼ãªã¼ããçºçããªã REPEATABLE READ ã確èªåºæ¥ã¾ããã次ã«ã©ã¤ã¿ã¼ã§å ¨è¡ v = 0 ã«å¤æ´ãã¾ãã
[ WRITER1 ]> UPDATE t1 SET v = 0;
Query OK, 4 rows affected (0.25 sec)
Rows matched: 4 Changed: 4 Warnings: 0
ãªã¼ãã¼ã§ k = 1 è¡ãå ±æããã¯èªåããã¦ã¿ã¾ãã
[ READER ]> SELECT * FROM t1 WHERE k = 1 LOCK IN SHARE MODE;
+---+------+
| k | v |
+---+------+
| 1 | 0 |
+---+------+
1 row in set (0.13 sec)
REPEATABLE READ ã§ããããã¯èªåãã«ãã v = 0 ã®ææ°å¤ãè¦ãã¦ãã¾ã£ã¦ãã¾ãããã®ã¾ã¾å ¨è¡ SELECT ããã¨
[ READER ]> SELECT * FROM t1;
+-----+------+
| k | v |
+-----+------+
| 1 | 1 |
| 10 | 1 |
| 100 | 1 |
| 200 | 0 |
+-----+------+
4 rows in set (0.08 sec)
1 ã«æ»ãã¾ããã第10å ããã¯èªã¿åã㯠READ COMMITTED ã§è§£èª¬ãã MySQL åºæã®æåã§ãã
performance_schema ã§ããã¯ã確èª
ãã®ç¶æ ã§ãªã¼ãã¼ã¯å ±æããã¯ããæã£ã¦ãã¾ããããperformance_schema ã® data_locks ãã¼ãã«ã§ç¢ºèªãã¦ã¿ã¾ãã
[ READER ]> SELECT * FROM performance_schema.data_locks\G
Empty set (0.13 sec)
æ®å¿µãªããæã£ã¦ããçã®ããã¯ã¯ç´æ¥ç¢ºèªãããã¨ãåºæ¥ã¾ããã§ãããã©ã¤ã¿ã¼ã§åæ§ã«ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããk = 1 ã§å ±æããã¯ãåå¾ã performance_schema ã§ç¢ºèªãã¦ã¿ã¾ãã
[ WRITER1 ]> START TRANSACTION;
Query OK, 0 rows affected (0.47 sec)
[ WRITER1 ]> SELECT * FROM t1 WHERE k = 1 LOCK IN SHARE MODE;
+---+------+
| k | v |
+---+------+
| 1 | 0 |
+---+------+
1 row in set (0.17 sec)
[ WRITER1 ]> SELECT * FROM performance_schema.data_locks WHERE LOCK_TYPE = 'RECORD' \G
*************************** 1. row ***************************
ENGINE: INNODB
ENGINE_LOCK_ID: 23076404210640:21:4:2:23076435686432
ENGINE_TRANSACTION_ID: 304551380921296 <=== ãã©ã³ã¶ã¯ã·ã§ã³ID
THREAD_ID: 706
EVENT_ID: 29
OBJECT_SCHEMA: learning_mysql
OBJECT_NAME: t1
PARTITION_NAME: NULL
SUBPARTITION_NAME: NULL
INDEX_NAME: PRIMARY
OBJECT_INSTANCE_BEGIN: 23076435686432
LOCK_TYPE: RECORD
LOCK_MODE: S,REC_NOT_GAP <=== å
±æã¬ã³ã¼ãããã¯
LOCK_STATUS: GRANTED
LOCK_DATA: 1 <=== k = 1 è¡
1 row in set (0.16 sec)
ãã©ã³ã¶ã¯ã·ã§ã³ID ãä¸æçãªå·¨å¤§ãª 64ãããå¤ã«ãªã£ã¦ãããªã© MySQL InnoDB ã®ã¨ãã¨å ¨ãåãæåã§ãS,REC_NOT_GAP ã®å ±æã®åç´ãªã¬ã³ã¼ãããã¯ãä¿æãã¦ãããã¨ããããã¾ãã
第11åã¾ã¨ã
– Aurora ã§ã¯ã¯ã©ã¤ã¢ã³ã㧠transaction_isolation ãã°ãã¼ãã«å¤æ´åºæ¥ãªããã»ãã·ã§ã³ã®ã¿ãæä¹
çãªããã©ã¡ã¼ã¿ã¼ã°ã«ã¼ãã§
– Aurora ã®ãªã¼ãã¼ã§ã¯æ´æ°ããã³ SELECT … FOR UPDATE ã¯å®è¡åºæ¥ãªã
– ãªã¼ãã¼ã§ transaction_isolation è¨å®ã¯ç¡è¦ãããããaurora_read_replica_read_committed è¨å®ã¨çµã¿åããã㨠READ COMMITTED ãå®ç¾åºæ¥ã
– ãã ããªã¼ãã¼ã® READ COMMITTED 㯠MySQL ã¨æåãç°ãªãã®ã§ Aurora ã¬ããªã«ã§ã® READ COMMITTED ã®åä½ã®éã ã¯å¿
ãåç
§
– ããã©ã«ã REPEATABLE READ ã§ã® MySQL ã¨ã®äºææ§ã¯é«ããããã¯èªåãã«ãã READ COMMITTED çãã¡ã¸ã¼ãªã¼ãã¾ã§åã
ä»¥ä¸ Aurora 㯠MySQL ã¨ã®äºææ§ãé«ããã¨ããã¥ã¼ãã³ã°ææ³ã¨ã㦠READ COMMITTED ãæ¡ç¨ããå ´åã«ã¯å ¥å¿µã«æ¤è¨ããå¿ è¦ããããã¨ããããã¾ããï¼
次å第12å㯠MySQL äºæ DB ã¨ãã¦äººæ°ãé«ã PingCAP TiDB ã¨ã®éãã«ã¤ãã¦è§£èª¬ãããã¨æãã¾ãããæå¾ ãã ããï¼