欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL系统变量auto_increment_increment与auto_increment_offset学习总结

程序员文章站 2022-06-26 20:29:42
在MySQL中,系统变量auto_increment_increment与auto_increment_offset是与自增列相关的两个参数变量。在官方文档中,将其划分为Replication Master Options and Variables 。具体参考官方文档17.1.6.2 Replic... ......

mysql中,系统变量auto_increment_incrementauto_increment_offset是与自增列相关的两个参数变量。在官方文档中,将其划分为replication master options and variables 。具体参考官方文档17.1.6.2 replication master options and variables

 

    auto_increment_offset     auto_increment列值的起点,也就是初始值。取值范围是1 .. 65535

   auto_increment_increment   控制列中的值的增量值,也就是步长。其默认值是1,取值范围是1 .. 65535

 

系统变量auto_increment_incrementauto_increment_offset 都有会话级别和全局级别两个值(注意:设置全局系统变量时,对当前连接或已存在的连接不生效,只对新的连接有效)。它们的取值范围为1 ..65535, 如果设置的时候超过这个范围的话,会是什么情况? 如下所示:

 

mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
4 rows in set (0.00 sec)
 
mysql> set session auto_increment_increment=0;
query ok, 0 rows affected, 1 warning (0.01 sec)
 
mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql>  set session auto_increment_increment=65536;
query ok, 0 rows affected, 1 warning (0.00 sec)
 
mysql>  show variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> 
mysql> set session auto_increment_offset=-1;
query ok, 0 rows affected, 1 warning (0.04 sec)
 
mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 65535 |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

 

 

如上所示,如果系统变量设置其值小于10或负数),mysql会默认设置为1 ,如果大于65535mysql会默认设置为65535. 也就是说这两个系统变量的取值范围为 1 ... 65535.

 

 

系统变量auto_increment_increment修改后,自增列的变化规律

 

 

如果我们想知道系统变量auto_increment_increment变化后,自增列的变化规律,最简单、有效的方式就是实验测试,如下所示:

 

mysql> drop table if exists test;
query ok, 0 rows affected, 1 warning (0.00 sec)
 
mysql> create table test(id int auto_increment primary key, name varchar(32));
query ok, 0 rows affected (0.03 sec)
 
mysql> insert into test(name) value('kerry1');
query ok, 1 row affected (0.00 sec)
 
