mySQL快速删除大量行

发布于 2024-11-18 17:10:24 字数 354 浏览 2 评论 0原文

我有两个表,我需要第一个表中未出现在第二个表中的所有行。

这些表可以被销毁,因为它们是从其他表转储的。

第一个表有约 5700 万行。第二个表有大约 1000 万行。

由于显而易见的原因,这两个查询都花费了很长时间,请帮助我更快地完成此操作。

SELECT *
FROM db.first
WHERE id NOT IN (SELECT id FROM db.second)
DELETE FROM db.first
WHERE id IN (SELECT id FROM db.second)

编辑:我不需要第二个表中的任何记录,我只需要第一个表中出现但第二个表中未出现的行。

I have two tables, I need all rows from the first that don't appear in the second.

The tables can be destroyed as they're dumps from other tables.

First table has ~57million rows. Second table has ~10million rows.

Both of these queries are taking forever for obvious reasons, please help me do this quicker.

SELECT *
FROM db.first
WHERE id NOT IN (SELECT id FROM db.second)
DELETE FROM db.first
WHERE id IN (SELECT id FROM db.second)

Edit: I don't need any records from the second table, I only need rows that appear in the first table that don't appear in the second table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病毒体 2024-11-25 17:10:24

可能会快得多:

select one.*
from db.first one
left join db.second two on one.id = two.id
where two.id is null

使用 join:和删除

delete first
from first 
join second on first.id = second.id

It would probably be a lot quicker using joins:

select one.*
from db.first one
left join db.second two on one.id = two.id
where two.id is null

and the delete:

delete first
from first 
join second on first.id = second.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文