自增值溢出问题

mysql5.6.25

mysql> select * from t2;
+------------+------+------+------+
| id | c1 | c2 | c3 |
+------------+------+------+------+
| 4 | 4 | 4 | NULL |
| 100 | 100 | 100 | NULL |
| 103 | 103 | 103 | 103 |
| 104 | 104 | 104 | 104 |
| 2147483647 | 101 | 101 | NULL |
+------------+------+------+------+
5 rows in set (0.00 sec)

mysql> show create table t2;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| t2 | CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c1` int(11) DEFAULT NULL,
`c2` int(11) DEFAULT NULL,
`c3` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_c1` (`c1`),
KEY `idx_c3` (`c3`)
) ENGINE=InnoDB AUTO_INCREMENT=2147483647 DEFAULT CHARSET=utf8mb4 |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

update t2 set id=4147483647,c1=101,c2=101 where id=101;  修改主键会导致自增值溢出。AUTO_INCREMENT不变,但是重启或者使用pt-online-schema-change执行DDL操作,自增值变为最大值AUTO_INCREMENT=2147483647

mysql直接执行alter,自增值不会变。

如何处理:

1、数据量小,只是一条语句导致,update这条语句,然后修改自增值。

  必须要先把值修改后才能修改自增值

  update t2 set id=101 where c1=101 and c2=101;

  alter table t2 AUTO_INCREMENT=105;

2、如果是大表写满21亿,表很大,只能重建表,否则主从会延时。

  alter table `hm3_realtime_flash_view_client` modify id bigint(20) auto_increment;

  这里需要注意,需要加上auto_increment,否则就不会自增了

 

posted on 2021-05-14 18:01  星期六男爵  阅读(51)  评论(0编辑  收藏  举报

导航