- 浏览: 202957 次
- 性别:
- 来自: 重庆
文章分类
最新评论
以前内容主要是对官方文档中的意译,并加入了一些自己的理解(新版本还是很给力的)。侯哥原创,欢迎交流。
详细参照官方文档:http://dev.mysql.com/doc/refman/5.1/en/innodb-auto-increment-handling.html#innodb-auto-increment-configurable
在mysql5.1.22之前,mysql的“INSERT-like”语句(包INSERT, INSERT...SELECT, REPLACE,REPLACE...SELECT, and LOAD DATA)会在执行整个语句的过程中使用一个AUTO-INC锁将表锁住,直到整个语句结束(而不是事务结束)。因此在使用INSERT...SELECT、INSERT...values(...),values(...)时,LOAD DATA等耗费时间较长的操作时,会将整个表锁住,而阻塞其他的“INSERT-like”、Update等语句,推荐使用程序将这些语句分成多条语句,一一插入,减少单一时间的锁表时间,荣嬷嬷的superdump就不错。
mysql5.1.22之后mysql进行了改进,引入了参数 innodb_autoinc_lock_mode,通过这个参数控制mysql的锁表逻辑,在介绍这个之前先引入几个术语,方便说明 innodb_autoinc_lock_mode。
1.“INSERT-like”:
INSERT, INSERT ... SELECT, REPLACE, REPLACE ... SELECT, and LOAD DATA, INSERT ... VALUES(),VALUES()
2.“Simple inserts”
就是通过分析insert语句可以确定插入数量的insert语句, INSERT, INSERT ... VALUES(),VALUES()
3.“Bulk inserts”
就是通过分析insert语句不能确定插入数量的insert语句, INSERT ... SELECT, REPLACE ... SELECT, LOAD DATA
4.“Mixed-mode inserts”
下面两种,不确定是否需要分配auto_increment id
INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d');
INSERT ... ON DUPLICATE KEY UPDATE
一、innodb_autoinc_lock_mode = 0 (“traditional” lock mode)
这种方式就和mysql5.1.22以前一样,为了向后兼容而保留了这种模式,如同前面介绍的一样,这种方式的特点就是“表级锁定”,并发性较差
二、innodb_autoinc_lock_mode = 1 (“consecutive” lock mode)
这种方式是新版本中的默认方式,推荐使用,并发性相对较高,特点是“consecutive”,即保证同一条insert语句中新插入的auto_increment id都是连续的。
这种模式下:
“Simple inserts”:直接通过分析语句,获得要插入的数量,然后一次性分配足够的auto_increment id,只会将整个分配的过程锁住。
“Bulk inserts”:因为不能确定插入的数量,因此使用和以前的模式相同的表级锁定。
“Mixed-mode inserts”:直接分析语句,获得最坏情况下需要插入的数量,然后一次性分配足够的auto_increment id,只会将整个分配的过程锁住。需要注意的是,这种方式下,会分配过多的id,而导致”浪费“。比如INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d');会一次性的分配5个id,而不管用户是否指定了部分id;INSERT ... ON DUPLICATE KEY UPDATE一次性分配,而不管将来插入过程中是否会因为duplicate key而仅仅执行update操作。
注意:当master mysql版本<5.1.22,slave mysql版本>=5.1.22时,slave需要将innodb_autoinc_lock_mode设置为0,因为默认的innodb_autoinc_lock_mode为1,对于INSERT ... ON DUPLICATE KEY UPDATE和INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d');的执行结果不同,现实环境一般会使用INSERT ... ON DUPLICATE KEY UPDATE。
三、innodb_autoinc_lock_mode = 2 (“interleaved” lock mode)
这种模式是来一个分配一个,而不会锁表,只会锁住分配id的过程,和innodb_autoinc_lock_mode = 1的区别在于,不会预分配多个,这种方式并发性最高。但是在replication中当binlog_format为statement-based时(简称SBR statement-based replication)存在问题,因为是来一个分配一个,这样当并发执行时,“Bulk inserts”在分配的时会同时向其他的INSERT分配,会出现主从不一致(从库执行结果和主库执行结果不一样),因为binlog只会记录开始的insert id。
测试SBR,执行begin;insert values(),();insert values(),();commit;会在binlog中每条insert values(),();前增加SET INSERT_ID=18/*!*/;。
但是row-based replication RBR时不会存在问题。
另外RBR的主要缺点是日志数量在包括语句中包含大量的update delete(update多条语句,delete多条语句)时,日志会比SBR大很多;假如实际语句中这样语句不是很多的时候(现实中存在很多这样的情况),推荐使用RBR配合innodb_autoinc_lock_mode,不过话说回来,现实生产中“Bulk inserts”本来就很少,因此innodb_autoinc_lock_mode = 1应该是够用了。
下面是官方文档举得几个例子,这里就不翻译的
For example, assume c1 is an AUTO_INCREMENT column of table t1, and that the most recent automatically generated sequence number is 100. Consider the following “mixed-mode insert” statement:
INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d');
With innodb_autoinc_lock_mode set to 0 (“traditional”), the four new rows will be:
+-----+------+
| c1 | c2 |
+-----+------+
| 1 | a |
| 101 | b |
| 5 | c |
| 102 | d |
+-----+------+
The next available auto-increment value will be 103 because the auto-increment values are allocated one at a time, not all at once at the beginning of statement execution. This result is true whether or not there are concurrently executing “INSERT-like” statements (of any type).
With innodb_autoinc_lock_mode set to 1 (“consecutive”), the four new rows will also be:
+-----+------+
| c1 | c2 |
+-----+------+
| 1 | a |
| 101 | b |
| 5 | c |
| 102 | d |
+-----+------+
However, in this case, the next available auto-increment value will be 105, not 103 because four auto-increment values are allocated at the time the statement is processed, but only two are used. This result is true whether or not there are concurrently executing “INSERT-like” statements (of any type).
With innodb_autoinc_lock_mode set to mode 2 (“interleaved”), the four new rows will be:
+-----+------+
| c1 | c2 |
+-----+------+
| 1 | a |
| x | b |
| 5 | c |
| y | d |
+-----+------+
The values of x and y will be unique and larger than any previously generated rows. However, the specific values of x and y will depend on the number of auto-increment values generated by concurrently executing statements.
Finally, consider the following statement, issued when the most-recently generated sequence number was the value 4:
INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d');
With any innodb_autoinc_lock_mode setting, this statement will generate a duplicate-key error 23000 (Can't write; duplicate key in table) because 5 will be allocated for the row (NULL, 'b') and insertion of the row (5, 'c') will fail.
发表评论
-
分页,MySQL的SQL_CALC_FOUND_ROWS
2012-12-17 16:44 1346FOUND_ROWS() 函数 ,可以在调用包含LIMIT的 ... -
使用mysql的last_insert_id() 产生自增的id
2012-12-17 15:48 1084刚才查了下last_insert_id()相关资料,找到几篇不 ... -
innodb的记录锁、gap锁、next-key锁
2012-12-17 13:45 1388相关文章见: http://dev.mysql ... -
MySQL Innodb表死锁情况分析与归纳(转载)
2012-12-14 16:22 1940案例描述 在定时脚本运行过程中,发现当 ... -
MySQL连接超时
2012-12-13 20:41 1113在负载较重的MySQL服务器上,有时你偶尔会看到一些 ... -
mysql优化索引 —— Using filesort
2012-12-11 12:13 819用Explain分析SQL语句的时候,经常发现有的语句在 ... -
IN条件结果顺序问题
2012-11-23 14:42 663项目中需要记录用户的浏览历史,我的意见是前端直接存cook ... -
mysql那点事(行锁+not null + varchar)
2012-10-12 15:08 1651一。not null vs DEAFUL value ... -
mysql 与unicode
2012-08-29 15:36 1681UTF8字符集(转换Unicode表示)是存储Unico ... -
深入Mysql字符集设置
2012-08-29 14:46 55805 Jan 08 深入Mysql字符集设置 ... -
关于set names
2012-08-29 14:37 809<?php$conn=mysql_connect( ... -
MYSQL索引优化和in or替换为union all
2012-08-22 17:16 4042一个文章库,里面有两个表:category和article。c ... -
mysql读写分离(PHP类)
2012-08-18 18:08 3607自己实现了php的读写 ... -
mysql分表的3种方法
2012-08-02 14:39 829当一张的数据达到几百万时,你查询一次所花的时间会变多,如 ... -
MySql BLOB类型
2012-08-02 13:42 708MySql的Bolb四种类型 MySQL中,BLOB是一个二进 ... -
MySQL 查询优化技巧
2012-07-18 20:26 754瓶颈 众所周知,几乎所有大型项目的最终效率瓶颈,都来自 ... -
mysql多表联合查询
2012-07-18 19:16 1637我在工作中天天研究zen cart的程序,那个叫人痛苦,最近比 ...
相关推荐
MySQL AUTO_INCREMENT 和主键等解释
MySQL 序列 AUTO_INCREMENT详解及实例代码 MySQL序列是一组整数:1, 2, 3, …,由于一张数据表只能有一个字段自增主键, 如果你想实现其他字段也实现自动增加,就可以使用MySQL序列来实现。 本章我们将介绍如何使用...
主要介绍了MySQL查询和修改auto_increment的方法,实例分析了select查询auto_increment及ALTER修改auto_increment的技巧,需要的朋友可以参考下
主要介绍了python mysql自增字段AUTO_INCREMENT值的修改方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
04_auto_increment.avi MYSQL应用 MYSQL视频 MYSQL教程,讲解中连接上一集03集
重置 MySQL 自增列 AUTO_INCREMENT 初时值 注意, 使用以下任意方法都会将现有数据删除. 方法一: delete from tb1; ALTER TABLE tbl AUTO_INCREMENT = 100; (好处, 可以设置 AUTO_INCREMENT 为任意值开始) 提示:...
当清空一个表的时候,重新插入数据,发现auto_increment属性的字段计数不是从1开始的时候,可以使用以下命令
本篇文章是对mysql中的auto_increment的问题进行了详细的分析介绍,需要的朋友参考下
innodb_autoinc_lock_mode这个参数控制着在向有auto_increment 列的表插入数据时,相关锁的行为; 通过对它的设置可以达到性能与安全(主从的数据一致性)的平衡 【0】我们先对insert做一下分类 首先insert大致上可以...
mysql中的自增auto_increment功能相信每位phper都用过,也都知道如何设置字段为自增字段,但并不是所有phper都知道auto_increment的起始值和递增量是如何设置的!本文就为大家分享一下mysql字段自增功能的具体查看及...
像MySQL一样,将Mongoid id字段更改为Integer的宝石。 MongoDB文档中的想法: 注意! 这个宝石已经帮助超过了4年,并产生了超过一百万行,这是非常...gem 'mongoid_auto_increment_id' , "0.6.1" # Mongoid 3.1.x gem
) ENGINE=MyISAM AUTO_INCREMENT=35 DEFAULT CHARSET=utf8; -- ---------------------------- -- Table structure for city -- 创建城市表的结构 -- ---------------------------- CREATE TABLE `city` ( `id` int...
a%b表⽰以a开头b结尾的任意长度的字符串 _(下横线) a_b表⽰以a开头b结尾的长度为3的任意字符, PRIMARY KEY UNIQUE NOT NULL AUTO_INCREMENT DEFAULT default_value PRIMARY KEY 与UNIQUE 数据库操作: show ...
主键自增长(AUTO_INCREMENT)
消息聊天系统MySQL表设计_消息系统数据库设计 1、新建platform_info表,此表存储调⽤消息平台⽅应⽤信息 CREATE TABLE IF NOT EXISTS `platform_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `platform_code` ...