mysql 和 TiDB 在 snapshot 隔离级别上的区别

Posted adream307

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 和 TiDB 在 snapshot 隔离级别上的区别相关的知识,希望对你有一定的参考价值。

如果一个事务包含多条 SQL 语句,在 snapshot 隔离级别上,mysql 并不是事务开始的时候获得一个 snapshot 而是在执行第一条 SQL 语句的时候获得 snapshot 的; 而 TiDB 是在事务开始的时候获得一个 snapshot

参考 https://pingcap.com/blog-cn/tikv-source-code-reading-12/ TiDB 在事务开始的时候从 PD 获得一个 start_TS,这个 start_TS 就标记一个 snapshot

启动 mysql

docker run --rm  --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306  mysql:5.6

测试 mysql

client 1

$ mysql --user=root --password=123456  --port=3306 --host=127.0.0.1

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed
mysql> create table t1(a int);
Query OK, 0 rows affected (0.04 sec)

mysql> insert into t1 values(1);
Query OK, 1 row affected (0.02 sec)

mysql> select * from t1;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> 

client 2

$ mysql --user=root --password=123456  --port=3306 --host=127.0.0.1

mysql> use test;
Database changed
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> 

client 1

mysql> create table t2(b int);
Query OK, 0 rows affected (0.03 sec)

mysql> select * from t2;
+------+
| b    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> 

client 2

mysql> select * from t2;
+------+
| b    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

client 1

mysql> create table t3(a int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t3 values(1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from t3;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

client 2

mysql> select * from t3;
ERROR 1412 (HY000): Table definition has changed, please retry transaction

client 2

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from t3;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

启动 TiDB

docker-compose.yml

version: '3.5'

services:
 pd0:
   image: pingcap/pd:latest
   ports:
     - "2379:2379"
     - "2380:2380"
   volumes:
     - /etc/localtime:/etc/localtime:ro
   command:
     - --name=pd0
     - --client-urls=http://0.0.0.0:2379
     - --peer-urls=http://0.0.0.0:2380
     - --advertise-client-urls=http://pd0:2379
     - --advertise-peer-urls=http://pd0:2380
     - --initial-cluster=pd0=http://pd0:2380
     - --data-dir=/data/pd0
     - --log-file=/logs/pd0.log

 tikv0:
   image: pingcap/tikv:latest
   ports:
     - "20160:20160"
   volumes:
     - /etc/localtime:/etc/localtime:ro
   command:
     - --addr=0.0.0.0:20160
     - --advertise-addr=tikv0:20160
     - --data-dir=/data/tikv0
     - --pd=pd0:2379
     - --log-file=/logs/tikv0.log
   depends_on:
     - "pd0"

 tikdb0:
   image: pingcap/tidb:latest
   ports:
     - "4001:4000"
   volumes:
     - /etc/localtime:/etc/localtime:ro
   command:
     - --store=tikv
     - --path=pd0:2379
     - --P=4000
   depends_on:
     - "tikv0"

 tikdb1:
   image: pingcap/tidb:latest
   ports:
     - "4002:4000"
   volumes:
     - /etc/localtime:/etc/localtime:ro
   command:
     - --store=tikv
     - --path=pd0:2379
     - --P=4000
   depends_on:
     - "tikv0"

启动 TiDB

docker-compose up -d

测试 TiDB

client 1

$ mysql -h 127.0.0.1 -P 4001 -u root

mysql> use test
Database changed
mysql> create table t1(a int);
Query OK, 0 rows affected (0.14 sec)

mysql> insert into t1 values(1);
Query OK, 1 row affected (0.04 sec)

mysql> select * from t1;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> 

client 2

$ mysql -h 127.0.0.1 -P 4001 -u root
mysql> use test;
Database changed
mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> 

client 1

mysql> create table t2(a int);
Query OK, 0 rows affected (0.14 sec)

mysql> insert into t2 values(1);
Query OK, 1 row affected (0.04 sec)

mysql> select * from t2;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> 

client 2

mysql> select * from t2;
ERROR 1146 (42S02): Table 'test.t2' doesn't exist

client 2

mysql> commit;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from t2;
+------+
| a    |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> 

以上是关于mysql 和 TiDB 在 snapshot 隔离级别上的区别的主要内容,如果未能解决你的问题,请参考以下文章

TiDB笔记

一个长耗时SQL在TiDB和Mysql上的耗时测试

TIDB - TIDB用户角色权限管理

tidb入门

在MySQL和分布式TiDB之间迁移数据

我们为什么放弃 MongoDB 和 MySQL,选择 TiDB