Mariadb 自动增量在删除所有记录后得到重置
Mariadb Auto Increment get reset after deleting all records
当我从 mysql table 中删除所有记录时,自动递增计数器重置为 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
,但 TRUNCATE
会,请参阅 https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
实际上,截断查询首先完全删除 table 然后创建一个空白 table... 因为它现在是一个全新的 table,auto_increment id重置为 1..
另一方面,删除查询只会删除 table 而不会创建新的;所以自增id保持不变
由于 innodb 是基于内存的计数器,因此 Innodb 不会保留 auto_increment 值。我已经实施了解决方法来解决我的解决方案:
- 从 Mariadb 中删除记录后,我在其他 table 中保留了最大 ID。
- mariadb 服务启动后,在 postinit 中,我在读取最大 ID 后为 table 设置 auto_increment 计数器。在这里我检查 auto_increment 计数器是否已经大于我拥有的 MAX ID,然后我不更新它。如果小于我更新计数器。
希望对遇到同样问题的其他人有所帮助。
当我从 mysql table 中删除所有记录时,自动递增计数器重置为 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
,但 TRUNCATE
会,请参阅 https://dev.mysql.com/doc/refman/5.7/en/truncate-table.html
实际上,截断查询首先完全删除 table 然后创建一个空白 table... 因为它现在是一个全新的 table,auto_increment id重置为 1..
另一方面,删除查询只会删除 table 而不会创建新的;所以自增id保持不变
由于 innodb 是基于内存的计数器,因此 Innodb 不会保留 auto_increment 值。我已经实施了解决方法来解决我的解决方案:
- 从 Mariadb 中删除记录后,我在其他 table 中保留了最大 ID。
- mariadb 服务启动后,在 postinit 中,我在读取最大 ID 后为 table 设置 auto_increment 计数器。在这里我检查 auto_increment 计数器是否已经大于我拥有的 MAX ID,然后我不更新它。如果小于我更新计数器。
希望对遇到同样问题的其他人有所帮助。