【问题标题】:web transactions having sql performance or accuracy issue具有 sql 性能或准确性问题的 Web 事务
【发布时间】:2018-04-14 20:17:35
【问题描述】:

故事是这样的:

当同一账户有2个10美元同时提现,账户总额只有10美元。

结果:均取款成功,账户余额为-$10。

如果我进行如下查询:

UPDATE table
SET amount = amount - 10
WHERE (amount-10 > 0) AND id = 123;

它将是安全和准确的,只有一次提款会成功。

由于金额无法索引,是否会出现性能问题?

【问题讨论】:

  • 您的id 列上可能已经有一个主键。此查询中不需要任何索引。

标签: mysql sql web-applications transactions query-performance


【解决方案1】:

将查询写成:

 UPDATE table
    SET amount = amount - 10
    WHERE amount > 10;

然后可以使用索引。我确实认为您应该将update 仅限于给定帐户:

 UPDATE table
    SET amount = amount - 10
    WHERE account = @account AND amount > 10;

那么account 上的索引总是可以使用的。

【讨论】:

  • 哦,还有一个问题。这个查询是不是 UPDATE table SET amount = amount - 10 WHERE amount > 10 AND account = @account;与 UPDATE 表相同 SET amount = amount - 10 WHERE account = @account AND amount > 10;
  • @user3581646 。 . .是的,它们是一样的。
  • 我会接受 Roland 的评论而不是 Gordon 的回答。但前提是 id 是 PK。
猜你喜欢
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多