MySQL int max value
What is MySQL int max value?
MySQL int max value is 2147483647 when signed and of 4294967295 when unsigned. Although rare, there are situations where auto increment columns does reach MySQL int 11 max value specially when signed.
Comparison of MySQL int data types and there max values
MySQL provide five int data types,
- TINYINT - 1 byte - max 127 signed and 255 unsigned
- SMALLINT - 2 bytes - max 32767 signed and 65535 unsigned
- MEDIUMINT - 3 bytes - max 8388607 signed and 16777215 unsigned
- INT (11) - 4 bytes - max 2147483647 signed and 4294967295 unsigned
- BIGINT - 8 bytes - max 263 - 1 and 264 - 1 unsigned
First three int types can be used when the highest value is predictable, else it is always safe to go for MySQL int 11 or bigint. Bigint does provide huge limit advantage over int 11 , compromising the fixed storage space usage of 2 times (8 bytes compared to 4 bytes) which can cause access overheads as well.
Script to identify auto increment columns reaching MySQL max int value
mysql -u root -p -e "`mysql -s -u root -p information_schema -e "select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, EXTRA from COLUMNS
where EXTRA='auto_increment'" | awk '{print "select max("$3"), \x27"$2"\x27\ as table_name, \x27"$1"\x27\ as db_name from "$1"."$2";"}'`"
Above script will provide the current max value used by int auto increment columns. Script needs the password to be entered twice.
If any of the int columns are reaching towards the max value, best option would be rename the current table and let the data insertion to continue on new table, but in this case the auto increment column value will be reset. (starting from 1). Another option would be to alter the int 11 column to bigint. But this can take time, cause locking and performance overhead as well.
Comments
Post a Comment