mysql 区间锁 对于没有索引 非唯一索引 唯一索引 各种情况
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 区间锁 对于没有索引 非唯一索引 唯一索引 各种情况的更多相关文章
- RR区间锁 不是唯一索引,即使区间内没值,也锁
+--------- +---------------------------------------------------------------------------------------- ...
- mysql索引之一:索引基础(B-Tree索引、哈希索引、聚簇索引、全文(Full-text)索引区别)(唯一索引、最左前缀索引、前缀索引、多列索引)
没有索引时mysql是如何查询到数据的 索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储10 ...
- MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析
文章出处:http://inter12.iteye.com/blog/1430144 MYSQL的全表扫描,主键索引(聚集索引.第一索引),非主键索引(非聚集索引.第二索引),覆盖索引四种不同查询的分 ...
- 温故知新-Mysql索引结构&页&聚集索引&非聚集索
文章目录 摘要 索引 索引概述 索引优势劣势 索引结构 BTREE 结构 B+TREE 结构 页 索引分类 索引语法 索引设计原则 聚触索引 & 非聚触索引 你的鼓励也是我创作的动力 Post ...
- Mysql 索引实现原理. 聚集索引, 非聚集索引
Mysql索引实现: B-tree,B是balance,一般用于数据库的索引.使用B-tree结构可以显著减少定位记录时所经历的中间过程,从而加快存取速度.而B+tree是B-tree的一个变种,My ...
- Mysql索引介绍及常见索引(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别
Mysql索引概念:说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不是越多越好,假如这本书1000页,有500也是目录,它当然效率低,目录是要 ...
- Mysql主键索引、唯一索引、普通索引、全文索引、组合索引的区别
原文:Mysql主键索引.唯一索引.普通索引.全文索引.组合索引的区别 Mysql索引概念: 说说Mysql索引,看到一个很少比如:索引就好比一本书的目录,它会让你更快的找到内容,显然目录(索引)并不 ...
- Mysql 表约束 非空、唯一、主键、自增长、默认、外键约束(基础6)
非空(not null).唯一(unique key).主键(primary key).自增长(auto_increment).默认约束(default) 准备基础环境: mysql> crea ...
- Gap Locks 区间锁
Gap Locks 区间锁 1. 区间锁不能用于语句锁定记录使用一个唯一索引来搜索一个唯一的记录 2.READ COMMITTED 没有区间锁 区间锁是一个锁在一个在index记录间的区间,或者一个l ...
随机推荐
- python中关于正则表达式三
2015年8月14日 11:10 7.2正则表达式操作 正则表达式使用反斜杠字符'\'来暗示一些特殊的形式或者允许特殊的字符使用但是没有调用它们特殊的意思.在字符串常量中的相同目标的字符的python ...
- Mvc分页:为IQueryable定义一个扩展方法,直接反回PagedList<T>结果集
namespace Entity{ public interface IPagedList { /// <summary> /// 记录数 /// </summary> int ...
- Js中的appenChild,insertBefore--createDocumentFragment
平时项目中会有一些流程,或者是评论相关的东西,这些一般只会是在页面初次加载一部分,剩余部分搞一个更多的标签,当点击更多的时候,ajax请求把所有数据加载完(当然这里也有分页的实现方法,本篇不作讨论), ...
- 关于使用navigationController,前后2个视图控制器navigationBar隐藏属性不同,导致右滑手势失效问题的解决办法
###1.问题描述:如A是navigationController的rootViewController,在这个页面navigationBar是显示的(隐藏属性为NO),它push圧栈过来B视图控制器 ...
- Spring中Bean实例的生命周期及其行为
- requirejs源码
require.js /** vim: et:ts=4:sw=4:sts=4 * @license RequireJS 2.1.11 Copyright (c) 2010-2014, The Dojo ...
- Core模块其他常用知识点[OpenCV 笔记14]
Matx 轻量级的Mat,必须在使用前规定好大小,比如一个2x3的float型的Matx,可以声明为Matx23f Vec Vec是Matx的一个派生类,是一个一维的Matx,跟vector很相似.在 ...
- caffe源码阅读(2)-Layer
神经网络是由层组成的,深度神经网络就是层数多了.layer对应神经网络的层.数据以Blob的形式,在不同的layer之间流动.caffe定义的神经网络已protobuf形式定义.例如: layer { ...
- Qt 获得终端执行结果
代码 话不多直接上本人代码 void MainWindow::on_pushButton_3_clicked() { myprocess = new QProcess(this); myprocess ...
- ASP.NET错误页
当页面发生错误的时候,ASP.Net会将错误信息展示出来,这样一来不好看,二来会泄露网站的内部实现信息,给网站带来安全隐患,因此需要定制错误页,发生错误时显示开发人员定制的页面404页面放点广告也是好 ...