ALTER语句的坑

alter modify 字段

ALTER 对于默认的Null值是不会修改的, 如果sex存在null的情况, 下面的语句会报错

1
2
3
ALTER TABLE customer
MODIFY `sex` int(11) NOT NULL DEFAULT '-1' COMMENT '性别. -1,未知; 0,女; 1,男';

必须先执行update

1
UPDATE customer SET  sex = -1 WHERE sex is NULL ;

alter 加字段

超过10W 的数据不能直接加字段, 需要创建临时表

参考 https://zhidao.baidu.com/question/917987491401145099.html

https://help.aliyun.com/document_detail/26132.html

alter加索引

30W 条数据, 2340ms, 大概10W条数据 800ms

1
2
3
4
5
6

alter table customer_device_info
add key idx_customer_id_is_use(customer_id, is_use),
add key idx_customer_id_registration_id(customer_id, registration_id(4)),
add key idx_device_id(device_id(15)),
drop key index_customer_id;