减少查找重复记录查询执行时间

时间:2016-09-14 10:59:24

标签: mysql

我有3个lac的记录。我需要计算重复记录并返回所有重复记录(例如,如果exam​​ple@example.com是10次,则返回所有10条重复10条记录)

我为此创建了查询,但需要15秒的时间。有什么建议可以减少时间吗?

SELECT g.guest_name, g.email, b.totalCount AS duplicate_guest
FROM guest g 
INNER JOIN (SELECT email, COUNT(Id) AS totalCount FROM guest  GROUP BY email ) b ON g.email = b.email

2 个答案:

答案 0 :(得分:0)

只需将HAVING count(*) > 1添加到内部选择

即可
SELECT g.guest_name, g.email, b.totalCount AS duplicate_guest
FROM guest g 
INNER JOIN 
(
     SELECT email, COUNT(Id) AS totalCount 
     FROM guest  
     GROUP BY email 
     HAVING count(*) > 1
) b ON g.email = b.email

答案 1 :(得分:0)

需要通过添加具有条件(如下面的查询)来减少加入条件的数据。

还要确保电子邮件列上应有一个索引以对其进行优化

SELECT g.guest_name, g.email, b.count as duplicate_guests
FROM guest g 
INNER JOIN 
(
     SELECT email, COUNT(Id) AS count
     FROM guest  
     GROUP BY email 
     HAVING count(*) > 1
) b ON g.email = b.email