我仍在学习并试图理解SQL中的内容。我有一个脚本,可以像这样批量删除:
while rows_were_deleted:
DELETE FROM some_table WHERE id IN (SELECT id FROM deletes_table) LIMIT 1000
这很慢。我首先想到的是,SELECT id FROM deletes_table
花费了很长时间,因为deletes_table
有数千行。所以我尝试了类似的东西
while rows_were_deleted:
ids_to_delete = SELECT id FROM deletes_table
for delete_id in ids_to_deletes:
DELETE FROM some_table WHERE id = delete_id LIMIT 1000
这甚至更慢。我猜是因为还有更多的操作。这样做的最佳方法是什么?
答案 0 :(得分:0)
while rows_were_deleted:
DELETE FROM some_table WHERE id IN (SELECT id FROM deletes_table) LIMIT 1000
您也可以尝试向select id from deletes_tables
添加限制。