删除所有记录后,Mariadb Auto Increment会重置

时间:2017-05-24 06:38:39

标签: mysql mariadb auto-increment

当我从mysql表中删除所有记录时,自动增量计数器将重置为0.我的应用程序逻辑是将记录从MySQL迁移到其他分析引擎,然后从MySQL中删除迁移的记录。分析引擎使用与MySQL相同的主键管理记录,因此当MySQL重置计数器时,我的分析引擎中出现重复键错误。我希望Mysql保留auto_increment,即使我从数据库中删除了所有记录。

alter table test auto_increment = 100;

我可以执行上面的查询以在删除我的记录后保留auto_increment但是如果我重新启动MySQL,这个计数器将再次重置为默认值0。

我知道这是MySQL的预期行为。但我正在寻找一种解决方法来克服它。

我使用的是mariadb而不是mysql。请在重启前后查看auto_increment计数器。

MariaDB [MY_DB]> SHOW TABLE STATUS FROM `MY_DB` WHERE `name` LIKE 'test' ;
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| Name    | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| test | InnoDB |      10 | Compact    |    6 |           2730 |       16384 |               0 |            0 |         0 |              7 | 2017-05-22 23:38:06 | NULL        | NULL       | utf8_bin  |     NULL |                |         |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
1 row in set (0.00 sec)

MariaDB [MY_DB]> select max(id) from test;
+---------------+
| max(id) |
+---------------+
|             6 |
+---------------+
1 row in set (0.00 sec)

MariaDB [MY_DB]> delete from test where id in (1,2,3,4,5,6);
Query OK, 6 rows affected (0.00 sec)

MariaDB [MY_DB]> SHOW TABLE STATUS FROM `MY_DB` WHERE `name` LIKE 'test' ;
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| Name    | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| test | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |            0 |         0 |              7 | 2017-05-22 23:38:06 | NULL        | NULL       | utf8_bin  |     NULL |                |         |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
1 row in set (0.00 sec)

MariaDB [MY_DB]> exit
Bye
root@atd-3000:~# systemctl restart mariadb
root@atd-3000:~# mysql -p***** MY_DB;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 57
Server version: 10.1.17-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [MY_DB]> SHOW TABLE STATUS FROM `MY_DB` WHERE `name` LIKE 'test' ;
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| Name    | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time         | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+
| test | InnoDB |      10 | Compact    |    0 |              0 |       16384 |               0 |            0 |         0 |              1 | 2017-05-22 23:38:06 | NULL        | NULL       | utf8_bin  |     NULL |                |         |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+

3 个答案:

答案 0 :(得分:1)

实际上,截断查询首先完全删除表格&然后创建一个空白表...因为它现在是一个全新的表,auto_increment id重置为1 ..

另一方面,drop查询只删除表&没有创造新的;所以自动增量ID保持不变。

答案 1 :(得分:1)

由于innodb是基于记忆的计数器,Innodb不会保留auto_increment值。我已经实施了一个解决方案来解决我的解决方案:

  1. 从Mariadb删除记录后,我保留了其他表中的最大ID。
  2. mariadb服务启动后,在postinit中,我在读取最大ID后设置了表的auto_increment计数器。在这里,我检查auto_increment计数器是否已经大于我的MAX ID,然后我没有更新它。如果它比我更新计数器少。
  3. 希望它能帮助其他面临同样问题的人。

答案 2 :(得分:0)

DELETE不会重置AUTO_INCREMENT,但TRUNCATE会重置https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html