mysql筛选删除重复值,从MySQL表中删除重复值的最佳方法是什么?

I have the following SQL to delete duplicate values form a table,

DELETE p1

FROM `ProgramsList` p1, `ProgramsList` p2

WHERE p1.CustId = p2.CustId

AND p1.CustId = 1

AND p1.`Id`>p2.`Id`

AND p1.`ProgramName` = p2.`ProgramName`;

Id is auto incremental

for a given CustId ProgramName must be unique (currently it is not)

The above SQL takes about 4 to 5 hours to complete with about 1,000,000 records

Could anyone suggest a quicker way of deleting duplicates from a table?

解决方案

First, You might try adding indexes to ProgramName and CustID fields if you don't already have them.

De-Duping

You can group your records to identify dupes, and as you are doing that, grab the min ID value for each group. Then, just delete all records whose ID is not one of the MinID's.

In-Clause Method

delete from

ProgramsList

where

id not in

(select min(id) as MinID

from ProgramsList

group by ProgramName, CustID)

Join-Method

You may have to run this more than once, if there are many members per group.

DELETE P

FROM ProgramsList as P

INNER JOIN

(select count(*) as Count, max(id) as MaxID

from ProgramsList

group by ProgramName, CustID) as A on A.MaxID = P.id

WHERE A.Count >= 2

Some people have performance issues with the In-Clause, some don't. It depends a lot on your indexes and such. If one is too slow, try the other.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值