(评论)
(comments)
原始链接: https://news.ycombinator.com/item?id=38781442
这篇文章强调了使用“UPDATE … LIMIT”语法作为处理数据库中并发访问问题的更简单的替代方案。 然而,作者建议一些 RDBMS 系统(例如 PostgresQL)已经在 SELECT FOR UPDATE 命令中包含此功能。 此外,作者指出某些 ORM 工具对类似功能的支持有限。 Ultimately, these approaches help eliminate problems such as "lost updates," and ensure data consistency regardless of the transaction being executed simultaneously by multiple users.
总的来说,这个问题通常可以通过应用悲观或乐观并发控制机制来解决,具体取决于具体的约束和要求。 乐观控制的并发更新在涉及无关紧要的值的情况下可能会很有效,例如计算追随者。 或者,悲观锁定解决方案(包括 SELECT FOR UPDATE 方法)可以解决关键数据的问题。 然而,无论选择哪种方法,都必须认识到并承认性能、弹性、复杂性和数据正确性之间的潜在权衡。 Finally, it's crucial to prioritize scalability and fault tolerance throughout database architecture and design processes.
关于 PHP、Ruby、Python 和 Java 等 Web 开发人员流行的语言中不存在 SELECT ... LOCK 关键字的建议 - 这是事实。 这些关键字通常在数据库管理界面中可用,包括像 pgAdmin 这样的控制台客户端或具有直接 SQL 界面的图形前端。 不幸的是,绝大多数在流行的 Web 框架中开发的数据库支持的应用程序严重依赖对象关系映射器 (ORM),这些映射器屏蔽了有关实际底层数据库操作的详细信息。 虽然这种方法有助于防止数据库模式和操作特定的实现差异,但它也可能导致效率低下或性能瓶颈。 因此,作者建议在可行的情况下考虑采用低级、直接、本机数据库支持库,特别是在使用分布式或实时系统时。 这些优势抵消了较小的学习曲线障碍。 总体而言,作者鼓励开发人员直接熟悉特定的关系数据库后端,而不是主要依赖于 ORM 抽象。 通过了解实际 SQL 语句和数据库原语背后的基本概念,开发人员可以显着提高应用程序的整体性能和正确性。
no it may not
this is fully standard compatible behavior and even expected behavior for _any_ SQL database implementing a read committed transaction insulation level
also it's a problem which on a theoretically level is not solvable with how SQL works, hence why on stricter isolation levels like serializable committing the transaction in such a situation will fail (in any SQL database) and needs to be retired. (Through in some unusual setups the db might be able to translate retry the transaction by itself, through at the cost of a ton of drawbacks)
Anyway reading the postgres documentation is always a good idea, it's not perfect but pretty good.
reply