开发者

mySQL Quickly Delete Lots of Rows

开发者 https://www.devze.com 2023-03-18 00:00 出处:网络
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.

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开发者_如何学Python. 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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消