Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resetting AUTO_INCREMENT is taking a long time in MySQL

ALTER TABLE tablename AUTO_INCREMENT = 10000000

This query is taking long time to update. Why? I need to optimize this query.

like image 576
user320343 Avatar asked Apr 21 '10 09:04

user320343


People also ask

How do I reset my Autoincrement ID?

Reset the auto increment fieldALTER TABLE `table` AUTO_INCREMENT = number; Replacing 'number' with the result of the previous command plus one and replacing table with the table name. If you deleted all the rows in the table, then you could run the alter table command and reset it to 0.

Does truncate table reset auto increment?

The TRUNCATE TABLE statement in MySQL completely deletes the table's data without removing a table's structure and always resets the auto-increment column value to zero.

What is MySQL Auto_increment?

MySQL AUTO INCREMENT Field Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

How do I remove Autoincrement?

To disable or remove the auto increment from a column in MySQL Table, you can simply Modify the column with same data type but without auto_increment clause.


2 Answers

ALTER TABLE causes a rebuild of the entire table - if your table contains many rows,this can take ages.

If you just need to bump up the value of the auto_increment value, the quickest way is to insert a dummy row (and then delete that roow if need be). This will only take a fraction of a second, whereas ALTER TABLE can take days for a large table.

For example, suppose I have a table with an auto_increment ID column and other columns col1, col2...:

insert into autoinc_table set ID = 10000000;
delete from autoinc_table where ID = 10000000;
like image 105
Martin Avatar answered Nov 15 '22 20:11

Martin


user and admin data must be distinguished not by id, but by another field. If you would treat an id as an abstract identifier with no other meanings it will save you a lot of time and resources, trust me.

like image 23
Your Common Sense Avatar answered Nov 15 '22 18:11

Your Common Sense