为什么mysql innodb table-auto increment列会跳1个以上,即使变量为';自动增量增量';是否设置为1?

为什么mysql innodb table-auto increment列会跳1个以上,即使变量为';自动增量增量';是否设置为1?,mysql,sql,Mysql,Sql,为什么mysql innodb table-auto-increment列跳跃超过1个数字,即使“auto\u increment\u increment”等变量设置为1 注意:表上未使用任何更新或删除查询。回滚将插入行的事务将使自动递增计数器跳过值。同时插入忽略和插入。。。在重复时,键将增长自动增量计数器。插入查询时,自动增量值也会增加。诀窍在于,即使没有添加实数行,它也可以递增。有几个案例,我将使用小演示表来解释: CREATE TABLE test ( id INT UNSIGNED

为什么mysql innodb table-auto-increment列跳跃超过1个数字,即使“auto\u increment\u increment”等变量设置为1


注意:表上未使用任何更新或删除查询。

回滚将插入行的事务将使自动递增计数器跳过值。

同时插入忽略和插入。。。在重复时,键将增长自动增量计数器。

插入查询时,自动增量值也会增加。诀窍在于,即使没有添加实数行,它也可以递增。有几个案例,我将使用小演示表来解释:

CREATE TABLE test (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY (name)
) ENGINE = INNODB;

root@localhost/test> Show table status like 'test'\G
*************************** 1. row ***************************
           Name: test
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 0
 Avg_row_length: 0
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: 1 <-- Next auto_increment field value
    Create_time: 2016-06-06 14:39:48
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)
案例2:插入。。。关于重复密钥更新

root@localhost/test> INSERT INTO test (name) VALUES ("One") ON DUPLICATE KEY UPDATE name=VALUES(name);
Query OK, 0 rows affected (0.00 sec)

root@localhost/test> SELECT * FROM test ORDER BY id;
+----+------+
| id | name |
+----+------+
|  1 | One  |
+----+------+
1 row in set (0.00 sec)

root@localhost/test> INSERT INTO test (name) VALUES ("One") ON DUPLICATE KEY UPDATE name=VALUES(name);
Query OK, 0 rows affected (0.00 sec)

root@localhost/test> Show table status like 'test'\G
*************************** 1. row ***************************
           Name: test
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 1
 Avg_row_length: 16384
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: 5
    Create_time: 2016-06-06 14:39:48
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)
案例3:插入错误

root@localhost/test> INSERT INTO test (name) VALUES ("One");
ERROR 1062 (23000): Duplicate entry 'One' for key 'name'

root@localhost/test> Show table status like 'test'\G
*************************** 1. row ***************************
           Name: test
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 1
 Avg_row_length: 16384
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: 6
    Create_time: 2016-06-06 14:39:48
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

您可以在本文或MySQL文档中阅读更多关于出现这些漏洞的原因:

这里是提到的自动增量类型。将它们更改为其中一个会有帮助,还是会导致性能下降?据我所知,“传统”锁定模式(innodb_autoinc_lock_mode=0)有助于避免id序列中出现漏洞,但这种模式可能会导致一些性能问题,因为它会在每个INSERT语句上获取表级锁,并阻止并发插入。所有其他锁定模式都可以创建孔。
root@localhost/test> INSERT INTO test (name) VALUES ("One");
ERROR 1062 (23000): Duplicate entry 'One' for key 'name'

root@localhost/test> Show table status like 'test'\G
*************************** 1. row ***************************
           Name: test
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 1
 Avg_row_length: 16384
    Data_length: 16384
Max_data_length: 0
   Index_length: 16384
      Data_free: 0
 Auto_increment: 6
    Create_time: 2016-06-06 14:39:48
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)