Quantcast
Channel: Planet MySQL
Viewing all articles
Browse latest Browse all 18766

A better way to re-set the Auto Increment value within MySQL

$
0
0
I work for a real estate website and we are running out of property numbers for some of our listings. Many moons ago logic in the code stated that listing types. For example a Commercial listing or Residential listing ID start with a different digit. Residential listings start with a '1' and Commercial listings start with a '5' for example. 


Putting aside that logic for a second, we are in a situation whereby our Commercial listings are now in the range of 599000. Meaning we only have a few new listings before, according to the 'business' here, the world ends. (As the numbers will rollover to 600000 and the hard coded numeric logic in the code will blow up and cause a fire in the datacenters we run).


So the thoughts are to re-set the Auto Increment value appending a few zeros to keep the logic of the listing starting with a '5'. The change SQL is something like this (and we're InnoDB everywhere):

 ALTER TABLE Commercial AUTO_INCREMENT = 500000000;

Now, that works, but its really bad. Why? Well MySQL will actually re-create the entire table to just re-set the Auto Increment value. The table is around 10G and on our infrastructure with our MySQL and configured values takes around 5 minutes. When you demand 100% uptime, having something blocking for 5 minutes is unacceptable.  We use MySQL Master / Master Active Standby replication to perform online schema changes though this is really unnecessary.


Keep in mind we're InnoDB here and we have foreign keys set up and unique indexes on this table. 




Here is the better way to perform the change that is not impacting and very quick is the following SQL on the master server only. (Note PropertyNumber is the Auto Increment primary key column)









/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
begin;
insert into Commercial (PropertyNumber) values(500000000);
delete from Commercial where PropertyNumber=500000000;                                          
commit;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;



For those watching will have noticed that I affectively appended the mysqldump output to relax to sql mode and disable foreign key and unique checks.


PlanetMySQL Voting: Vote UP / Vote DOWN

Viewing all articles
Browse latest Browse all 18766

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>