去除外键约束、引用完整性和 Hibernate

发布于 2024-09-08 22:34:38 字数 765 浏览 4 评论 0原文

我的同事提到,我们的客户 DBA 建议删除我们项目 Oracle DB 模式中的所有外键约束。起初我并不同意这个决定。我是一名开发人员而不是 DBA。所以后来意识到这个决定背后可能有一些原因。所以我正在尝试了解这个决定的利弊。

项目信息:

  1. 具有 Hibernate 持久性的 Spring 应用程序。
  2. Oracle 10g DB
  3. 有些批处理作业仅使用 SQL 加载程序或普通 JDBC。

这是我的优点和缺点列表(如果我错了,请纠正我)

优点:

  1. 由于应用程序持久性由 Hibernate 管理,因此不需要外键级联。它由 Hibernate 使用适当的级联选项进行管理。

  2. Hibernate DELETE 操作(包括删除级联选项)会在删除其主键记录之前删除外键表记录(即避免引用完整性问题)。对于无外键情况、外键情况和外键级联情况,此行为是相同的。但是添加外键会不必要地减慢Oracle删除操作。

缺点

  1. Hibernate 提供了一种管理对象之间关联以及关联内级联操作的机制。但它从来没有提供 DB 那样完整的引用完整性解决方案。

  2. 仅使用 SQL 加载程序或纯 JDBC 的批处理作业需要引用完整性。

伙计们,我需要你的建议。如果你们中有人是 DBA,请提供 DBA 的侧面原因。

谢谢。

My colleague mentioned that our client DBA proposed the removal of all foreign key constraints in our project Oracle DB schema. Initially I did not agree with the decision. I am a developer not a DBA. So later realized that there could be some reasons behind the decision. So I am trying get the pros and cons of this decision.

Proj info:

  1. Spring application with Hibernate persistent.
  2. Oracle 10g DB
  3. There are batch jobs use only SQL-loader or plain JDBC.

Here is my list of pros and cons (Please correct me if I am wrong)

Pros:

  1. Since application persistent is managed by Hibernate, foreign key cascading is not necessary. it is managed by Hibernate with appropriate cascading option.

  2. Hibernate DELETE action(includes delete cascading option) removes the foreign key table records before removing its primary key record (i.e to avoid referential integrity issue). This behavior is same for no-foreign-key case, foreign-key case and foreign-key-with-cascade case. But adding foreign-key will unnecessarily slow down Oracle delete operation.

Cons

  1. Hibernate provides a mechanism for managing association between objects and cascading operations within association. But it never provides complete referential integrity solution that DB has.

  2. Referential integrity is required for those batch jobs use only SQL-loader or plain JDBC.

Guys, I need your advice on this. If anyone of you are a DBA, please provide DBA side reasons.

Thank you.

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

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

发布评论

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

