Forum Moderators: phranque
Method 1:
mysql> delete from Inventory;
Query OK, 69561 rows affected [b](0.04 sec)[/b]mysql> LOAD DATA INFILE 'tfout.txt' INTO TABLE Inventory;
Query OK, 69561 rows affected [b](50 min 52.42 sec)[/b]
Records: 69561 Deleted: 0 Skipped: 0 Warnings: 0
Total elapsed time = 50 minutes 52.46 seconds. Ouch.
Method 2:
mysql> delete from Inventory;
Query OK, 69561 rows affected [b](0.05 sec)[/b]mysql> ALTER TABLE Inventory DISABLE KEYS;
Query OK, 0 rows affected [b](0.01 sec)[/b]mysql> LOAD DATA INFILE 'tfout.txt' INTO TABLE Inventory;
Query OK, 69561 rows affected [b](2.29 sec)[/b]
Records: 69561 Deleted: 0 Skipped: 0 Warnings: 0mysql> ALTER TABLE Inventory ENABLE KEYS;
Query OK, 0 rows affected [b](44.85 sec)[/b]
Total elapsed time = 47.2 seconds. Much better! :)
Time saved using ALTER TABLE .. DISABLE KEYS & ALTER TABLE .. ENABLE KEYS = 50 minutes 5.26 seconds
Time it took me to figure this out = hours.
Hope this helps someone someday.
I have several tables using an auto_increment field as primary unique key. I use that number to identify unique items in the database.
As items get deleted, the incrementer keeps going, without filling in the deleted numbers.
Is there a way to configure the db to allow it to reclaim the missing numbers?
I can envision the field becoming too big to use within a few years. One use of the field is as the barcode number for the inventory item. Another is a unique entry number on a contest form.
Is there a way to configure the db to allow it to reclaim the missing numbers?
An INSERT to a TABLE with an AUTO_INCREMENT column is going to generate an error once you reach the limit for the numeric value; it won't wrap and start over (obviously that would be bad).
There is a User Contributed note in the MySQL Manual AUTO_INCREMENT [mysql.com] page that reads as follows:
For those that are looking to "reset" the auto_increment, say on a list that has had a few deletions and you want to renumber everything, you can do the following.DROP the field you are auto_incrementing.
ALTER the table to ADD the field again with the same attributes.You will notice that all existing rows are renumbered and the next auto_increment number will be equal to the row count plus 1.
(Keep in mind that DROPping that column will remove all existing data, so if you have exterior resources that rely on that data, or the numbers that are already there, you may break the link. Also, as with any major structure change, it's a good idea to backup your table BEFORE you make the change.)
**CAUTION**
Anybody considering this -- if you are using an InnoDB table type -- MySQL warns "If you delete all rows in the table with DELETE FROM table_name (without a WHERE) in AUTOCOMMIT mode, the sequence starts over for all table types except InnoDB. See section 7.5.12.5 How an AUTO_INCREMENT Column Works in InnoDB." Therefore I don't know what will happen if you DROP the column altogether.
I don't like this idea anyway. Why not just ALTER your column to Column Type INTEGER? This would give you 4,294,967,295 INSERTS before you would have to get concerned. And if that doesn't satisfy you, go with BIGINT. That would probably be overkill though.
Just some thoughts.