批量删除
我有一个包含超过 1400 万行的数据库表。
如果我尝试执行:
delete from table
连接挂起。
如何“批量”删除所有行,以便最终删除它们?
I have a database table that has >14 million rows.
If I attempt to perform:
delete from table
The connection hangs out.
How can I delete all the rows, in "batches" so they are deleted eventually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试一下:
这具有不可回滚的优点,因此不必记录其操作,也不会触发触发器。这使得它比等效的
delete from tblname
快得多。否则,您可以根据表数据计算出要删除的子集。例如,如果有一个包含姓氏的字段(希望已建立索引):
请注意,这只是一个示例。您还可以使用
等等。您的选择完全取决于您要删除的表。
我自己更喜欢 truncate 命令,因为它很简单。
Try out:
This has the advantage of not being rollback-able so will not have to log its actions, and also does not fire triggers. That makes it substantially faster than the equivalent
delete from tblname
.Otherwise, you can work out subsets to delete, based on your table data. For example, if there's a field (hopefully indexed) containing last names:
Note that this is just an example. You can also use things like
and so on. What you choose depends entirely on the table you're trying to delete.
I prefer the
truncate
command myself due to its simplicity.删除是可以回滚的记录操作,因此它们可能会非常慢。如果您可以截断或删除并重新创建表,那么这是您最好的选择。或者,您可以执行此sql。
deletes are logged operation which can be rolled back, hence they can be quite slow. If you can truncate or drop and recreate the table, that's your best bet. Alternatively, you can execute this sql.