计算auto_increment与其最大值的接近程度的简便方法是什么?

Dav*_*d M 4 mysql auto-increment

所以昨天我们有一个表,它有一个auto_increment PK,用于达到最大值的smallint.我们不得不在紧急情况下改变桌子,这绝对不是我们喜欢滚动的方式.

有没有一种简单的方法来报告我们使用的每个auto_increment字段与其最大值的接近程度?我能想到的最好的方法是做一个SHOW CREATE TABLE语句,解析自动递增列的大小,然后将其与表的AUTO_INCREMENT值进行比较.

另一方面,假设模式不经常更改,我应该存储有关列的最大值的信息并获取当前的AUTO_INCREMENT SHOW TABLE STATUS吗?

Ike*_*ker 7

你的问题对我来说似乎很合理.您应该能够从information_schema获取每个表的当前自动增量值.我不认为各种int类型的最大值在MySQL中可用作常量,但Roland Bouman演示了一种在MySQL中生成它们的简单方法:

在SQL中如何获取整数的最大值?

如果将该数据放入表中,则可以编写单个SQL查询以获取所有表的当前自动增量状态,这样您就可以看到用尽的值.

这是一个快速而肮脏的例子,可以帮助您入门:

create temporary table max_int_values
(
int_type varchar(10) not null,
extra varchar(8) not null default '',
max_value bigint unsigned not null,
primary key (int_type,max_value),
key int_type (int_type),
key max_value (max_value)
);

insert into max_int_values(int_type,extra,max_value) values ('tinyint','',~0 >> 57);
insert into max_int_values(int_type,extra,max_value) values ('tinyint','unsigned',~0 >> 56);
insert into max_int_values(int_type,extra,max_value) values ('smallint','',~0 >> 49);
insert into max_int_values(int_type,extra,max_value) values ('smallint','unsigned',~0 >> 48);
insert into max_int_values(int_type,extra,max_value) values ('mediumint','',~0 >> 41);
insert into max_int_values(int_type,extra,max_value) values ('mediumint','unsigned',~0 >> 40);
insert into max_int_values(int_type,extra,max_value) values ('int','',~0 >> 33);
insert into max_int_values(int_type,extra,max_value) values ('int','unsigned',~0 >> 32);
insert into max_int_values(int_type,extra,max_value) values ('bigint','',~0 >> 1);
insert into max_int_values(int_type,extra,max_value) values ('bigint','unsigned',~0);

select t.table_Schema,t.table_name,c.column_name,c.column_type,
  t.auto_increment,m.max_value,
  round((t.auto_increment/m.max_value)*100,2) as pct_of_values_used,
  m.max_value - t.auto_increment as values_left
from information_schema.tables t
  inner join information_schema.columns c 
    on c.table_Schema = t.table_Schema and c.table_name = t.table_name
  inner join max_int_values m 
    on m.int_type = substr(c.column_type,1,length(m.int_type)) 
    and ((m.extra like '%unsigned') = (c.column_type like '%unsigned'))
where c.extra = 'auto_increment'
order by pct_of_values_used;
Run Code Online (Sandbox Code Playgroud)


jos*_*und 5

在openark中,这是一个用于检查自动增量容量的查询:

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'
;
Run Code Online (Sandbox Code Playgroud)

当然,您可以添加ORDER BY AUTO_INCREMENT_RATIO DESC或,LIMIT以轻松挑选出最接近其极限值的那些。