什么时候应该使用MySQL事务? [英] When should I use MySQL transactions?

查看:674
本文介绍了什么时候应该使用MySQL事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了一些有关何时应该使用交易的文章.我读到:

I read some articles about when I should use transactions. I read:

我应何时使用交易? 基本上,只要您拥有对外部更改敏感或需要回滚所有更改(如果有)的工作单元, 错误或其他原因.

When should I use transactions? Basically any time you have a unit of work that is either sensitive to outside changes or needs the ability to rollback every change, if an error occurs or some other reason.

但是有人可以更好地向我解释吗?

But can someone explain it better to me?

  • 当我执行两个或多个时,我应该开始交易吗? 删除/更新/插入查询?
  • 当我只有一个删除/更新/插入查询时,我也应该开始交易吗?
  • 我应该在一个页面上进行10次类似的交易,还是对整个页面仅一次进行一次更好的交易,还是为每个页面建议一个最大值(例如5个)?
  • Should I start a transaction when I execute two or more delete/update/insert queries?
  • Should I also start a transaction when I just have one delete/update/insert query?
  • Should I start a transaction like 10 times on a page, or better only once for the whole page, or do you recommend a max for each page (for example 5)?

感谢您的帮助!

推荐答案

当您有一组彼此依赖的查询时,将使用事务.

Transactions are used when you have a group of queries that all depend on one another.

例如,一家银行

  • 银行客户约翰"将100美元转入爱丽丝"帐户.
  • 在此示例中,有2个查询(我没有显示日志记录或交易记录...等).您需要从John的余额中扣除$ 100,然后将其添加到Alice的余额中.
  • Start transaction
  • 从约翰扣除
    • UPDATE accounts SET balance=balance-100 WHERE account='John'
    • Bank customer "John" transfers $100 to the account of "Alice".
    • For this example, there are 2 queries (I'm not showing logging or transaction history...etc). You need to deduct $100 from John's balance and add that to Alice's balance.
    • Start transaction
    • Deduct from John
      • UPDATE accounts SET balance=balance-100 WHERE account='John'
      • UPDATE accounts SET balance=balance+100 WHERE account='Alice'

      在您提交交易之前,不会保存交易.因此,如果两个查询中的任何一个错误,都可以调用rollback并撤消自事务启动以来运行的所有查询.如果由于某种原因向Alice添加$ 100的查询失败,您可以回滚而不从John扣除$ 100.这是确保您可以根据需要自动撤消查询的一种方法.

      A transaction isn't saved until you commit it. So if there was an error in either query, you could call rollback and undo any queries that have run since the transaction was started. If for some reason the query for adding $100 to Alice failed, you could rollback and not deduct $100 from John. It is a way of ensuring that you can undo queries automatically if needed.

      • 当我执行两个或多个删除/更新/插入查询时,我应该开始交易吗?

      • Should I start a transaction when I execute two or more delete/update/insert queries?

      取决于查询的内容.

      当我只有一个删除/更新/插入查询时,我也应该启动事务吗?

      Should I also start a transaction when I just have one delete/update/insert query?

      不是必需的,除非您需要一种回滚(撤消)查询的方式,就像您想要在调用commit(保存)之前进行更新并验证它一样.

      我应该在一个页面上进行10次类似的交易,还是对整个页面仅进行一次更好的交易,还是建议为每个页面设置一个最大值(例如5)?

      Should I start a transaction like 10 times on a page, or better only once for the whole page, or do you recommend a max for each page (for example 5)?

      根据需要启动任意多个.我会怀疑您每页有多个事务,因为您很可能在每次加载页面时都做一件事(即转账).

      这篇关于什么时候应该使用MySQL事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