2019独角兽企业重金招聘Python工程师标准>>>

MySQL/InnoDB处理AUTO_INCREMENT(二)

http://dev.mysql.com/doc/refman/5.6/en/innodb-auto-increment-handling.html

Configurable InnoDB Auto-Increment Locking

As described in the previous section, InnoDB uses a special lock called the table-level AUTO-INC lock for inserts into tables with AUTO_INCREMENT columns. This lock is normally held to the end of the statement (not to the end of the transaction), to ensure that auto-increment numbers are assigned in a predictable(可预言的,可预料的) and repeatable(反复的) order for a given sequence of INSERT statements.

In the case of statement-based replication(主从复制,同步), this means that when an SQL statement is replicated(复制) on a slave server, the same values are used for the auto-increment column as on the master server. The result of execution of multiple INSERT statements is deterministic(确定性的), and the slave reproduces the same data as on the master. If auto-increment values generated by multiple INSERT statements were interleaved(交叉存取的,隔行扫描的), the result of two concurrent INSERT statements would be nondeterministic(不确定性的), and could not reliably(依靠依赖) be propagated(传播) to a slave server using statement-based replication.

To make this clear, consider an example that uses this table:

?

1

2

3

4

5

CREATE TABLE t1 (

c1 INT(11) NOT NULL AUTO_INCREMENT,

c2 VARCHAR(10) DEFAULT NULL,

PRIMARY KEY (c1)

) ENGINE=InnoDB;

Suppose that there are two transactions running, each inserting rows into a table with an AUTO_INCREMENT column. One transaction is using an INSERT ... SELECT statement that inserts 1000 rows, and another is using a simple INSERT statement that inserts one row:

Tx1: INSERT INTO t1 (c2) SELECT 1000 rows from another table ...

Tx2: INSERT INTO t1 (c2) VALUES ('xxx');

InnoDB cannot tell in advance(提前) how many rows will be retrieved(取回) from the SELECT in the INSERT statement in Tx1, and it assigns(分配) the auto-increment values one at a time as the statement proceeds(前进). With a table-level lock, held to the end of the statement, only one INSERT statement referring to table t1 can execute at a time, and the generation of auto-increment numbers by different statements is not interleaved(交叉存储的). The auto-increment value generated by the Tx1 INSERT ... SELECT statement will be consecutive(连续的,连贯的), and the (single) auto-increment value used by the INSERT statement in Tx2 will either be smaller or larger than all those used for Tx1, depending on which statement executes first.

As long as the SQL statements execute in the same order when replayed(重放) from the binary log (when using statement-based replication, or in recovery scenarios(场景)), the results will be the same as they were when Tx1 and Tx2 first ran. Thus, table-level locks held until the end of a statement make INSERT statements using auto-increment safe for use with statement-based replication. However, those locks limit concurrency and scalability(可扩展性,可伸缩性) when multiple transactions are executing insert statements at the same time.

In the preceding example, if there were no table-level lock, the value of the auto-increment column used for the INSERT in Tx2 depends on precisely(精确的,严谨的) when the statement executes. If the INSERT of Tx2 executes while the INSERT of Tx1 is running (rather than before it starts or after it completes), the specific auto-increment values assigned by the two INSERT statements are nondeterministic(不确定性的), and may vary(不同) from run to run.

InnoDB can avoid using the table-level AUTO-INC lock for a class of INSERT statements where the number of rows is known in advance, and still preserve(保持,保存) deterministic(确定性的) execution and safety for statement-based replication. Further, if you are not using the binary log to replay SQL statements as part of recovery or replication, you can entirely(完全地) eliminate(排除) use of the table-level AUTO-INC lock for even greater concurrency and performance, at the cost of permitting(允许) gaps in auto-increment numbers assigned by a statement and potentially(潜在的可能的) having the numbers assigned by concurrently executing statements interleaved.

For INSERT statements where the number of rows to be inserted is known at the beginning of processing the statement, InnoDB quickly allocates the required number of auto-increment values without taking any lock, but only if there is no concurrent session already holding the table-level AUTO-INC lock (because that other statement will be allocating auto-increment values one-by-one as it proceeds). More precisely(更准确的说), such an INSERT statement obtains(获得) auto-increment values under the control of a mutex (a light-weight lock) that is not held until the statement completes, but only for the duration of the allocation process.

This new locking scheme enables much greater scalability, but it does introduce some subtle(微妙的) differences in how auto-increment values are assigned compared to the original mechanism. To describe the way auto-increment works in InnoDB, the following discussion defines some terms, and explains how InnoDB behaves using different settings of the innodb_autoinc_lock_mode configuration parameter, which you can set at server startup. Additional considerations are described following the explanation of auto-increment locking behavior.

