删除子表的多条记录

发布于 2024-07-13 03:35:06 字数 335 浏览 3 评论 0原文

情况:

  • 表 TBL 有约 10k 条目需要删除,
  • 表 TBL 有 14 个子表,删除规则为“无操作”,
  • 我想删除 10k 条目以及子表中引用的条目。

过程:

  • 删除子表中的记录,
  • 禁用约束(如果不禁用约束,下一步的删除将永远进行),
  • 删除TBL表中的记录,
  • 启用约束。

有更优雅的方法吗?

问题是第三步花费的时间太长,因为它要检查 14 个大表中是否存在不存在的记录。 我的程序有很好的执行时间,但我认为有更优雅的方法。

Situation:

  • Table TBL has ~10k entries for deletion,
  • Table TBL has 14 child tables with delete rule "no action",
  • I want to delete 10k entries and referenced entries in child tables.

Procedure:

  • Delete records in child tables,
  • Disable constraints (if constraints are not disabled deletion in next step takes forever),
  • Delete records in TBL table,
  • Enable constraints.

Is there more elegant way for doing this?

The problem is that third step takes too long because it is checking 14 big tables for existence of non existent records. My procedure has good execution time but I think that there is more elegant way.

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

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

发布评论

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

评论(1

枯寂 2024-07-20 03:35:06

听起来您需要在子表上索引外键。 每次删除子表上没有索引的父记录时,都必须对子表进行全表扫描以检查外键约束是否被破坏。

对于索引,最坏的情况是索引范围扫描。

编辑
可以在此处找到更多信息以及用于确定这是否是您的问题的脚本。
http://asktom.oracle.com/tkyte/unindex/index.html

It sounds like you need to index your foreign keys on your child tables. Every time you delete a parent record without an index on the child table it must do a full table scan of the child to check if the foreign key constraint was broken.

With the index, it is at worst case an index range scan.

Edit:
More info, and a script to determine if this is your problem can be found here.
http://asktom.oracle.com/tkyte/unindex/index.html

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