MySQL auto_increment capacity calculation
Have you ever wondered if your tables in a MySQL database may be nearing capacity on INT AUTO_INCREMENT columns?First things first: If your application design allows, set the AUTO_INCREMENT column to be UNSIGNED. It effectively doubles it’s capacity.
The following query on INFORMATION_SCHEMA provides a convenient way to verify your usage. Only top 100 columns are listed but you can change the query to suit your requirements.
- IS_UNSIGNED:
- 1 = column is UNSIGNED
- 0 = column is SIGNED
- MAX_VALUE: maximum integer value that can be stored in the column.
- AUTO_INCREMENT: current AUTO_INCREMENT value for the column.
- AUTO_INCREMENT_RATIO: value in the range [0..1], where 1 means “100% full”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, COLUMN_TYPE, IF( LOCATE('unsigned', COLUMN_TYPE) > 0, 1, 0 ) AS IS_UNSIGNED, ( 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, AUTO_INCREMENT, AUTO_INCREMENT / ( 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 AUTO_INCREMENT_RATIO FROM INFORMATION_SCHEMA.COLUMNS INNER JOIN INFORMATION_SCHEMA.TABLES USING (TABLE_SCHEMA, TABLE_NAME) WHERE TABLE_SCHEMA NOT IN ('mysql', 'INFORMATION_SCHEMA', 'performance_schema') AND EXTRA='auto_increment' ORDER BY AUTO_INCREMENT_RATIO DESC LIMIT 100 ; |
Recent Posts
- Enable http/2 in Apache 16th Jan 2021
- MySQL Shell 8.x 16th Jan 2021
- vi/vim tricks 30th Dec 2020
- SELinux on CentOS 29th Dec 2020
- ssh-copy-id (password less ssh login) 26th Dec 2020
- MySQL auto_increment capacity calculation 26th Dec 2020
- Using UUID as a Primary Key 24th Dec 2020
- Keycloak and InnoDB Cluster 29th Nov 2020
- How to create Column in MySQL only if one does not exist? 19th Mar 2019
- Inject empty transactions in MySQL 5.6 GTID based Replication 25th Dec 2018
- Adding a new disk to CentOS 26th Aug 2018
- mysqldump extended inserts 26th Jun 2018
- CentOS Logical Volume Manager (LVM) 26th Oct 2017
- How to prepare SD Card for Raspberry Pi on a MAC 26th Jun 2017
- Convert .dmg file on a MAC to .iso 26th Apr 2017
Categories
- CentOS (4)
- CentOS 8 (2)
- InnoDB cluster (1)
- mysql (7)
- Raspberry Pi (1)
- ssh (1)
- Technical (3)
Tag cloud
Archives
- January 2021 (2)
- December 2020 (5)
- November 2020 (1)
- March 2019 (1)
- December 2018 (1)
- August 2018 (1)
- June 2018 (1)
- October 2017 (1)
- June 2017 (1)
- April 2017 (1)
- February 2016 (1)