dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mysql/#36
Script #36
mysql
Monitoringread-onlyMin Version: 5.7

MySQL AUTO_INCREMENT Overflow Risk

Calculates how close auto-increment columns are to their data type limit. Provides early warning because writes stop once the limit is hit.

#auto-increment#overflow#capacity
mysql · sql
SELECT table_schema, table_name, column_name, column_type, auto_increment,
       max_val, ROUND(100 * auto_increment / max_val, 2) AS pct_used
FROM (
  SELECT t.table_schema, t.table_name, c.column_name, c.column_type, t.auto_increment,
    CASE
      WHEN c.column_type LIKE 'tinyint%unsigned'   THEN 255
      WHEN c.column_type LIKE 'tinyint%'           THEN 127
      WHEN c.column_type LIKE 'smallint%unsigned'  THEN 65535
      WHEN c.column_type LIKE 'smallint%'          THEN 32767
      WHEN c.column_type LIKE 'mediumint%unsigned' THEN 16777215
      WHEN c.column_type LIKE 'mediumint%'         THEN 8388607
      WHEN c.column_type LIKE 'int%unsigned'       THEN 4294967295
      WHEN c.column_type LIKE 'int%'               THEN 2147483647
      WHEN c.column_type LIKE 'bigint%unsigned'    THEN 18446744073709551615
      ELSE 9223372036854775807
    END AS max_val
  FROM information_schema.tables t
  JOIN information_schema.columns c
    ON c.table_schema = t.table_schema AND c.table_name = t.table_name AND c.extra LIKE '%auto_increment%'
  WHERE t.auto_increment IS NOT NULL
    AND t.table_schema NOT IN ('mysql','sys','information_schema','performance_schema')
) x
ORDER BY pct_used DESC
LIMIT 20;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 5.7).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.