在Mysql中,我有一个包含两列(id、uuid)的表。然后我向其中插入了3000万个值。(ps: uuid可以重复)
现在,我想使用Mysql语法在表中查找重复值,但是sql花费了太多时间。
我想搜索所有列,但这需要很多时间,所以我尝试查询前一百万行,这花费了8秒。
然后我尝试了1000万行,它花了5分钟,然后2000万行,服务器似乎死了。
select count(uuid) as cnt
from uuid_test
where id between 1
and 1000000
group by uuid having cnt > 1;任何人都可以帮我优化sql,谢谢
发布于 2019-01-30 15:58:03
尝试此查询,
SELECT uuid, count(*) cnt FROM uuid_test GROUP BY 1 HAVING cnt>1;
希望能有所帮助。
发布于 2019-01-30 20:50:28
通常,查找重复项的最快方法是使用相关子查询,而不是聚合:
select ut2.*
from uuid_test ut2
where exists (select 1
from uuid_test ut2
where ut2.uuid = ut.uuid and
ut2.id <> ut.id
);这可以利用uuid_test(uuid, id)上的索引。
https://stackoverflow.com/questions/54434598
复制相似问题