我们的产品表的主键即将耗尽。我们不希望这种情况再次发生。因此,我们需要一种机制来根据插入率在主键用完时发出警报,以便我们可以缓解这种情况。
我编写了一个 Jenkins 作业,它运行并执行信息模式查询,并从 MySQL 数据库中提取索引长度、表行和 auto_increment。但是我们需要跟踪先前的读数,因为我们想要将其与当前的读数进行比较,以便预测 key 何时会耗尽。
SELECT
INDEX_LENGTH, TABLE_NAME, AUTO_INCREMENT, TABLE_ROWS
FROM
information_schema.TABLES
ORDER BY
index_length DESC;
我找不到任何与 Mysql 表中的变化率相关的内容来预测主键的使用率。如果有人想到一种好的替代方法,我也愿意使用任何好的替代方法,但请记住,我必须通过 cron 作业或 Jenkins 作业运行一个查询,该查询应该告诉哪些表将在今年年底耗尽。
最佳答案
找到相关查询来获取自动增量用完的百分比。请参阅this链接和评论部分。
SELECT table_schema, table_name, column_name, column_type, auto_increment,max_int,ROUND(auto_increment/max_int*100,2) AS “USED %” FROM
(SELECT table_schema, table_name, column_name, column_type, auto_increment,
pow(2, case data_type
when ‘tinyint’ then 7
when ‘smallint’ then 15
when ‘mediumint’ then 23
when ‘int’ then 31
when ‘bigint’ then 63
end+(column_type like ‘% unsigned’))-1 as max_int
FROM information_schema.tables t
JOIN information_schema.columns c USING (table_schema,table_name)
WHERE t.table_schema not in (‘mysql’,’information_schema’,’performance_schema’) and t.table_type = ‘base table’
and c.extra = ‘auto_increment’ AND t.auto_increment IS NOT NULL order by auto_increment desc limit 10) TMP;
关于mysql - 基于插入率预测MySQL主键耗尽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56386124/