关于MySQL的AUTO_INCREMENT列出现不连续的原因,本文列出了几个比较常见的场景。MySQL 5.1.42, InnoDB Plugin 1.0.6, innodb_autoinc_lock_mode = 1
Scenario 1
CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(10)) ENGINE=InnoDB;
START TRANSACTION;
INSERT INTO test VALUES(NULL, '1');
ROLLBACK;
INSERT INTO test VALUES(NULL, '2');
SELECT * FROM test;
+----+------+
| id | name |
+----+------+
| 2 | 2 |
+----+------+
Scenario 2
CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(10)) ENGINE=InnoDB;
INSERT INTO test VALUES(NULL, '1');
INSERT INTO test VALUES(3, '3');
SELECT * FROM test;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 3 | 3 |
+----+------+
Scenario 3
CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(10)) ENGINE=InnoDB;
ALTER TABLE test AUTO_INCREMENT=100;
INSERT INTO test VALUES(NULL, '100'), (99, '99'), (NULL, '101');
INSERT INTO test VALUES(NULL, '103');
SELECT * FROM test;
+-----+------+
| id | name |
+-----+------+
| 99 | 99 |
| 100 | 100 |
| 101 | 101 |
| 103 | 103 |
+-----+------+
Scenario 4
CREATE TABLE test(id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(10)) ENGINE=InnoDB;
CREATE TABLE t(id INT) ENGINE=InnoDB;
INSERT INTO t VALUES(1), (2), (3), (4), (5);
INSERT INTO test SELECT NULL, id FROM t;
INSERT INTO test VALUES(NULL, '8');
SELECT * FROM test;
+----+------+
| id | name |
+----+------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 8 | 8 |
+----+------+
点评:在这种场景下,InnoDB无法在执行Insert语句之前知道确切的插入记录数,因此会使用表级的AUTO_INC锁(该锁比较特殊,并不像通常的锁那样,在事务结束时释放,而是在该语句执行完毕后释放)。对于AUTO_INCREMENT值,目前InnoDB会采取预分配的策略,即首先分配1,如果用尽则double,如果用尽再double,即1,2,4,8...。需要注意的是,如果innodb_autoinc_lock_mode =2,那么InnoDB不会使用AUTO_INC锁。
Scenario 5
CREATE TABLE test (a INT PRIMARY KEY AUTO_INCREMENT, b INT, c INT, UNIQUE INDEX(b)) ENGINE=InnoDB;
INSERT INTO test values(NULL, 1, 1), (NULL, 2, 2);
INSERT INTO test (a,b,c) VALUES (NULL,2,3) ON DUPLICATE KEY UPDATE c=c+1;
INSERT INTO test values(NULL, 4, 4);
SELECT * FROM test;
+---+------+------+
| a | b | c |
+---+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 4 | 4 | 4 |
+---+------+------+
点评:在INSERT语句执行之前,InnoDB无法知道数据最终是被插入还是更新,因此可能会导致InnoDB预分配的AUTO_INCREMENT值最终没有被使用。
@see also:
http://dinglin.iteye.com/blog/1279536
分享到:
相关推荐
关于GAPS技术的,一部分的开发规范和步骤,很适合用的
Python-gaps 是一个开源项目,专门设计用于解决图像拼图问题。它利用了遗传算法(Genetic Algorithm)和OpenCV库的强大功能,为拼图游戏提供了一种智能的求解策略。遗传算法是一种受到生物进化启发的搜索算法,能够...
标题《跨越叶序研究中的数学-植物学鸿沟—大对称序叶序图形形成的机理》及描述,向我们介绍了一篇首发论文的主要研究内容,其核心在于探讨叶序研究领域中数学与植物学之间的鸿沟问题,并尝试揭示叶序中更高对称序...
使用这种方式的前提必须有最近的备份集或者知道出现误操作起始的binlog 位点或者GTID,利用备份集恢复到中间的机器上,然后利用MySQL的slave 特性 START SLAVE [SQL_THREAD] UNTIL MASTER_LOG_FILE = ‘log_n
i3-gaps-deb是一个专为Debian和Ubuntu用户设计的工具,它的主要目标是帮助用户轻松地创建和安装i3-gaps这个扩展版的窗口管理器。i3-gaps是一个流行的窗口管理器i3的增强版本,它添加了用户界面的间隙,使得多窗口...
本文档聚焦于信息安全视角下的数据安全问题,特别是服务器领域中一些潜在的安全漏洞(Gaps)以及红队(Red Team)视角下的应对策略。文档内容涉及移动安全、安全架构、应急响应、解决方案和安全防护等多个方面,并对...
The off-plane propagation of electromagnetic (EM) waves in a two-dimensional (2D) graphite photonic crystal structure was studied using transfer matrix method. Transmission spectra calculations ...
Filling Execution Gaps: How Executives and Project Managers Turn Corporate Strategy into Successful Projects By 作者: Todd C. Williams ISBN-10 书号: 1501515209 ISBN-13 书号: 9781501515200 出版日期: ...
这个指标在柱形图上显示了差距。
matlab导入excel代码utl_identify_gaps_in_magazine_subscriptons 确定订阅者之间有3个月或3个月以上的时间间隔的订阅者。 关键字:sas sql join合并大数据分析宏oracle teradata mysql sas社区stackoverflow ...
The remaining chapters focus exclusively on EDM’s emerging role in helping to advance educational research—from identifying at-risk students and closing socioeconomic gaps in achievement to aiding ...
A photonic crystal fiber has been filled with a cholesteric liquid crystal. A temperature sensitive photonic band gap effect was observed, which was especially pronounced around the liquid crystal ...
用于x11-wm / i3 GAPS选件的FreeBSD端口 测试端口 您可以通过克隆此存储库并在配置中启用GAPS选项来测试端口。 git clone https://github.com/tony/i3-gaps-freebsd.git cd i3-gaps-freebsd # one way make WITH=...
Leon Starr and Andrew Mangogna, "Models to Code: With No Mysterious Gaps" English | ISBN: 1484222164 | 2017 | 305 pages | PDF | 18 MB Learn how to translate an executable model of your application ...
Models to Code With No Mysterious Gaps 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
there is an expanding science base from diverse disciplines that can support science communicators in making these determinations....gaps in knowledge about how to communicate effectively about science...
Models to Code With No Mysterious Gaps 英文无水印原版pdf pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看...
标题“Gender-gaps-in-graduates-from-majors”揭示了一个关于高等教育领域中性别差异的研究主题,重点关注大学毕业生的专业选择和毕业比例。这个项目聚焦于分析男性和女性在17个不同专业领域的毕业生数量和比例,...
安装克隆回购: $ git clone https://github.com/nemanja-m/gaps.git$ cd gaps 安装要求: $ pip install -r requirements.txt$ sudo apt-get install python-tk 以可编辑模式安装项目: $ pip install -e ....