First, some definitions:

“INSERT-like” statements

All statements that generate new rows in a table, including INSERT, INSERT ... SELECT, REPLACE, REPLACE ... SELECT, and LOAD DATA.

“Simple inserts”

Statements for which the number of rows to be inserted can be determined in advance (when the statement is initially processed). This includes single-row and multiple-row INSERT and REPLACE statements that do not have a nested subquery, but not INSERT ... ON DUPLICATE KEY UPDATE.

“Bulk inserts”

Statements for which the number of rows to be inserted (and the number of required auto-increment values) is not known in advance. This includes INSERT ... SELECT, REPLACE ... SELECT, and LOAD DATA statements, but not plain INSERT. InnoDB will assign new values for the AUTO_INCREMENT column one at a time as each row is processed.

“Mixed-mode inserts”

These are “simple insert” statements that specify the auto-increment value for some (but not all) of the new rows. An example follows, where c1 is an AUTO_INCREMENT column of table t1:

INSERT INTO t1 (c1,c2) VALUES (1,'a'), (NULL,'b'), (5,'c'), (NULL,'d');

Another type of “mixed-mode insert” is INSERT ... ON DUPLICATE KEY UPDATE, which in the worst case is in effect an INSERT followed by a UPDATE, where the allocated value for the AUTO_INCREMENT column may or may not be used during the update phase.

There are three possible settings for the innodb_autoinc_lock_mode parameter:

innodb_autoinc_lock_mode = 0 (“traditional” lock mode)

This lock mode provides the same behavior as before innodb_autoinc_lock_mode existed. For all “INSERT-like” statements, a special table-level AUTO-INC lock is obtained and held to the end of the statement. This assures(确保) that the auto-increment values assigned by any given statement are consecutive(连续的).

This lock mode is provided for:

Backward compatibility.

Performance testing.

Working around issues with “mixed-mode inserts”, due to the possible differences in semantics described later.

innodb_autoinc_lock_mode = 1 (“consecutive” lock mode)

default lock mode

This is the default lock mode. In this mode, “bulk inserts” use the special AUTO-INC table-level lock and hold it until the end of the statement. This applies to all INSERT ... SELECT, REPLACE ... SELECT, and LOAD DATA statements. Only one statement holding the AUTO-INC lock can execute at a time.

With this lock mode, “simple inserts” (only) use a new locking model where a light-weight mutex is used during the allocation of auto-increment values, and no table-level AUTO-INC lock is used, unless an AUTO-INC lock is held by another transaction. If another transaction does hold an AUTO-INC lock, a “simple insert” waits for the AUTO-INC lock, as if it too were a “bulk insert”.

This lock mode ensures that, in the presence of INSERT statements where the number of rows is not known in advance (and where auto-increment numbers are assigned as the statement progresses), all auto-increment values assigned by any “INSERT-like” statement are consecutive, and operations are safe for statement-based replication.

Simply put, the important impact of this lock mode is significantly better scalability. This mode is safe for use with statement-based replication. Further, as with “traditional” lock mode, auto-increment numbers assigned by any given statement are consecutive. In this mode, there is no change in semantics compared to “traditional” mode for any statement that uses auto-increment, with one important exception.

The exception is for “mixed-mode inserts”, where the user provides explicit values for an AUTO_INCREMENT column for some, but not all, rows in a multiple-row “simple insert”. For such inserts, InnoDB will allocate more auto-increment values than the number of rows to be inserted. However, all values automatically assigned are consecutively generated (and thus higher than) the auto-increment value generated by the most recently executed previous statement. “Excess” numbers are lost.

innodb_autoinc_lock_mode = 2 (“interleaved” lock mode)

In this lock mode, no “INSERT-like” statements use the table-level AUTO-INC lock, and multiple statements can execute at the same time. This is the fastest and most scalable lock mode, but it is not safe when using statement-based replication or recovery scenarios when SQL statements are replayed from the binary log.

In this lock mode, auto-increment values are guaranteed(有保证的) to be unique and monotonically increasing across all concurrently executing “INSERT-like” statements. However, because multiple statements can be generating numbers at the same time (that is, allocation of numbers is interleaved across statements), the values generated for the rows inserted by any given statement may not be consecutive.

If the only statements executing are “simple inserts” where the number of rows to be inserted is known ahead of time, there will be no gaps in the numbers generated for a single statement, except for “mixed-mode inserts”. However, when “bulk inserts” are executed, there may be gaps in the auto-increment values assigned by any given statement.

========================END========================

转载于:https://my.oschina.net/airship/blog/608444

