MySQL ãã¡ã³ã®çæ§ãã¾ãããã¾ããï¼ MySQL ããã³ MySQL äºæ DB ã®ããã¯ã«é¢ãã¦è§£èª¬ããã·ãªã¼ãºãæå¾ã¨ãªã第ï¼ï¼åã®ä»å㯠PingCAP 社㮠TiDB ã«ã¤ãã¦ç´¹ä»ãã¾ãï¼
â
第ï¼å ãã©ã³ã¶ã¯ã·ã§ã³åé¢ã¬ãã«
â
第ï¼å ããã¯ã¢ãã¿ã¼ã®èªã¿æ¹
â
第ï¼å ããã¯èªåãã SELECT ã¯æ¢ããããªã
â
第ï¼å INSERT ãæ¢ããã¤ã³ãã³ã·ã§ã³ããã¯
â
第ï¼å WHERE æ¡ä»¶ã¨éãããã¯èªåã
â
第ï¼å performance_schema
â
第ï¼åã®ã£ããããã¯ã¨æ¶ããããã¯
â
第ï¼å ãã¯ã¹ããã¼ããã¯ã¨éé ã¤ã³ããã¯ã¹
â
第ï¼å å
±æããã¯ã§ãããããã¯
â
第ï¼ï¼å ããã¯èªã¿åã㯠READ COMMITTED
â
第ï¼ï¼å Aurora 㨠MySQL ã®éã
â
第ï¼ï¼å TiDB 㨠MySQL ã®éã
TiDB ã¨ã¯
TiDB 㯠Google Spanner ã«ã¤ã³ã¹ãã¤ã¢ããéçºããããªã¼ãã³ã½ã¼ã¹ã®åæ£ SQL ãã¼ã¿ãã¼ã¹ã§ãã
TiDB ã¯ãã©ã³ã¶ã¯ã·ã§ã³å¦çå¯è½ãªãã¼ããªã¥ã¼ã¹ã㢠(KVS) ã® RocksDB ããã¼ã¹ã«ã Raft ã®åæå½¢æã«ãã£ã¦å¤æ°ã®ãµã¼ãã¼ã§åãåæ£ KVS ã¨ãã¦ä½ããã TiKV ãã¹ãã¬ã¼ã¸ã¬ã¤ã¤ã¼ã¨ãã¦ä½¿ãã ãã®ä¸ã§ SQL ã¬ã¤ã¤ã¼ã® TiDB ãåä½ãããã¨ã§ãåæ£ RDB ãå®ç¾ãã¦ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ãã㯠MySQL äºæã®åä¸ãªå·¨å¤§ãã¼ã¿ãã¼ã¹ã¨ãã¦ä½¿ããã¨ãã§ããããå½å ã§ãæ¡ç¨ããäºä¾ãå¢ãã¦ãããä»æ³¨ç®ã®ãã¼ã¿ãã¼ã¹ã¨è¨ãã¾ãã
ç¡æã® Serverless ãæ´»ç¨
ãªã¼ãã³ã½ã¼ã¹ã§æä¾ããã¦ãã TiDB ã¯å ¬å¼ã»éå ¬å¼ã§å¤æ§ãªå½¢æ ã§ã®å©ç¨ãå¯è½ã§ãã
- åãµã¼ãã¼ãããã»ã¹ã¨ãã¦ããã¼ã«ã«ç°å¢ã« TiDB ãã¹ã¦ã®ãµã¼ãã¼ãç°¡åã«æ§ç¯ãã¦è©¦ãã tiup playground
- ï¼éå ¬å¼ã§ï¼playground ã docker compose ç°å¢ã§ä½ãã ti-click/docker-tidb-playground
- åã¦ã¼ã¶ã¼å°ç¨ã®ã¯ã©ã¦ã VPC ã«ããã¼ã¸ããµã¼ãã¹ãå±éãã TiDB Dedicated https://pingcap.co.jp/tidb-dedicated/
- ä¸å®éã¾ã§ç¡æå©ç¨ã§ãããã«ããã¼ã¸ããµã¼ãã¹ã® TiDB Serverless https://pingcap.co.jp/tidb-serverless-pricing-details
Serverless ã¯ã¹ãã¬ã¼ã¸å®¹é 25GBããªã¯ã¨ã¹ã㯠250 ç¾ä¸åã¾ã§ç¡æã§ä½¿ãã¦æ¤è¨¼ãç 究ãªã©ã®ç¨éã«ã¯ååãªéãããã¾ããä»åã¯ãã® Serverless
ã使ã£ã¦ã¿ã¾ããï¼
TiDB Cloud ã®ãµã¤ã㧠Start Free ãã¿ã³ãæ¼ãã¦ãµã¤ã³ã¢ãããã¾ãã
ãµã¤ã³ã¢ããå¾ä½æããã¯ã©ã¹ã¿ãé¸æã
Connect ãã¿ã³ãæ¼ãã¨æ¥ç¶æ å ±ãæãã¦ãããç»é¢ã«ãªãã¾ãã
ãã®ç»é¢ã§æ示ãããæ¥ç¶æ å ±ã¯ä»¥ä¸ã«ãªã
- æ¥ç¶å ã¨ã³ããã¤ã³ã
- èªåçæããã root å½¹å²ã®ã¦ã¼ã¶ã¼å
- ãã®ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãã®ãªã»ãã
--ssl-ca
ã§æå®ãã pem å½¢å¼ã® CA certification ãã¡ã¤ã«- ãã¼ã㯠4000
ä»ä¸ããã MySQL ã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãããªã»ããããããSSL ã§æ¥ç¶ããããã® pem å½¢å¼ã®éµãã¡ã¤ã«ããã¦ã³ãã¼ãããããããã¨ãã§ãã¾ãããããã使ã£ã¦ mysql ã¯ã©ã¤ã¢ã³ãã§æ¥ç¶ããã³ãã³ãä¾ããã¡ãã
(ä¾) mysql --ssl-ca=isrgrootx1.pem -h gateway01.eu-central-1.prod.aws.tidbcloud.com -P 4000 -uXXXXXXXXXXXX.root -pXXXXXXXXXX mydatabase
REPEATABLE READ ã®ã®ã£ããããã¯
MySQL InnoDB REPEATABLE READ ã®ç¹å¾´ã¨ãã¦ã ç¯å²ã«å¯¾ãã¦ã®ããã¯èªã¿åãã§ã®ã£ããããã¯ãåå¾ãããã®ã®ã£ããã¸ã® INSERT ãé²æ¢ããæ©è½ãããã¾ããTiDB ã REPEATABLE READ ãããã©ã«ãã®åé¢ã¬ãã«ã§ãããã®ã£ããããã¯ã®åä½ãæ¤è¨¼ãã¦ã¿ã¾ãã
+-----+------+
| k | v |
+-----+------+
| 1 | 0 |
| 10 | 0 |
| 100 | 0 |
+-----+------+
k = 1,10,100 ã® 3è¡ãæ ¼ç´ããã t1 ãã¼ãã«ãããã¾ãã 端æ«ï¼ã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããk > 10 ã®ç¯å²ã§ FOR UPDATE ã® SELECT ã«ããæä»ããã¯ãè¨ç½®ãã¾ãã
[1]> BEGIN;
Query OK, 0 rows affected (0.80 sec)
[1]> SELECT * FROM t1 WHERE k > 10 FOR UPDATE;
+-----+------+
| k | v |
+-----+------+
| 100 | 0 |
+-----+------+
1 row in set (0.25 sec)
ãã®ç¶æ ã§ç«¯æ«ï¼ã§ããã©ã³ã¶ã¯ã·ã§ã³ãéå§ããk = 50 ã« INSERT ãã¦ã¿ã¾ãã
[2]> BEGIN;
Query OK, 0 rows affected (0.25 sec)
[2]> INSERT INTO t1 VALUES (50, 2);
Query OK, 1 row affected (0.25 sec)
INSERT åºæ¥ã¦ãã¾ãã¾ããï¼
TiDB ã® REPEATABLE READ ã MySQL InnoDB ã¨åãããã¡ã³ãã ãªã¼ããé²ãæ©è½ã¯ãããMySQL ã¨ã®äºææ§ã¯é«ãã§ããããæ®å¿µãªããã®ã£ããããã¯ã¯åãã£ã¦ããªããã ç¯å²ã«å¯¾ã㦠FOR UPDATE ã®ããã¯ãè¨ç½®ãã¦ãééã¸ã® INSERT ãé²ããã¨ãåºæ¥ã¾ããã
端æ«ï¼ã¤ã¨ã rollback ã§ãã©ã³ã¶ã¯ã·ã§ã³ãçµäºããã 端æ«ï¼ã§ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããk = 100 ã®è¡ã§ LOCK IN SHARE MODE ãããå¤æ´ãç¦æ¢ãã¦ã¿ã¾ãã
[1]> BEGIN;
Query OK, 0 rows affected (0.63 sec)
[1]> SELECT * FROM t1 WHERE k = 100 LOCK IN SHARE MODE;
+-----+------+
| k | v |
+-----+------+
| 100 | 0 |
+-----+------+
1 row in set (0.24 sec)
端æ«ï¼ã§ãªã¼ãã³ãããã®ï¼ã¯ã¨ãªã§ FOR UPDATE ãå®è¡ãã¦ã¿ã¾ãã
[2]> SELECT * FROM t1 WHERE k = 100 FOR UPDATE;
+-----+------+
| k | v |
+-----+------+
| 100 | 0 |
+-----+------+
1 row in set (0.25 sec)
ãããã¯ãããã«éã£ã¦ãã¾ãã¾ããï¼ ããã¯å¾è¿°ãã¾ããã ãªã¼ãã³ãããã®ã¯ã¨ãªã¯èªåçã«å¾è¿°ããã楽観çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ããã§åã ããã é常㮠RDBMS ã¨è¿ãæåã®ããã©ã«ããæ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ããã¨ã¯ç°ãªãåä½ã«ãªãã¾ãã
ããããã¦ç«¯æ«ï¼ã§ã端æ«ï¼ã¨åæ§ãæ示çã«ãã©ã³ã¶ã¯ã·ã§ã³ãéå§ããFOR UPDATE ã試ãã¦ã¿ã¾ãã
[2]> BEGIN;
Query OK, 0 rows affected (0.25 sec)
[2]> SELECT * FROM t1 WHERE k = 100 FOR UPDATE;
ãããã¯ããã¾ãããã¤ã³ã¿ã¼ãããçµç±ã®ãµã¼ãã¼ã¬ã¹ç°å¢ã®ããããCTRL-C ã§ã¯ãã©ã³ã¶ã¯ã·ã§ã³ãã¢ãã¼ããããªããã¨ãããããã端æ«ï¼ãã kill ã³ãã³ãã§ã¯ã¨ãªã¯åæ¢ãã¾ãã
[2]> SELECT * FROM t1 WHERE k = 100 FOR UPDATE;
ERROR 1105 (HY000): context canceled
å ±æããã¯ã«å¯¾ãã¦æä»ããã¯ããããã¯ãããã®ã¯æ³å®ã©ããã§ãã次ã«åã LOCK IN SHARE MODE ã試ãã¾ãã
[2]> BEGIN;
Query OK, 0 rows affected (0.24 sec)
[2]> SELECT * FROM t1 WHERE k = 100 LOCK IN SHARE MODE;
ERROR 1105 (HY000): context canceled
FOR UPDATE ã¨åæ§ã«ãããã¯ããã¾ããï¼ TiDB ã§ã¯ LOCK IN SHARE MODE ãåå¨ããè¡ã«å¯¾ãã¦å®è¡ããã¨ãFOR UPDATE ã¨åãæä»ããã¯ã®åãã¨ãªãããã§ãã
次ã«å ç¨ã® FOR UPDATE ã¨åãããã« k > 10 ã®ç¯å²ã« LOCK IN SHARE MODE ãããã¦ã¿ã¾ããã
[1]> BEGIN;
Query OK, 0 rows affected (0.24 sec)
[1]> SELECT * FROM t1 WHERE k > 10 LOCK IN SHARE MODE;
ERROR 1235 (42000): function LOCK IN SHARE MODE has only noop implementation in tidb now, use tidb_enable_noop_functions to enable these functions
ç¯å²ã«å¯¾ã㦠LOCK IN SHARE MODE ã®ããã¯èªã¿åããå®è¡ããã¨ãå¹æã®ç¡ãã¯ã¨ãªã¨ãã¦ã¨ã©ã¼ã«ãªãã¾ããï¼ tidb_enable_noop_functions
ãæå¹ã«ããã°ã¨ã©ã¼ã«ã¯ãªããã¹ã«ã¼ãããããã«ã¯ãªãã¾ããã å¹æãç¡ãã®ããããæ¹ãããããåç´ã«ã¯ã¨ãªã®ä¿®æ£ãä¸è¦ãªæ¹ããããã¯éç¨ã«åããã¦ãé¸ã³ããã ããã°ã¨æãã¾ãï¼
æ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¨æ¥½è¦³çãã©ã³ã¶ã¯ã·ã§ã³
TiDB ã¯æ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¨æ¥½è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¨ããï¼ã¤ã®ã¢ã¼ããããã ããã©ã«ã㯠tidb_txn_mode å¤æ°ã§ç¢ºèªåºæ¥ãããã«æ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ãã§åãã¦ãã¾ãã
[1]> SELECT @@tidb_txn_mode;
+-----------------+
| @@tidb_txn_mode |
+-----------------+
| pessimistic |
+-----------------+
1 row in set (0.27 sec)
BEGIN ã« OPTIMISITC ãã©ã¡ã¼ã¿ã¼ã追å ãã¦ç«¯æ«ï¼ã§ãã©ã³ã¶ã¯ã·ã§ã³ã楽観çã¢ã¼ãã§éå§ããk = 1 ã®è¡ã v = 1 ã«ãã¾ãã
[1]> BEGIN OPTIMISTIC;
Query OK, 0 rows affected (0.24 sec)
[1]> UPDATE t1 SET v = 1 WHERE k = 1;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
次ã«ç«¯æ«ï¼ã§ã k = 1 ã®è¡ã v = 2 ã«ãã¾ãã
[2]> BEGIN OPTIMISTIC;
Query OK, 0 rows affected (0.24 sec)
[2]> UPDATE t1 SET v = 2 WHERE k = 1;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
両æ¹ã®ãã©ã³ã¶ã¯ã·ã§ã³ã COMMIT ãã¦ããªãç¶æ ã§ã¯ããããã¯ãããã«ä¸¡æ¹ã®å¤æ´ãéã£ãå½¢ã«ãªã£ã¦ãã¾ãã ããã§ä¸¡æ¹ã®ãã©ã³ã¶ã¯ã·ã§ã³ã COMMIT ãã¾ãã
[1]> COMMIT;
Query OK, 0 rows affected (0.25 sec)
[2]> COMMIT;
ERROR 9007 (HY000): Write conflict, txnStartTS=452493005972045832, conflictStartTS=452492983047553030, conflictCommitTS=452493040771661830, key=????, reason=Optimistic [try again later]
ãã¨ã§ COMMIT ãã端æ«ï¼ã®æ¹ãã¨ã©ã¼ã«ãªãã¾ããã
MySQL ãæ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ãã§ã®ãããããã¯æã¨åãããã«ã 楽観çãã©ã³ã¶ã¯ã·ã§ã³ã¯ããã¯ã使ç¨ããä¸æ¹ã®ãã©ã³ã¶ã¯ã·ã§ã³ã失æã«ãããã¨ã§åææ´æ°ã®è¡çªã解決ãã¾ãã
楽観çãã©ã³ã¶ã¯ã·ã§ã³ã§ã®ãããããã¯ç¢ºèª
楽観çãã©ã³ã¶ã¯ã·ã§ã³ã§ã® UPDATE ã«ããããããããã¯ã¯ã©ããªããè¦ã¦ã¿ã¾ãããã 端æ«ï¼ã¯ k = 1 ã端æ«ï¼ã¯ k = 100 ã§ããããå¤æ´ãã¾ãã
[1]> BEGIN OPTIMISTIC;
Query OK, 0 rows affected (0.24 sec)
[1]> UPDATE t1 SET v = 0 WHERE k = 1;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
[2]> BEGIN OPTIMISTIC;
Query OK, 0 rows affected (0.24 sec)
[2]> UPDATE t1 SET v = 2 WHERE k = 100;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
次ã«äºãã®è¡ã«å¯¾ãã¦æ´æ°ãããã¦ã¿ã¾ãã
[1]> UPDATE t1 SET v = 1 WHERE k = 100;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
[2]> UPDATE t1 SET v = 2 WHERE k = 1;
Query OK, 1 row affected (0.24 sec)
Rows matched: 1 Changed: 1 Warnings: 0
ã¯ã¨ãªèªä½ã¯ä¸¡è ãããããã¯ããæ´æ°åºæ¥ã¦ãã¾ãã¾ããï¼ æ¬¡ã« COMMIT ããã¨
[1]> COMMIT;
Query OK, 0 rows affected (0.24 sec)
[2]> COMMIT;
ERROR 9007 (HY000): Write conflict, txnStartTS=452494708322336783, conflictStartTS=452494699409178625, conflictCommitTS=452494752952090629, key=????, reason=Optimistic [try again later]
ï¼ã¤ç®ã®ãã©ã³ã¶ã¯ã·ã§ã³ã¯ã¨ã©ã¼ã«ãªããã¼ã«ããã¯ããã¾ãã ãã®ããã«æ¥½è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ãã§ã¯ãåææ´æ°ã®è¡çªã¨åããããããããã¯ãåæ§ã« COMMIT ã®ã¿ã¤ãã³ã°ã§ä¸æ¹ã®å¤±æã¨ãã¦å¦çããã¾ãã
ã¾ã¨ã
- TiDB ã® REPEATABLE READ 㯠MySQL ã¨åãããã¡ã³ãã ãªã¼ããé²ãããã®ã£ããããã¯ã¯ç¡ããã INSERT ã¯ãããã¯åºæ¥ãªã
- ããã©ã«ãã¯æ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ãã ã楽観çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ããããããªã¼ãã³ãããã®ã¯ã¨ãªã¯å¸¸ã«æ¥½è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã«
- æ²è¦³çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ã
- æä»ããã¯ã®æåããããããã¯ã®å¦çãªã© MySQL ã¨åãæå
- LOCK IN SHARE MODE ã®å¯¾è±¡è¡ãåå¨ãã¦ããã¨å®è¡åºæ¥ããããã®å¹æ㯠FOR UPDATE ã¨åãæä» ã§åä½ããã
- ç¯å²ã«å¯¾ãã¦ã® LOCK IN SHARE MODE ã¯ç¡å¹æã§ã¨ã©ã¼ã«ãªãï¼ãªãã·ã§ã³ã«ãã£ã¦ã¹ã«ã¼åã¯å¯è½ï¼
- 楽観çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ã
- æ´æ°ãããã¯èªã¿åãã«ããããã¯çãããã¯ã¯ä½ç¨ããªã
- ãããããã¯ãåææ´æ°ã®è¡çªã¯ COMMIT æã«ä¸æ¹ã®ãã¼ã«ããã¯ã§å¦çããã
åæ£ SQL ãã¼ã¿ãã¼ã¹ã® TiDB ã® MySQL InnoDB ã¨ã¯ç°ãªãããã¯ã¾ããã®æåã«ã¤ãã¦è§£èª¬ãã¾ããã
MySQL ã®æåã¨ã¯éã£ã¦ã¯ããæã¯ãããã®ã®ãããã¯å¾ ã¡ãç¡ã楽観çãã©ã³ã¶ã¯ã·ã§ã³ã¢ã¼ãã¯æ§è½ãèããã¨é åçã§ã æ·±ãç解ããã¨ãã§å¤§è¦æ¨¡ãã¼ã¿ãã¼ã¹ã¨ãã¦ã®æ§è½ãããçºæ®åºæ¥ããã§ãã
ï¼ï¼åã«ããã解説ãã¦ãã¾ãã MySQL ã®ããã¯å ¥éã·ãªã¼ãºãä»åã® TiDB ã®ç´¹ä»ã§çµäºã¨ãªãã¾ãï¼ è§£èª¬ããããªãã£ããã¿ã¼ã³ãã¾ã ã¾ã ããã¾ãããããã¾ã§ã®ä¾ã§ç¤ºãããããªèª¿æ»æ¹æ³ã身ã«çãã¦ããã ããã°ã çãããåå MySQL ã«ç«ã¡åãã£ã¦ãããã¨æãã¾ãï¼ãããã¾ã§ã覧ããã ããããã¨ããããã¾ããï¼
å¼ç¤¾ã§ã¯ MySQL ã使ã£ããµã¼ãã¹ï¼ã·ã¹ãã æ§ç¯ã«é¢å¿ãããæ¹ã®å¿åããå¾ ã¡ãã¦ããã¾ãï¼ãèå³ã®ããæ¹ã¯æ¯éãé£çµ¡ãã ããï¼â æ¡ç¨æ å ±