`

MySQL自增ID最大值被使用

阅读更多

问题:评论数据表hotel_info_comments自增ID列达到最大值2147483647,但是并不是每一个ID都被使用了

 

解决办法:清理数据表的跳跃自增ID,保持ID连贯。

 

解决步骤:

 

1.   创建评论临时表

create table `hotel_info_comments_tmp` (

  `id` int(11) not null auto_increment comment '自增id',

  `hotel_id` varchar(50) not null comment '酒店id',

  `hotel_type` varchar(10) not null comment '酒店品牌id',

  `out_id` varchar(20) not null default '' comment '来源评论id',

  `src` varchar(10) not null comment '评论来源,kuaijie,ctrip,elong……',

  `room_id` varchar(10) default null comment '房型id',

  `order_id` varchar(50) default '' comment '订单id',

  `user_id` varchar(50) default '' comment '用户id',

  `parent_id` int(11) default null comment '父评论id',

  `content` varchar(2000) not null comment '评论内容',

  `side` int(1) not null default '1' comment '正面评价:1;负面评价:0',

  `aver_score` float default null comment '平均评分',

  `loca_score` float default null comment '位置评分0-5之间',

  `faci_score` float default null comment '设备评分0-5之间',

  `serv_score` float default null comment '服务评分0-5之间',

  `hygi_score` float default null comment '卫生评分0-5之间',

  `voted` int(1) not null default '0' comment '点赞',

  `is_reply` int(1) not null default '0' comment '是否可回复,1:可回复,0:不可回复',

  `status` int(1) not null default '1' comment '是否有效,1:有效,0:无效',

  `user_name` varchar(50) default null comment '用户名',

  `user_type` varchar(10) default 'customer' comment '用户类别,customer, shopper',

  `extra_info` varchar(800) not null comment '附加信息',

  `create_time` datetime not null comment '创建时间',

  `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间',

  primary key (`id`),

  unique key `outsrcid` (`src`,`out_id`),

  key `idx_hotel_idtype` (`hotel_id`,`hotel_type`,`order_id`),

  key `idx_user_order` (`user_id`,`order_id`),

  key `idx_user_hotel` (`hotel_id`,`hotel_type`,`user_id`),

  key `pid_index` (`parent_id`)

) engine=innodb auto_increment=2949076 default charset=utf8 comment='评论信息表';

 

2.   将线上表的数据插入临时表(自增ID列不转移)

insert into hotel_info_comments_tmp(`hotel_id`,`hotel_type`,`out_id`,`src`,`room_id`,`order_id`,`user_id`,`parent_id`,`content`,`side`,`aver_score`,`loca_score`,`faci_score`,`serv_score`,`hygi_score`,`voted`,`is_reply`,`status`,`user_name`,`user_type`,`extra_info`,`create_time`,`update_time`) select `hotel_id`,`hotel_type`,`out_id`,`src`,`room_id`,`order_id`,`user_id`,`parent_id`,`content`,`side`,`aver_score`,`loca_score`,`faci_score`,`serv_score`,`hygi_score`,`voted`,`is_reply`,`status`,`user_name`,`user_type`,`extra_info`,`create_time`,`update_time` from hotel_info_comments;

 

3.   检验线上表的的数据和临时表是否一致

select count(*) from hotel_info_comments;

select count(*) from hotel_info_comments_tmp;

 

4.   删除线上表

drop table hotel_info_comments;

 

5.   重命名临时表为线上表

rename table hotel_info_comments_tmp to hotel_info_comments;

 

*注意事项:尝试过alter table hotel_info_comments drop column id. alter table hotel_info_comments add column id. 表的数据200多万,这样非常慢;当前使用方法不适合更新维护频繁的表。

 

sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install MySQL-python

分享到:
评论

相关推荐

    mysql自增id超大问题的排查与解决

    小A正在balabala写代码呢,DBA小B突然发来了一条消息,“快看看你的用户特定信息表T,里面的主键,也就是自增id,都到16亿了,这才多久,在这样下去过不了多久主键就要超出范围了,插入就会失败,balabala……” ...

    MySQL的自增ID(主键) 用完了的解决方法

    如果用 int unsigned (int,4个字节 ), 我们可以算下最大当前声明的自增ID最大是多少,由于这里定义的是 int unsigned,所以最大可以达到2的32幂次方 – 1 = 4294967295。 这里有个小技巧,可以在创建表的时候,直接...

    浅谈MySQL中的自增主键用完了怎么办

    面试官:”那自增主键达到最大值了,用完了怎么办?” 你:”what,没复习啊!!” (然后,你就可以回去等通知了!) 这个问题是一个粉丝给我提的,我觉得挺有意(KENG)思(B)! 于是,今天我们就来谈一谈,这个自增主键...

    DogerRain#LearnJavaToFindAJob#MySQL的自增ID用完了会怎样?1

    1、MySQL的自增ID用完了会怎样 2、达到最大值了怎么办 3、有遇到过ID用完的情况吗

    SqlServer Mysql数据库修改自增列的值及相应问题的解决方案

    SQL Server 平台修改自增列值 由于之前处理过sql server数据库的迁移工作,尝试过其自增列值的变更,但是通过SQL 语句修改自增列值,是严格不允许的,直接报错(无法更新标识列 ‘自增列名称‘)。sql server我测试...

    oracle迁移mysql自增序列问题

    今天从oracle迁移数据到mysql碰到个需求:原先的主键字段需要改成mysql中的自增字段,而且原先数据的值不能变,以后新插入的值从原先数据最大的值开始自增。 解决办法: 构建环境: mysql> CREATE TABLE test -> ( ...

    PHP获取MySql新增记录ID值的3种方法

    使用此方法得到的是 id最大的值,确为最后一个值,但当多链接线程时,这个最大的id并不一定是我们插入数据的自增id值,因此不适用于多线程。 二,使用函数:msyql_insert_id(); 在PHP中,经常需要把插入数据库中的id...

    Mysql获取id最大值、表的记录总数等相关问题的方法汇总

    一、mysql 获取当前字段最大id SQL语句: select max(id) from yourtable; 二、获取mysql表自增(Auto_increment)值 Auto_increment是表中的一个属性,只要把表的状态获取到,也就可以获取到那个自增值 SQL语句: ...

    mysql中自增auto_increment功能的相关设置及问题

    mysql中的自增auto_increment功能相信每位phper都用过,本文就为大家分享一下mysql字段自增功能的具体查看及设置方法

    golang开发的ID生成器go-id-builder.zip

    如何实现的go-id-builder使用mysql来做为最大id数的持久化存储。程序在每次启动的时候都会加载数据表中当前的所记录的id类型,将会自动申请1000个(配置文件中可修改)新的id号,加载到一个缓冲通道中,当用户向生成...

    mysql数据库的基本操作语法

    auto_increment自增模式,设置自增后在插入数据的时候就不需要给该列插入值了。 4、 foreign key 约束 外键约束是保证一个或两个表之间的参照完整性,外键是构建于一个表的两个字段或是两个表的两个字段之间的参照...

    mysql基础只是总结

    float[(M,D)] 10的(M-D)次方->代表最大值 D->代表保留位数不够补0 //For float(M,D), double(M,D) or decimal(M,D), M must be >= D //只写M 0-24代表float精确度总的十位数超过6位用科学计数法,小数超出四舍五...

Global site tag (gtag.js) - Google Analytics