MySQL/InnoDB处理AUTO_INCREMENT(二)相关推荐

  1. MySQL InnoDB 锁介绍及不同 SQL 语句分别加什么样的锁

    作者:iceman1952(本文来自作者投稿) 本文中,我们详细介绍MySQL InnoDB存储引擎各种不同类型的锁,以及不同SQL语句分别会加什么样的锁. 阅读提示 1. 本文所参考的MySQL文档 ...

  2. MySQL数据库程序设计(二)

    MySQL数据库程序设计(二) 前言 MySQL编程语言 结构化查询语言SQL MySQL语言组成 数据库定义语言(DDL) 数据操纵语言(DML) 数据控制语言(DCL) MySQL扩展增加的语言要 ...

  3. mysql innodb 的锁机制_Mysql之Innodb锁机制详解

    InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.关于事务我们之前有专题介绍,这里就着重介绍下它的锁机制. 总的来说,InnoDB按照不同的分类共有 ...

  4. Mysql进阶学习(二)条件查询

    Mysql进阶学习(二)条件查询 进阶2:条件查询 一.按条件表达式筛选 案例1:查询工资>12000的员工信息 案例2:查询部门编号不等于90号的员工名和部门编号 二.按逻辑表达式筛选 案例1 ...

  5. mysql innodb源代码调试跟踪分析 何登成_Mysql+Innodb源代码调试跟踪分析+何登成_IT168文库...

    Mysql+Innodb源代码调试跟踪分析+何登成_IT168文库 Mysql Innodb源代码调试跟踪分析 何登成 1 2 3 4 5 6 7 8 9 10 10.1 10.2 10.3 10.4 ...

  6. mysql高级篇(二)mysql索引优化分析

    mysql高级篇笔记 mysql高级篇(一)mysql的安装配置.架构介绍及SQL语句的复习. mysql高级篇(二)mysql索引优化分析. mysql高级篇(三)查询截取分析(慢查询日志).主从复 ...

  7. 从一个死锁看mysql innodb的锁机制

    2019独角兽企业重金招聘Python工程师标准>>> 背景及现象 线上生产环境在某些时候经常性的出现数据库操作死锁,导致业务人员无法进行操作.经过DBA的分析,是某一张表的inse ...

  8. MySQL优化系列(二)--查找优化(1)(非索引设计)

    MySQL优化系列(二)--查找优化(1)(非索引设计) 接下来这篇是查询优化,用户80%的操作基本都在查询,我们有什么理由不去优化他呢??所以这篇博客将会讲解大量的查询优化(索引以及库表结构优化等高 ...

  9. mysql innodb 事务_Mysql InnoDB事务

    事务特点 ACID ATOMICITY:原子性 一个事务必须被视为一个不可分割的最小工作单元,整个事务中的所有操作要么全部提交成功,要么全部失败回滚,对于一个事务来说,不可能只执行其中的一部分操作,这 ...

最新文章

  1. 矩阵乘以其矩阵转置求导-数学
  2. php 位深度,javascript - 流程图获取深度,求各位算法高手帮帮忙
  3. PS5 发布,揭秘真假难辨的虚拟和现实
  4. Oracle加入Eclipse MicroProfile项目
  5. SpringCloud系列——Zuul 动态路由
  6. 【转】基于 Android NDK 的学习之旅-----数据传输(引用数据类型)
  7. 计算机模拟分子设计,计算机模拟分子材料.pdf
  8. Confluence 6 访问你的宏正文(body)
  9. 你敢花一天时间看完本文在简历上添一笔“熟练使用C++编程”吗?
  10. include问题及错误解决
  11. Ubuntu联网问题解决
  12. 高通Hexagon通用计算DSP介绍
  13. EOJ 2706 Fenwick Tree 树状数组找规律
  14. IRIS的镜像配置(2)
  15. Ajax入门-搭建服务器并使用ajax技术向服务器发送一个请求并获得服务器返回的数据
  16. SMS发送WapPush
  17. tup,list,dict,set的创建
  18. 三八节送什么礼物好?最适合送礼的无线蓝牙耳机品牌排名
  19. SQL中in和not in
  20. 行为瘫痪(paralysis)、心流和可能的自我

热门文章

  1. linux shell 脚本攻略学习10--生成任意大小的文件和文本文件的交集与差集详解
  2. 一致性哈希算法及其在分布式系统中的应用
  3. ThinkPHP判断更新是否成功的正确方法
  4. Ext.Ajax.request和formPanel.getForm().submit()两种提交方法的异同:
  5. android控件之TextView(一)
  6. JavaScript 方法传参
  7. visudo 普通用户账户 添加root全新
  8. 详解keepalived配置和使用
  9. 《CSS 禅意花园》读书笔记1
  10. 用完U盘忘记安全退出了,会造成数据丢失吗