评论(4

乱世争霸 2024-09-15 22:34:38

我以前从未听过 DBA 提出这样的建议!是的,来自应用程序开发人员,但绝不会来自数据库管理员。这令人难以置信。

Tom Kyte已经说过很多次了(例如这里 ):应用程序来来去去,但数据是永恒的。

根据我自己的经验,我曾使用过已有 20 多年历史的 Oracle 数据库。他们从 Oracle 6 开始,多年来迁移到 10G 或 11g - 相同的数据。但是最上面的应用程序呢?首先它们是 Forms 3.0,然后在某些情况下迁移到 C++,有些在 Forms 6i 中重新构建,有些在 Application Express 中重新构建。当然,ADF 是另一种可能性;或者也许是 SOA 架构...

当前的应用程序开发工具有什么特别之处,以至于它突然取代了 Oracle 作为 DBMS 的工作?

I have never heard such a proposal from a DBA before! From an application developer, yes, but never from a Database Administrator. It beggars belief.

Tom Kyte has said many times (for example here): applications come and go, but data is forever.

In my own experience, I have worked on Oracle databases that are 20+ years old. They started out in Oracle 6 and got migrated up to 10G or 11g over the years - the same data. But the applications that sat on top? First they were Forms 3.0, then in some cases they got migrated to C++, in some got re-built in Forms 6i, in some rebuilt in Application Express. ADF is another possibility of course; or perhaps a SOA architecture...

What's so special about the current application development tool that it suddenly takes over Oracle's job as the DBMS?

相守太难 2024-09-15 22:34:38

我曾在决定放弃引用完整性约束的项目中从事数据库工作。

我们必须编写“QC 脚本”来检测每个表关系的孤立行(孤立行将被外键约束阻止)。

然后,当(而不是如果)它们发生时,我们必须制定如何解决孤儿问题的政策。选项包括以下内容:

  • 删除孤立行。
  • 归档孤立行。
  • 将任何孤立外键值更新为 NULL。
  • 将任何孤立外键值更新为父表中的某个现有值。
  • 与异常共存。编写更多代码以从报告中排除孤儿。也许是所有表的一组视图?

您可能希望与该数据库的利益相关者安排每周一次的会议,以审查 QC 脚本报告,并决定如何处理每个孤立行。

没有任何框架能够像数据库中运行的约束那样可靠地强制执行引用完整性。只有数据库才能提供真正的原子更改并确保一致性。

I've worked on databases in projects that decided to drop referential integrity constraints.

We had to write "QC script" to detect orphaned rows with respect to every table relationship (orphaned rows would have been prevented by a foreign key constraint).

Then when (not if) they occured, we had to have policies for how to resolve the orphans. Choices included the following:

  • Delete orphaned rows.
  • Archive orphaned rows.
  • Update any orphaned foreign key values to NULL.
  • Update any orphaned foreign key values to some existing value in the parent table.
  • Live with the anomalies. Write more code to exclude orphans from reports. Maybe a set of VIEWs over all the tables?

You might want to schedule a recurring weekly meeting with the stakeholders of this database to review the QC script report, and decided what to do with each of the orphaned rows.

No framework can enforce referential integrity as reliably as constraints that run in the database. Only the database can provide truly atomic changes and ensure consistency.

过期以后 2024-09-15 22:34:38

由于数据库约束得到保证,因此在某些情况下,它们可以允许额外的优化。

例如,假设您有一个视图

CREATE VIEW orders_vw AS
SELECT ord.order_id, ord.customer_id, lin.product_id
FROM orders ord JOIN order_lines lin on ord.order_id = lin.order_id

,那么您有一个执行 SELECT Product_id FROMorders_vw WHERE order_id = :val 的查询
通过强制执行完整性,数据库知道 order_lines 中的任何 order_id 在父表中都有一行,并且由于实际上没有选择订单表中的任何值,因此它可以通过不访问订单表来节省工作量。
如果没有约束,数据库无法确定 order_lines 中的条目是否有父项,因此它必须执行访问订单表来检查它的额外工作。

根据您的查询模式,您可能会发现删除约束实际上会增加数据库的工作负载。

Since database constraints are guaranteed they can, in some circumstances, allow additional optimizations.

For example, say you have a view

CREATE VIEW orders_vw AS
SELECT ord.order_id, ord.customer_id, lin.product_id
FROM orders ord JOIN order_lines lin on ord.order_id = lin.order_id

Then you have a query that does a SELECT product_id FROM orders_vw WHERE order_id = :val
With the integrity enforced, the database knows that any order_id in order_lines has one row in the parent table and, since no value from the orders table are actually selected, it can save work by not visiting the orders table.
Without the constraint, the database can't be sure that an entry in order_lines has a parent, so it has to do the extra work of visiting the orders table to check it.

Depending on your query patterns, you may find removing constraints actually increases the workload on the DB.

从此见与不见 2024-09-15 22:34:38

通常,数据库性能优化首先从去除外键开始。这是一种权衡:您在 DBMS 级别上出售有保证的完整性,并且必须自己管理它(这对于 Hibernate 来说相当容易,但要求在普通 SQL 中非常准确),并且由于查询中的外键检查,您可以获得更高的查询性能相当昂贵。

Usually, foreign key removal is what database performance optimization starts with. It's kind of trade-off: you sell guaranteed integrity on DBMS level and have to manage it yourself (which is fairly easy with Hibernate but requires to be very accurate in plain SQL), and you get increased query performance since foreign key checks in queries are quite expensive.

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