优化 sql 查询以获取重复项

Optimize sql query to get duplicates

A​​ 有以下 sql 查询:

SELECT users.* FROM users users

WHERE users.name <> '' and users.email <> '' and users.phone <> ''

and users.name in (  SELECT name
            FROM users
                where name <> '' and name is not null
            GROUP BY name
            HAVING count(name) > 1 )
and users.email in (  SELECT email
            FROM users
                where email <> '' and email is not null
            GROUP BY email
            HAVING count(email) > 1 )
and users.phone in (  SELECT phone
            FROM users
                where phone <> '' and phone is not null
            GROUP BY phone
            HAVING count(phone) > 1 )
ORDER BY users.name+users.email+users.phone ASC
LIMIT 0,200

不幸的是 运行 在庞大的数据库上速度非常慢。有优化此查询的选项吗?

查询结果思路:获取数据库中所有重复的记录(例如获取同名用户+同phone+同邮箱

我尝试使用内部联接,但似乎无法正常工作

如果您希望用户具有相同的姓名 phone 和电子邮件,请使用 group by:

select u.name, u.phone, u.email, group_concat(u.user_id)
from users u
group by u.name, u.phone, u.email
having count(*) > 1;

如果您想要所有行,而不仅仅是列表中的 ID,请使用 join:

select u.*
from (select u.name, u.phone, u.email
      from users u
      group by u.name, u.phone, u.email
      having count(*) > 1
     ) udup join
     users u
     on u.name = udup.name and u.phone = udup.phone and u.email = udup.email
order by u.name, u.phone, u.email;

注意:这些查询与您的原始查询不同。相反,它基于您在文本中描述的逻辑 ("for example get users with same name+same phone+same email")。