In a table in mysql db (version: 5.7.37) I have an auto-increment field (id column).
The table is a huge one, but sometime we delete records (so id of 1 million is not 1 millions records) - still a huge one after the deletion.
Sometime the id reaches the maximum integer value (2147483647).
I won't change to bigint (it is complicated - there is limitation in code also).
I want to do it cyclic - when it reaches 2147483647, the next value will be 1, so I want to do:
alter table mytable AUTO_INCREMENT=1;
That may seems work with no any exception (for a fraction of second), but when I do:
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES t
WHERE TABLE_SCHEMA = 'myschema'
AND TABLE_NAME = 'mytable';
I see that the AUTO_INCREMENT
for above is still of value: 2147483647 and not of value 1.
If I do something like:
SET FOREIGN_KEY_CHECKS = 0;
set unique_checks=0;
alter table mytable
change column `id` `id` int(11) NOT NULL;
set unique_checks=1;
SET FOREIGN_KEY_CHECKS = 1;
SET FOREIGN_KEY_CHECKS = 0;
set unique_checks=0;
alter table mytable
change column `id` `id` int(11) NOT NULL AUTO_INCREMENT;
SET FOREIGN_KEY_CHECKS = 1;
SET unique_checks=1;
alter table paranoid_zero AUTO_INCREMENT=1;
The above take too long time (few minutes), and still AUTO_INCREMENT value checked by query:
SELECT `AUTO_INCREMENT`
FROM INFORMATION_SCHEMA.TABLES t
WHERE TABLE_SCHEMA = 'myschema'
AND TABLE_NAME = 'mytable';
is equal to 2147483647.
How can I update auto increment value (fast) in a proper way with mysql query.
Thanks.