已有数据的表-建联合索引

Posted weihengblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了已有数据的表-建联合索引相关的知识,希望对你有一定的参考价值。

清理重复数据,建立联合唯一索引

1. 查看重复数据

SELECT * FROM holiday_focamobilerel 
WHERE (cardnumber, mobile) IN (SELECT cardnumber, mobile FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1);

2. 删除重复数据

- 保留order_id and sub_order_id不为空的

- 如果order_id和sub_order_id都为空,保留id最大的

DELETE FROM holiday_focamobilerel h
WHERE (cardnumber, mobile) IN (SELECT cardnumber, mobile FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1)
AND h.id NOT IN (select id from holiday_focamobilerel h where h.order_id  is not null  or h.sub_order_id  is not null )
and h.id not in (SELECT MAX(id) FROM holiday_focamobilerel GROUP BY cardnumber, mobile HAVING COUNT(1) > 1);

 3. 建立联合唯一约束

alter table holiday_focamobilerel add constraint cardnumber_mobile_key unique("cardnumber","mobile")

 

以上是关于已有数据的表-建联合索引的主要内容,如果未能解决你的问题,请参考以下文章

mysql 联合索引和唯一索引

给有重复记录的表添加唯一索引

联合索引和多个单列索引选择

适合建索引?不适合建索引?分析

关于oracle索引数量

MySQL联合索引VS单列索引