【问题标题】:Mariadb Auto Increment get reset after deleting all recordsMariadb Auto Increment 在删除所有记录后重置
【发布时间】:2017-10-24 08:19:06
【问题描述】:

当我从我的 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 |                |         |
+---------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------+----------+----------------+---------+

【问题讨论】:

  • DELETE 查询不会重置 auto_increment id 而 TRUNCATE 会
  • 发布您用来删除所有迁移行的查询
  • 你是对的。但是如果你重新启动 mysql,那么它会将 auto_increment 计数器重置回 1。下面是我正在使用的删除查询:Delete from test where id in (1,2,3,4,5)
  • 不,不会!?!?

标签: mysql mariadb auto-increment


【解决方案1】:

由于 innodb 是基于内存的计数器,因此 Innodb 不会保留 auto_increment 值。我已经实施了一种解决方法来解决我的解决方案:

  1. 从 Mariadb 中删除记录后,我将最大 ID 保留在其他表中。
  2. mariadb 服务启动后,在 postinit 中,我在读取最大 ID 后设置表的 auto_increment 计数器。在这里,我检查 auto_increment 计数器是否已经大于我拥有的 MAX ID,然后我没有更新它。如果小于我更新计数器。

希望它能帮助面临同样问题的其他人。

【讨论】:

    【解决方案2】:

    其实truncate query是先把表全部删除然后再创建一个空白表...由于现在是一个全新的表,所以auto_increment id被重置为1..

    另一方面,删除查询只会删除表而不创建新表;所以自增 id 保持不变。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 你是对的。但是如果你重启 mysql,那么它会将 auto_increment 计数器重置为 1。
      • 因为 auto_increment 是基于内存的计数器。 Innodb 引擎执行 MAX(id) 以获取 auto_increment。参考:dev.mysql.com/doc/refman/5.7/en/…
      猜你喜欢
      • 2017-05-03
      • 2020-08-22
      • 2011-08-18
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多