I've been assigned to create a bash script that checks when an AUTO_INCREMENT
column is getting close to its limit.
I figured the best way to do this is to get the "Current Auto Increment Value" and compare to "maximum value supported column's data type" (say 255 for tinyint
).
We have about 2Tb of data in our databases, I dont want to query the INFORMATION SCHEMA
table to get the AUTO_INCREMENT
because it will take too long.
Therefore I can't use (SHOW TABLE STATUS LIKE 'table_name')
nor (SELECT AUTO_INCREMENT FROM information_schema.tables)
because both query INFORMATION SCHEMA
table.
I've been told there might be a workaround to get the current AUTO_INCREMENT without hitting the information schema table but I can't see how. Any ideas?
This is how I get the data type
DATA_TYPE="`$MYSQLCONNECT -D $DATABASE --batch -N -e "SELECT DATA_TYPE FROM
INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$DATABASE' AND TABLE_NAME='$TABLE' AND
EXTRA='auto_increment'"`"
This is how I get the max value that column can handle:
MAX_VALUE="`$MYSQLCONNECT -D $DATABASE --batch -N -e "SELECT ( CASE '$DATA_TYPE' WHEN 'tinyint'
THEN 255 WHEN 'smallint' THEN 65535 WHEN 'mediumint' THEN 16777215 WHEN 'int' THEN 4294967295
WHEN 'bigint' THEN 18446744073709551615 END >> IF(LOCATE('unsigned', COLUMN_TYPE) > 0, 0, 1) )
as MAX_VALUE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '$DATABASE' AND TABLE_NAME
= '$TABLE' AND EXTRA='auto_increment'"`"