批量删除

发布于 2024-08-12 01:58:29 字数 135 浏览 10 评论 0原文

我有一个包含超过 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 技术交流群。

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

发布评论

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

评论(2

绿萝 2024-08-19 01:58:29

尝试一下:

truncate table tblname

这具有不可回滚的优点,因此不必记录其操作,也不会触发触发器。这使得它比等效的 delete from tblname 快得多。

否则,您可以根据表数据计算出要删除的子集。例如,如果有一个包含姓氏的字段(希望已建立索引):

delete from tblname where lastname like 'A%'

请注意,这只是一个示例。您还可以使用

  • 工资等内容(<20K、20K-40K、40K-60K,...)。
  • SSN。
  • 当前余额。

等等。您的选择完全取决于您要删除的表。

我自己更喜欢 truncate 命令,因为它很简单。

Try out:

truncate table tblname

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:

delete from tblname where lastname like 'A%'

Note that this is just an example. You can also use things like

  • salary (<20K, 20K-40K, 40K-60K, ...).
  • SSN.
  • current balance.

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.

吻风 2024-08-19 01:58:29

删除是可以回滚的记录操作,因此它们可能会非常慢。如果您可以截断或删除并重新创建表,那么这是您最好的选择。或者,您可以执行此sql

BEGIN
loop 
  delete from table where rownum < 500
  exit when SQL%rowcount < 499
end loop;
END

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.

BEGIN
loop 
  delete from table where rownum < 500
  exit when SQL%rowcount < 499
end loop;
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文