
带你搞定MySQL实战,轻松对应海量业务处理及高并发需求,从容应对大场面试

如果英文不好的话,可以参考 searchdoc 翻译的中文版本
http://www.searchdoc.cn/rdbms/mysql/dev.mysql.com/doc/refman/5.7/en/index.com.coder114.cn.html

通常来说 ,有以下几点
慢SQL(重点) 、 主机的硬件资源(CPU、内存、磁盘I/O等)、网卡流量等等
啥叫大表? 粗略的定义 ,可以从两个维度去考虑,仅供参考
事务的4个特性: ACID
原子性 atomicity | 一致性 consistency | 隔离性 isolation | 持久性 durability
所以,综上.我们可以理解一致性就是:应用系统从一个正确的状态到另一个正确的状态.而ACID就是说事务能够通过AID来保证这个C的过程.C是目的,AID都是手段.
事务的操作,一旦提交,对于数据库中数据的改变是永久性的,即使数据库发生故障也不能丢失已提交事务所完成的改变。
我们以MySQL数据库为例子,对比下这两种事务隔离级别对查询数据的影响
打开两个会话
会话一 :
# 连接mysql
[root@artisan ~]# mysql -u root -p
Enter password:
....
....
....
....
# 切到artisan数据库
mysql> use artisan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> desc t_test; # 查看t_test表结构
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from t_test; # 查询数据
+----+
| id |
+----+
| 1 |
| 3 |
| 5 |
| 7 |
| 9 |
+----+
5 rows in set (0.00 sec)
mysql> show variables like '%iso%' # 查看隔离级别
-> ;
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.00 sec)
mysql> begin; # 开启事务
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t_test where id < 7;
+----+
| id |
+----+
| 1 |
| 3 |
| 5 |
+----+
3 rows in set (0.00 sec)
mysql> 然后 切到会话二 ,插入几条数据
[root@artisan ~]# mysql -u root -p
Enter password:
.....
.....
.....
mysql> use artisan;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from t_test;
+----+
| id |
+----+
| 1 |
| 3 |
| 5 |
| 7 |
| 9 |
+----+
5 rows in set (0.00 sec)
mysql> begin ; # 开启事务
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t_test values(2); #插入数据
Query OK, 1 row affected (0.00 sec)
mysql> commit; #提交事务
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t_test;
+----+
| id |
+----+
| 1 |
| 3 |
| 5 |
| 7 |
| 9 |
| 2 |
+----+
6 rows in set (0.00 sec)
mysql> 重新切回到 会话一,重复执行刚才的SQL (此时,会话一这个事务还未提交,还在事务中)

可以看到,在 **REPEATABLE-READ (可重复读)**这种隔离级别下, 事务一 在事务内,每次查询到的数据都是一样的,而且也无法读取到事务二已经提交的数据。
这也就理解了为啥叫 “可重复读” : 因为 它保证同一事务的多个实例在并发读取事务时,会“看到同样的”数据行 。
那 这种事务级别潜在的问题是啥呢 ? --------> 会导致另外一个棘手问题“幻读”。InnoDB和Falcon存储引擎通过多版本并发控制机制解决了幻读问题。
幻读是事务非独立执行时发生的一种现象,例如事务T1批量对一个表中某一列列值为1的数据修改为2的变更,但是在这时,事务T2对这张表插入了一条列值为1的数据,并完成提交。此时,如果事务T1查看刚刚完成操作的数据,发现还有一条列值为1的数据没有进行修改,而这条数据其实是T2刚刚提交插入的,这就是幻读
我们修改下 MySQL的隔离级别为 read-committed
我们把会话一的事务先提交了,然后修改下隔离级别
mysql> commit ;
Query OK, 0 rows affected (0.00 sec)
mysql> set session tx_isolation='read-committed'; # 设置隔离级别
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show variables like '%iso%'
-> ; #查看隔离级别
+-----------------------+----------------+
| Variable_name | Value |
+-----------------------+----------------+
| transaction_isolation | READ-COMMITTED |
| tx_isolation | READ-COMMITTED |
+-----------------------+----------------+
2 rows in set (0.01 sec)
mysql> 然后切到会话二

回切到会话一,重新查询

所以 READ-COMMITTED 又被称为不可重复读 ,因为对于数据库中的某个数据,一个事务执行过程中多次查询返回不同查询结果,这就是在事务执行过程中,数据被其他事务提交修改了。 每次查询都有可能查询到其他事务修改过的数据,所以称为 不可重复读。
不可重复读 VS 脏读 VS 可重复读

隔离行由低到高 : Read UnCommitted —> Read Committed —> Repeatable Read -----> Serializable
并发性由高到低 : Read UnCommitted —> Read Committed —> Repeatable Read -----> Serializable
全局修改需要修改MySql的全局文件my.cnf (linux操作系统)
#可选参数有:READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE.
[mysqld]
transaction-isolation = REPEATABLE-READ修改当前会话Session的隔离级
mysql> set session tx_isolation = 'REPEATABLE-READ';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show variables like '%iso%'
-> ;
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
| tx_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
2 rows in set (0.00 sec)
mysql> 另外MySql中有autoCommit参数,默认为on,也就是开启状态
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql> 如果需要关闭autocommit,我们可以使用下面语句设置
mysql> set autocommit=0;0就是OFF,1就是ON。设置为OFF之后,则用户执行语句之后,将一直处于一个事务中,直到执行commit或者rollback,才会结束当前事务,重新开始新的事务。
定义: 运行时间比较长,操作数据比较多的事务