我可以用保存点来替代 Oracle 中的新事务吗?

发布于 2024-08-07 12:22:25 字数 558 浏览 7 评论 0原文

现在,我们用于插入记录集的过程是这样的:(

请注意,“记录集”是指一个人的记录及其地址、电话号码或任何其他连接表)。

  1. 开始交易。
  2. 插入一组相关的记录。
  3. 如果一切成功则提交,否则回滚。
  4. 返回步骤 1 获取下一组记录。

我们应该做更多类似的事情吗?

  1. 在脚本开头启动事务
  2. 为每组记录启动一个保存点。
  3. 插入一组相关记录。
  4. 如果出现错误则回滚到保存点,如果一切成功则继续。
  5. 在脚本开头提交事务。

在遇到 ORA-01555 的一些问题并阅读了一些 Ask Tom 文章(例如 这个),我正在考虑尝试第二个过程。当然,正如 Tom 指出的那样,启动新事务应该由业务需求来定义。第二个过程值得尝试还是一个坏主意?

Right now the process that we're using for inserting sets of records is something like this:

(and note that "set of records" means something like a person's record along with their addresses, phone numbers, or any other joined tables).

  1. Start a transaction.
  2. Insert a set of records that are related.
  3. Commit if everything was successful, roll back otherwise.
  4. Go back to step 1 for the next set of records.

Should we be doing something more like this?

  1. Start a transaction at the beginning of the script
  2. Start a save point for each set of records.
  3. Insert a set of related records.
  4. Roll back to the savepoint if there is an error, go on if everything is successful.
  5. Commit the transaction at the beginning of the script.

After having some issues with ORA-01555 and reading a few Ask Tom articles (like this one), I'm thinking about trying out the second process. Of course, as Tom points out, starting a new transaction is something that should be defined by business needs. Is the second process worth trying out, or is it a bad idea?

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

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

发布评论

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

评论(2

终难愈 2024-08-14 12:22:25

事务应该是一个有意义的工作单元。但工作单元的构成取决于上下文。在 OLTP 系统中,工作单元将是单个人员及其地址信息等。但这听起来好像您正在实现某种形式的批处理,这会加载大量人员。

如果您遇到 ORA-1555 问题,几乎可以肯定是因为您有一个长时间运行的查询,提供的数据正在由其他事务更新。在循环内提​​交有助于 UNDO 段的循环使用,因此往往会增加您所依赖的提供读取一致性的段被重用的可能性。所以,不这样做可能是个好主意。

使用 SAVEPOINT 是否是解决方案是另一回事。我不确定在您的情况下这会给您带来什么优势。当您使用 Oracle10g 时,也许您应该考虑使用批量 DML 错误日志记录 相反。

或者,您可能希望重写驱动查询,以便它可以处理较小的数据块。如果不了解您的流程细节,我无法给出具体建议。但一般来说,与其为 10000 条记录打开一个游标,不如为每次弹出 500 行打开它 20 次。另一件需要考虑的事情是插入过程是否可以变得更加高效,例如使用批量收集和 FORALL。

A transaction should be a meaningful Unit Of Work. But what constitutes a Unit Of Work depends upon context. In an OLTP system a Unit Of Work would be a single Person, along with their address information, etc. But it sounds as if you are implementing some form of batch processing, which is loading lots of Persons.

If you are having problems with ORA-1555 it is almost certainly because you are have a long running query supplying data which is being updated by other transactions. Committing inside your loop contributes to the cyclical use of UNDO segments, and so will tend to increase the likelihood that the segments you are relying on to provide read consistency will have been reused. So, not doing that is probably a good idea.

Whether using SAVEPOINTs is the solution is a different matter. I'm not sure what advantage that would give you in your situation. As you are working with Oracle10g perhaps you should consider using bulk DML error logging instead.

Alternatively you might wish to rewrite the driving query so that it works with smaller chunks of data. Without knowing more about the specifics of your process I can't give specific advice. But in general, instead of opening one cursor for 10000 records it might be better to open it twenty times for 500 rows a pop. The other thing to consider is whether the insertion process can be made more efficient, say by using bulk collection and FORALL.

蓝海 2024-08-14 12:22:25

一些想法...

  1. 在我看来,asktom 链接的要点之一是适当调整回滚/撤消的大小以避免 1555。有什么原因这是不可能的吗?正如他指出的那样,购买磁盘比编写/维护代码来解决回滚限制要便宜得多(尽管在阅读了 36Gb 驱动器 250 美元的价格标签后我不得不再三考虑 - 该线程始于 2002 年)很好地说明了摩尔定律!)
  2. 此链接 (Burleson) 显示了一个可能的问题保存点。
  3. 您的交易实际上是第二个场景中的步骤 2、3 和 5 吗?如果是这样,那就是我要做的——提交每笔交易。在我看来,场景 1 听起来有点像将一组交易合并为一个?

Some thoughts...

  1. Seems to me one of the points of the asktom link was to size your rollback/undo appropriately to avoid the 1555's. Is there some reason this is not possible? As he points out, it's far cheaper to buy disk than it is to write/maintain code to handle getting around rollback limitations (although I had to do a double-take after reading the $250 pricetag for a 36Gb drive - that thread started in 2002! Good illustration of Moore's Law!)
  2. This link (Burleson) shows one possible issue with savepoints.
  3. Is your transaction in actuality steps 2,3, and 5 in your second scenario? If so, that's what I'd do - commit each transaction. Sounds a bit to me like scenario 1 is a collection of transactions rolled into one?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文