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. OLEDB读取EXCEL表格时,某些字段为空,怎么办?

    转载:http://blog.sina.com.cn/s/blog_53864cba01011cbn.html   前些日子,写了一个Excel导入数据库的共同Batch,突然有一天发现当我修改Exc ...

  2. win 10应用商店下载应用错误码0x80070422

    Win10应用商店下载应用提示错误0x80070422怎么办? 一些安装了Win10系统的朋友们在使用过程中发现,在使用WIn8应用商店下载免费应用的时候,系统提示:错误0x80070422,这是怎么 ...

  3. android .9文件的一点处理

    Android上面有很多平台,造成比较严重的碎片问题,适配比较困难,作为应用,一般都需要图文并茂,图片又是比较占资源的.面对缩放的问题,于是出来了矢量图片文件,作一点矢量处理,于是就是.9图片,IOS ...

  4. MVC小系列(十三)【全局异常处理与异常日志】

    在MVC网站的global.asax中的Application_Start方法里,有这样一段代码 protected void Application_Start() { //它的主要作用是将全局过滤 ...

  5. ios NSAssert趣谈

    Apple 官网介绍 NSAssert 的定义如下: #define NSAssert(condition, desc, ...) \ do { \ __PRAGMA_PUSH_NO_EXTRA_AR ...

  6. c#汉字与编码之间的转换(输出十六进制)

    /******************************************************************/ /*********************** ****** ...

  7. IOS 学习笔记 2015-04-09 0C-SQLite 数据存储

    1 项目导入 libsqlite3.0.dylib 框架 2 在使用数据sqlite的头文件(.h)上导入 #Impourt <sqlite3.h> 3 推荐自己本地电脑下个sqlite ...

  8. IOS 学习笔记 2015-03-20 OC-集合-数组

    [NSArray] 一 定义 1 不可变数组 2 oc中数组的元素可以是任何对象 3 数字中装有元素的地址 二 初始化 NSArray *变量 = [[NSArry alloc] initWithOb ...

  9. mysql远程连接缓慢的问题

    这两天发现服务器程序启动的时候到了mysql初始连接的那一步很耗时,启动缓慢,后来发现,将连接的主机的-h参数改成localhost的时候 瞬间就完成连接了.后来在网上查到,原来是由于mysql服务器 ...

  10. Javascript数组的indexOf()、lastIndexOf()方法

    在javascript数组中提供了两个方法来对数组进行查找,这两个方法分别为indexOf(),lastIndexOf(). 这两个方法都有两个参数,第一个参数为需要查找的项,第二个参数则是查找的起始 ...