The locks are normally next-key locks that also block inserts into the “gap” immediately before the record. 

mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec) 第一种情况 更新列没有索引: CREATE TABLE `SmsTest` (
`sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号',
`phoneNo` int(16) NOT NULL,
`channelType` int(11) DEFAULT NULL COMMENT '通道识别',
`status` tinyint(4) NOT NULL COMMENT '短信转态,1.发送成功,2.发送失败,3.发送异常',
PRIMARY KEY (`sn`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='短信发送成功记录表'; mysql> show create table SmsTest\G;
*************************** 1. row ***************************
Table: SmsTest
Create Table: CREATE TABLE `SmsTest` (
`sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号',
`phoneNo` int(16) NOT NULL,
`channelType` int(11) DEFAULT NULL COMMENT '通道识别',
`status` tinyint(4) NOT NULL COMMENT '短信转态,1.发送成功,2.发送失败,3.发送异常',
PRIMARY KEY (`sn`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 COMMENT='短信发送成功记录表'
1 row in set (0.00 sec) ERROR:
No query specified mysql> show index from SmsTest;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| SmsTest | 0 | PRIMARY | 1 | sn | A | 16 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec) mysql> select * from SmsTest;
+----+---------+-------------+--------+
| sn | phoneNo | channelType | status |
+----+---------+-------------+--------+
| 1 | 1 | 2 | 1 |
| 2 | 2 | 2 | 1 |
| 3 | 3 | 2 | 1 |
| 4 | 4 | 2 | 1 |
| 5 | 5 | 2 | 1 |
| 6 | 6 | 2 | 1 |
| 7 | 7 | 2 | 1 |
| 8 | 8 | 2 | 1 |
| 9 | 9 | 2 | 1 |
| 10 | 10 | 1 | 1 |
| 16 | 16 | 45 | 56 |
| 17 | 17 | 2 | 1 |
| 18 | 18 | 1 | 1 |
| 19 | 19 | 1 | 1 |
| 20 | 20 | 2 | 1 |
| 21 | 10 | 1 | 1 |
+----+---------+-------------+--------+
16 rows in set (0.00 sec) /*********第一种情况 更新列没有索引: Session 1:
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec) mysql> select * from SmsTest where phoneNo=16 for update;
+----+---------+-------------+--------+
| sn | phoneNo | channelType | status |
+----+---------+-------------+--------+
| 16 | 16 | 45 | 56 |
+----+---------+-------------+--------+
1 row in set (0.00 sec) mysql[192.168.11.187] processid[3] root@localhost in db[zjzc] hold transaction time 55
You have new mail in /var/spool/mail/root 进程ID为3 Session 2: mysql> start transaction;
Query OK, 0 rows affected (0.00 sec) mysql> insert into SmsTest values(11,11,1,1); ##堵塞 2016-11-25 10:03:19,152,3,root,localhost,zjzc
mysql[192.168.11.187] processid[3] root@localhost in db[zjzc] hold transaction time 152
112072530,26,insert into SmsTest values(11,11,1,1),112072529,3,
mysql[192.168.11.187] blocking_thread[3] blocking_query[] blocking waiting_thread[26]'s insert into SmsTest values(11,11,1,1)
You have new mail in /var/spool/mail/root mysql> insert into zjzc.SmsTest values(11,1,1,1); mysql> insert into zjzc.SmsTest values(110,110,1,1); ###堵塞 mysql[192.168.11.187] blocking_thread[3] blocking_query[] blocking waiting_thread[34]'s insert into zjzc.SmsTest values(110,110,1,1) mysql> insert into zjzc.SmsTest values(200,110,1,1); 112072532,34,insert into zjzc.SmsTest values(220,220,1,1),112072529,3,
mysql[192.168.11.187] blocking_thread[3] blocking_query[] blocking waiting_thread[34]'s insert into zjzc.SmsTest values(220,220,1,1) Vsftp:/root# mysql -uroot -p1234567 -e"show processlist"
Warning: Using a password on the command line interface can be insecure.
+----+------+-----------+------+---------+------+--------+----------------------------------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+--------+----------------------------------------------+
| 3 | root | localhost | zjzc | Sleep | 469 | | NULL |
| 34 | root | localhost | zjzc | Query | 7 | update | insert into zjzc.SmsTest values(220,220,1,1) |
| 41 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+--------+----------------------------------------------+ 更新列没有索引,此时锁全表 /*********第一种情况 更新列有索引,但是非唯一索引
mysql> show create table zjzc.SmsTest\G;
*************************** 1. row ***************************
Table: SmsTest
Create Table: CREATE TABLE `SmsTest` (
`sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号',
`phoneNo` int(16) NOT NULL,
`channelType` int(11) DEFAULT NULL COMMENT '通道识别',
`status` tinyint(4) NOT NULL COMMENT '短信转态,1.发送成功,2.发送失败,3.发送异常',
PRIMARY KEY (`sn`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 COMMENT='短信发送成功记录表'
1 row in set (0.00 sec) ERROR:
No query specified mysql> show index from SmsTest;
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| SmsTest | 0 | PRIMARY | 1 | sn | A | 15 | NULL | NULL | | BTREE | | |
+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec) 创建索引,在phoneNo 列上 mysql> start transaction;
Query OK, 0 rows affected (0.00 sec) mysql> select * from SmsTest where phoneNo=16 for update;
+----+---------+-------------+--------+
| sn | phoneNo | channelType | status |
+----+---------+-------------+--------+
| 16 | 16 | 45 | 56 |
+----+---------+-------------+--------+
1 row in set (0.00 sec) Session 2: mysql> insert into SmsTest values(99,9,1,1);
Query OK, 1 row affected (0.00 sec) mysql> insert into SmsTest values(99,10,1,1); ##堵塞 mysql> insert into SmsTest values(11,11,1,1); ##堵塞
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> mysql[192.168.11.187] processid[1] root@localhost in db[zjzc] hold transaction time 40
112074074,25,insert into SmsTest values(11,11,1,1),112074073,1,
mysql[192.168.11.187] blocking_thread[1] blocking_query[] blocking waiting_thread[25]'s insert into SmsTest values(11,11,1,1) mysql> insert into SmsTest values(12,12,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> insert into SmsTest values(13,13,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> insert into SmsTest values(14,14,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction mysql> insert into SmsTest values(14,14,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> insert into SmsTest values(15,15,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> insert into SmsTest values(16,16,1,1);
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
mysql> insert into SmsTest values(17,17,1,1);
ERROR 1062 (23000): Duplicate entry '17' for key 'PRIMARY'
mysql> insert into SmsTest values(99,17,1,1);
ERROR 1062 (23000): Duplicate entry '99' for key 'PRIMARY'
mysql> insert into SmsTest values(999,17,1,1);
Query OK, 1 row affected (0.00 sec) 更新列上有唯一索引 ,锁住的区间为【10,16】 /*********第一种情况 更新列有索引,且是唯一索引 mysql> create unique index SmsTest_idx1 on SmsTest(phoneNo);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table SmsTest\G;
*************************** 1. row ***************************
Table: SmsTest
Create Table: CREATE TABLE `SmsTest` (
`sn` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增编号',
`phoneNo` int(16) NOT NULL,
`channelType` int(11) DEFAULT NULL COMMENT '通道识别',
`status` tinyint(4) NOT NULL COMMENT '短信转态,1.发送成功,2.发送失败,3.发送异常',
PRIMARY KEY (`sn`),
UNIQUE KEY `SmsTest_idx1` (`phoneNo`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 COMMENT='短信发送成功记录表'
1 row in set (0.00 sec) ERROR:
No query specified Session 1: mysql> start transaction;
Query OK, 0 rows affected (0.00 sec) mysql> select * from SmsTest where phoneNo=16 for update;
+----+---------+-------------+--------+
| sn | phoneNo | channelType | status |
+----+---------+-------------+--------+
| 16 | 16 | 45 | 56 |
+----+---------+-------------+--------+
1 row in set (0.00 sec) Session 2: mysql> use zjzc;
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> insert into SmsTest values(11,11,1,1);
Query OK, 1 row affected (0.00 sec) mysql> rollback;
Query OK, 0 rows affected (0.00 sec) mysql> insert into SmsTest values(12,12,1,1);
Query OK, 1 row affected (0.00 sec) mysql> rollback;
Query OK, 0 rows affected (0.00 sec) mysql> insert into SmsTest values(13,13,1,1);
Query OK, 1 row affected (0.00 sec) mysql> rollback;
Query OK, 0 rows affected (0.00 sec) mysql> insert into SmsTest values(14,14,1,1);
Query OK, 1 row affected (0.00 sec) mysql> rollback;
Query OK, 0 rows affected (0.00 sec) mysql> insert into SmsTest values(15,15,1,1);
Query OK, 1 row affected (0.00 sec) mysql> rollback;
Query OK, 0 rows affected (0.00 sec) mysql> insert into SmsTest values(16,16,1,1); ##堵塞
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

mysql 区间锁 对于没有索引 非唯一索引 唯一索引 各种情况的更多相关文章

  1. RR区间锁 不是唯一索引,即使区间内没值,也锁

    +--------- +---------------------------------------------------------------------------------------- ...

  2. mysql索引之一:索引基础(B-Tree索引、哈希索引、聚簇索引、全文(Full-text)索引区别)(唯一索引、最左前缀索引、前缀索引、多列索引)

    没有索引时mysql是如何查询到数据的 索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储10 ...

  3. MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析

    文章出处:http://inter12.iteye.com/blog/1430144 MYSQL的全表扫描,主键索引(聚集索引.第一索引),非主键索引(非聚集索引.第二索引),覆盖索引四种不同查询的分 ...

  4. 温故知新-Mysql索引结构&页&聚集索引&非聚集索

    文章目录 摘要 索引 索引概述 索引优势劣势 索引结构 BTREE 结构 B+TREE 结构 页 索引分类 索引语法 索引设计原则 聚触索引 & 非聚触索引 你的鼓励也是我创作的动力 Post ...

  5. Mysql 索引实现原理. 聚集索引, 非聚集索引

    Mysql索引实现: B-tree,B是balance,一般用于数据库的索引.使用B-tree结构可以显著减少定位记录时所经历的中间过程,从而加快存取速度.而B+tree是B-tree的一个变种,My ...

  6. Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别

    Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...

  7. Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别

    原文:Mysql主键索引.唯一索引.普通索引.全文索引.组合索引的区别 Mysql索引概念: 说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不 ...

  8. Mysql 表约束 非空、唯一、主键、自增长、默认、外键约束(基础6)

    非空(not null).唯一(unique key).主键(primary key).自增长(auto_increment).默认约束(default) 准备基础环境: mysql> crea ...

  9. Gap Locks 区间锁

    Gap Locks 区间锁 1. 区间锁不能用于语句锁定记录使用一个唯一索引来搜索一个唯一的记录 2.READ COMMITTED 没有区间锁 区间锁是一个锁在一个在index记录间的区间,或者一个l ...

随机推荐

  1. MVC小系列(四)【向RouteData里扔数据】

    向RouteData里扔数据 当Url做路由之后,QueryString里当然是不可能再存你的信息了,而信息包括控制器,action,参数都会存储在RouteData里,而一般这里的信息都是通过前一个 ...

  2. 设置表格边框css样式

    table{ width:70%; text-align:center; border-left:#C8B9AE solid 1px; border-top:#C8B9AE solid 1px; bo ...

  3. 暑假集训(2)第九弹 ----- Points on Cycle(hdu1700)

                                                Points on Cycle Time Limit:1000MS     Memory Limit:32768 ...

  4. bzoj1231: [Usaco2008 Nov]mixup2 混乱的奶牛

    思路:状压dp,设f[i][j]表示当前已经选出的牛的状态为i,最后一头选出的牛为j的方案数. 然后注意就是初值不能是f[0][i]=1,因为所有牛本来都可以第一个被选中,然而这样一定初值有些牛可能就 ...

  5. itoa : Convert integer to string

      Quote from:  http://www.cplusplus.com/reference/cstdlib/itoa/   function   Required header : <s ...

  6. 通过shell脚本实现代码自动化部署

    通过shell脚本实现代码自动化部署 一.传统部署方式及优缺点 1.传统部署方式 (1)纯手工scp (2)纯手工登录git pull.svn update (3)纯手工xftp往上拉 (4)开发给打 ...

  7. jQuery如何检查某个元素在网页上是否存在

    $("ID")获取的永远是对象,即使网页上没有此元素.因此当要用jQuery检查某个元素在网页上是否存在时,不能使用以下代码: if($("#ID")){ // ...

  8. FBX SDK 从2012.1 到 2013.3 变化

    ==================================================== ============================== 译文               ...

  9. yii2源码学习笔记(十八)

    View继承了component,用于渲染视图文件:yii2\base\View.php <?php /** * @link http://www.yiiframework.com/ * @co ...

  10. echo和print语句

    在php中,有两种基本的输出方法:echo  和  print echo 和 print 之间的差异: echo——能够输出一个以上的字符串,无返回值 print——只能输出一个字符串,并始终返回值为 ...