mysql> insert into test(name) value('kerry2');
query ok, 1 row affected (0.01 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
+----+--------+
2 rows in set (0.01 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> set session auto_increment_increment=3;
query ok, 0 rows affected (0.00 sec)
 
mysql> insert into test(name) value('kerry3');
query ok, 1 row affected (0.01 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  4 | kerry3 |
+----+--------+
3 rows in set (0.00 sec)
 
mysql> 

 

 

mysql>  set session auto_increment_increment=1;
query ok, 0 rows affected (0.00 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> truncate table test;
query ok, 0 rows affected (0.05 sec)
 
mysql> insert into test(name) value('kerry1');
query ok, 1 row affected (0.00 sec)
 
mysql> insert into test(name) value('kerry2');
query ok, 1 row affected (0.00 sec)
 
mysql> insert into test(name) value('kerry3');
query ok, 1 row affected (0.00 sec)
 
mysql> set session auto_increment_increment=3;
query ok, 0 rows affected (0.00 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 3     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> insert into test(name) value('kerry4');
query ok, 1 row affected (0.03 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  3 | kerry3 |
|  4 | kerry4 |
+----+--------+
4 rows in set (0.00 sec)
 
mysql> 

 

为什么修改auto_increment_increment=3后,新增的数据id=4呢? 其实这个是因为自增列的计算逻辑为 auto_increment_offset + n × auto_increment_increment  n表示第几次,从1开始计算(auto_increment_offset=1),并且计算值必须大于自增列的最大值(max(id)),

 

对于第一种情况: id  12   因为最大值为2,而自增列的取值为1+1*3= 4, 所以自增列取值为4

对于第二种情况: id= 123. 因为最大值为3,而自增列的取值为1+1*3= 4, 所以自增列取值为4

 

官方文档的描述如下所示:

 

if either of these variables is changed, and then new rows inserted into a table containing an auto_increment column, the results may seem counterintuitive because the series of auto_increment values is calculated without regard to any values already present in the column, and the next value inserted is the least value in the series that is greater than the maximum existing value in the auto_increment column. the series is calculated like this:

auto_increment_offset + n × auto_increment_increment

where n is a positive integer value in the series [1, 2, 3, ...].

 

注意:如果表没有值,自增列第一个值为auto_increment_offset

 

 

如下所示对于这种情况,新增列的值为1+2*3=7 ,因为已有14这样的取值。  

 

mysql> set session auto_increment_increment=1;
query ok, 0 rows affected (0.00 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> truncate table test;
query ok, 0 rows affected (0.02 sec)
 
mysql> insert into test(name) value('kerry1');
query ok, 1 row affected (0.01 sec)
 
mysql> insert into test(name) value('kerry2');
query ok, 1 row affected (0.02 sec)
 
mysql> insert into test(name) value('kerry3');
query ok, 1 row affected (0.00 sec)
 
mysql>  insert into test(name) value('kerry4');
query ok, 1 row affected (0.00 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  3 | kerry3 |
|  4 | kerry4 |
+----+--------+
4 rows in set (0.00 sec)
 
mysql> set session auto_increment_increment=3;
query ok, 0 rows affected (0.00 sec)
 
mysql> insert into test(name) value('kerry5');
query ok, 1 row affected (0.03 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  3 | kerry3 |
|  4 | kerry4 |
|  7 | kerry5 |
+----+--------+
5 rows in set (0.00 sec)
 

 

 

auto_increment_offset修改后,自增列的变化规律

 

 

换个测试方法,如果修改系统变量auto_increment_offset的话,自增列会怎么变化呢?且看下面实验

 

mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  3 | kerry3 |
|  4 | kerry4 |
|  7 | kerry5 |
+----+--------+
5 rows in set (0.00 sec)
 
mysql> show  variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 3     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)
 
mysql> set auto_increment_offset=2;
query ok, 0 rows affected (0.00 sec)
 
mysql> insert into test(name) value('kerry6');
query ok, 1 row affected (0.01 sec)
 
mysql> select * from test;
+----+--------+
| id | name   |
+----+--------+
|  1 | kerry1 |
|  2 | kerry2 |
|  3 | kerry3 |
|  4 | kerry4 |
|  7 | kerry5 |
| 11 | kerry6 |
+----+--------+
6 rows in set (0.00 sec)

 

其实这个按下面这个公式来计算的:max(id)+(new_offset-old_offset)+increment ,也就是说变化auto_increment_offset后的第一个值为max(id)+(new_offset-old_offset)+increment之后再按步长递增。

 

max(id)   =7

new_offset =2

old_offset =1

increment =3

 

 

 

另外,需要注意的是: 如果设置auto_increment_offset的值远远大于auto_increment_increment mysql会忽略系统变量auto_increment_offset的值。

when the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored.(如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值会被忽略)

 

 

 

 

 

  另外,关于系统变量auto_increment_increment,在一些mysql版本中存在一个bugbug 15851528 : duplicate key error on auto-inc pk with mixed auto_increment_increment clients, 下面演示一下这个bug。更多详细信息,参考下面摘抄的官方资料:

 

1: 如下所示,首先准备好测速所需的相关表和存储过程。

 

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.23-log |
+------------+
1 row in set (0.00 sec)
 
mysql> show variables like 'innodb_autoinc_lock_mode';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| innodb_autoinc_lock_mode | 1     |
+--------------------------+-------+
1 row in set (0.00 sec)
 
mysql> drop table if exists test_items;
query ok, 0 rows affected (0.09 sec)
 
mysql> create table test_items (id int(11) auto_increment primary key);
query ok, 0 rows affected (0.01 sec)
 
mysql> delimiter &&
mysql> drop procedure if exists prc_insert;
    ->  
    -> create procedure prc_insert(in  cnt int)
    -> begin
    -> declare i int;
    -> set i=1;
    -> while i < cnt do
    ->     insert into `test_items` values (default);
    ->     
    ->     set i = i+1;
    ->  
    -> end while;
    -> end &&
query ok, 0 rows affected (0.00 sec)
 
query ok, 0 rows affected (0.00 sec)
mysql> delimiter ;

 

 

2:在连接a中,设置系统变量:auto_increment_increment=1

 

 

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|              16 |
+-----------------+
1 row in set (0.00 sec)
 
mysql> set auto_increment_increment=1;
query ok, 0 rows affected (0.00 sec)
 
mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 1     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.01 sec)

 

 

3:在连接b中,设置系统变量:auto_increment_increment=2

 

mysql> select connection_id();
+-----------------+
| connection_id() |
+-----------------+
|              14 |
+-----------------+
1 row in set (0.00 sec)
 
mysql> set auto_increment_increment=2;
query ok, 0 rows affected (0.00 sec)
 
mysql> show variables like 'auto_increment%';
+--------------------------+-------+
| variable_name            | value |
+--------------------------+-------+
| auto_increment_increment | 2     |
| auto_increment_offset    | 1     |
+--------------------------+-------+
2 rows in set (0.00 sec)

 

 

4:在两个连接中同时执行存储过程call prc_insert(100000); 构造并发插入的场景,就能构造出这个bug。 如下所示:

 

 

 

其实,其它场景也能构造出这样的错误

 

 

 

注意:在官方文档中:error duplicate entry ' ' for key 'primary' on auto-increment primary key when changing auto_increment_increment (文档 id 2498911.1),提示这个bug在下面版本中已经fix

 

this bug is fixed in 5.5.65, 5.6.45, 5.7.27, 8.0.17 releases.

for older versions than the above versions, workaround is to use the same auto_increment_increment for all sessions and do not try to set different values in the sessions running in parallel.

 

 

bug 15851528 : duplicate key error on auto-inc pk with mixed auto_increment_increment clients

 

 

 

 

 

产品线

oracle database products

系列

mysql

区域

mysql server

产品

8478 - mysql server

 

 

 

 

hdr: 15851528 n/a eninnodb 5.5.28 prodid-8478 portid-289 14049391

abstract: duplicate key error on auto-inc pk with mixed auto_increment_increment clients

 

*** 11/08/12 12:19 pm ***

* originally logged as bug 67526 in the mysql bug system

  by: rob linewewaver on 08-nov-2012 19:51:14

  - see http://bugs.mysql.com/bug.php?id=67526

  - reporter email is: rlineweaver@rosettastone.com

  - bug was reported against version(s): 5.5.28, 5.5.29, 5.7.0

 

description:

on mysql 5.5 with innodb_autoinc_lock_mode=1, with connected clients that

have different values for auto_increment_increment (some with 1 and some with

2) doing concurrent inserts onto an innodb table with an auto-increment

primary key, we see duplicate key errors like the following:

 

duplicate entry '21211440' for key 'primary'

 

no value was specified for the auto-increment column in the insert queries. 

i expect to never see a duplicate key error for an auto-increment pk in this

case.

 

scenarios tested:

  1. mysql 5.1.66 + innodb_autoinc_lock_mode=1:  no problem

  2. mysql 5.5.28 + innodb_autoinc_lock_mode=1: duplicate key error reliably

produced within a few seconds

     2a.  with both clients using auto_increment_increment = 1:  no problem

     2b.  with both clients using auto_increment_increment = 2:  no problem

  3. mysql 5.5.28 + innodb_autoinc_lock_mode=0: no problem

 

how to repeat:

 

1. set up a mysql 5.5.28 server with innodb_autoinc_lock_mode=1

2. create a simple innodb table with an auto-increment pk:

     create table test_items (id int(11) auto_increment primary key);

3. connect client 1 and set auto_increment_increment to 1:

     set auto_increment_increment=1;

4. connect client 2 and set auto_increment_increment to 2:

     set auto_increment_increment=2;

5. perform repeated inserts from both clients at the same time:

     insert into `test_items` values (default);

6. watch for "duplicate entry ... for key 'primary'" errors

 

suggested fix:

(none)

 

 

* original bug verified in the mysql bug system by: sveta smirnova.

 

 

 

 

*** 11/08/12 12:19 pm ***

* original entry logged in the mysql bug system by: sveta smirnova

  on 08-nov-2012 21:18:40

thank you for the report.

 

verified as described.

 

test case for mtr:

 

--source include/have_innodb.inc

 

create table test_items (id int(11) auto_increment primary key)

engine=innodb;

 

set auto_increment_increment=1;

 

delimiter |;

create procedure bug67526(iterations int) begin while iterations > 0 do set

iterations = iterations -1; insert into `test_items` values (default); end

while; end;|

delimiter ;|

 

connect(con1, localhost, root,,);

connection con1;

 

set auto_increment_increment=2;

 

connection default;

--send call bug67526(10000);

 

 

connection con1;

call bug67526(10000);

 

connection default;

--reap

 

select max(id) from test_items;

 

 

*** 11/08/12 12:20 pm *** (chg: sta->11)

*** 11/08/12 01:42 pm ***

* original entry logged in the mysql bug system by: rob lineweaver

  on 08-nov-2012 22:01:22

fixing typo in tag

 

 

*** 11/21/12 12:41 pm ***

*** 03/01/13 02:27 pm ***

*** 03/01/13 02:27 pm ***

*** 05/13/13 12:10 am ***

*** 05/13/13 12:10 am ***

*** 11/19/13 04:42 am ***

* original entry logged in the mysql bug system by: sam butler

  on 19-nov-2013 13:08:42

we're seeing the same bug reproduced on 5.5.33 (linux) with two replicating

servers that have the same auto_increment_increment value, but a different

offset. a single client is connected and trying to insert into the table that

throws the duplicate pk error on an auto inc field.

 

*** 02/11/14 09:44 pm ***

*** 08/20/15 06:42 am ***

* original entry logged in the mysql bug system by: rolf martin-hoster

  on 20-aug-2015 14:49:51

as noticed by sam this is occurring with same auto_increment_increment  and

is similar if not the same as http://bugs.mysql.com/bug.php?id=76872

 

*** 01/20/18 05:03 pm ***

*** 01/20/18 05:03 pm ***

*** 12/28/18 05:59 am ***

*** 12/28/18 06:03 am ***

*** 02/28/19 03:42 am ***

*** 02/28/19 09:11 am ***

*** 02/28/19 09:20 am ***

*** 02/28/19 09:20 am ***

*** 02/28/19 08:52 pm ***

*** 03/11/19 02:25 am ***

i was able to reproduce with

 

create table t(a serial,name varchar(200),primary key(a))engine=innodb;

 

set session auto_increment_increment=2; 

insert into t(name) values("vijay"),( "rohan"),("deepak"),("parakh");

 

--connect(con1,localhost,root,,)

 

--connection con1

 

set auto_increment_increment=2 ;

insert into t(name) values("rahul");

 

--connection default

 

set auto_increment_increment=1;

insert into t(name) values("vijay"),( "rohan"),("deepak"),("parakh");

 

--connection con1

 

set auto_increment_increment=1;

insert into t(name) values("rahul");

 

will post my analysis further 

*** 04/08/19 02:42 am ***

* original entry logged in the mysql bug system by: pavel katiushyn

  on 08-apr-2019 08:47:31

i had the same issue on 5.7.25.

 

*** 05/01/19 08:35 pm ***

*** 05/01/19 08:35 pm *** (chg: base bug-> null -> 14049391 [other])

*** 05/01/19 10:22 pm ***

pushed into repo mysql branch mysql-5.5 5.5.65

(hash:87d8fb242134b8d1d97734b2ad6023d36d814067

committer:rahul.m.malik@oracle.com)

(merge vers:5.5.65)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients problem: clients running different values

for auto_increment_increment and doing concurrent inserts leads

*** 05/01/19 10:22 pm ***

*** 05/01/19 10:22 pm *** (chg: sta->80)

pushed into repo mysql branch mysql-5.6 5.6.45

(hash:87d8fb242134b8d1d97734b2ad6023d36d814067

committer:rahul.m.malik@oracle.com)

(merge vers:5.6.45)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients problem: clients running different values

for auto_increment_increment and doing concurrent inserts leads

*** 05/01/19 10:22 pm ***

pushed into repo mysql branch mysql-5.7 5.7.27

(hash:87d8fb242134b8d1d97734b2ad6023d36d814067

committer:rahul.m.malik@oracle.com)

(merge vers:5.7.27)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients problem: clients running different values

for auto_increment_increment and doing concurrent inserts leads

*** 05/01/19 10:22 pm ***

pushed into repo mysql branch mysql-8.0 8.0.17

(hash:87d8fb242134b8d1d97734b2ad6023d36d814067

committer:rahul.m.malik@oracle.com)

(merge vers:8.0.17)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients problem: clients running different values

for auto_increment_increment and doing concurrent inserts leads

*** 05/01/19 10:22 pm ***

pushed into repo mysql branch mysql-trunk 8.0.18-tr

(hash:87d8fb242134b8d1d97734b2ad6023d36d814067

committer:rahul.m.malik@oracle.com)

(merge vers:8.0.18-tr)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients problem: clients running different values

for auto_increment_increment and doing concurrent inserts leads

*** 05/01/19 10:22 pm ***

*** 05/03/19 02:12 am ***

pushed into repo mysql branch mysql-5.7 5.7.27

(hash:847254dd0d79ef6b1bed429883ec47e72447ffd2

committer:rahul.m.malik@oracle.com)

(merge vers:5.7.27)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients

pushed into repo mysql branch mysql-8.0 8.0.17

(hash:847254dd0d79ef6b1bed429883ec47e72447ffd2

committer:rahul.m.malik@oracle.com)

(merge vers:8.0.17)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients

pushed into repo mysql branch mysql-trunk 8.0.18-tr

(hash:847254dd0d79ef6b1bed429883ec47e72447ffd2

committer:rahul.m.malik@oracle.com)

(merge vers:8.0.18-tr)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients

*** 05/07/19 02:13 pm ***

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients

 

problem:

clients running different values for auto_increment_increment

and doing concurrent inserts leads to "duplicate key error" in one of them.

   

analysis:

when autoinc_increment value is reduced in a session,

innodb uses last autoinc_increment value

to recalculate the autoinc value.

in case, some other session has inserted a value

with different autoinc_increment, innodb recalculate

autoinc values based on current session previous autoinc_increment

instead of considering the autoinc_increment used for last insert

across all session

   

fix:

revert 1d4494d93c2f as it causing the current bug.

*** 05/07/19 02:37 pm ***

fixed as of the upcoming 5.5.65, 5.6.45, 5.7.27, 8.0.17 releases, and here's

the changelog entry:

 

client sessions using different auto_increment_increment values while

performing concurrent insert operations could cause a duplicate key error.

*** 05/07/19 02:37 pm *** (chg: sta->74)

*** 05/29/19 07:12 am ***

pushed into repo mysql branch mysql-5.5-cluster-7.2 5.5.65-ndb-7.2.38

(hash:87d8fb242134b8d1d97734b2ad6023d36d814067

committer:rahul.m.malik@oracle.com)

(merge vers:7.2.38)

bug#15851528 duplicate key error on auto-inc pk with mixed

auto_increment_increment clients problem: clients running different values

for auto_increment_increment and doing concurrent inserts leads

*** 05/29/19 07:12 am ***

*** 05/29/19 07:12 am *** (chg: sta->80)

*** 05/29/19 10:24 am ***

*** 05/29/19 10:24 am *** (chg: sta->74)

 

 

参考资料

 

 

https://dev.mysql.com/doc/refman/8.0/en/replication-options-master.html

bug 15851528 : duplicate key error on auto-inc pk with mixed auto_increment_increment clients