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

猜你喜欢

转载自834945712.iteye.com/blog/2194667
今日推荐