When setting or retrieving the auto_increment
of a table in MySQL, starting from MySQL 8, the information_schema
is cached. This means that when you retrieve AUTO_INCREMENT
consecutively, you might get an outdated value. Here are some countermeasures for this issue.
ALTER TABLE my_table_name AUTO_INCREMENT = 1;
SELECT AUTO_INCREMENT
FROM information_schema.tables
WHERE TABLE_NAME = 'my_table_name';
However, there are instances where you might retrieve a cached value. You can address this using one of the following methods:
information_schema
within the sessionSET information_schema_stats_expiry = 0;
information_schema
for the tableANALYZE TABLE my_table_name;
Comments