问题描述
好吧,哪一种更快更好用?
Alright, which is faster and better to use?
自动增量
或
调用一个执行最大值的sql,然后添加一个
Call an sql which which does a max and then add one
因此,最终的问题是,您应该使用mysql还是自己使用mysql?
So ultimately the question is should you use mysql to do it or do it yourself?
推荐答案
使用MAX的问题是隔离级别-充其量,该值可能会由于读取不正确的记录而过度膨胀.最糟糕的是,要获取当前最大值的查询是在上一次插入发生之前读取表-导致冲突.
The problem with using MAX is the isolation level - at best, there's a chance of the value getting over-inflated due to reading records that may be incorrect. At worst, the query to fetch the current max value is reading the table before a previous insert occurs - causing a collision.
另一个考虑因素是必须滚动自己的ID生成器和东西来检索ID,因为您不能使用LAST_INSERT_ID()
.
Another consideration is having to roll your own id generator and stuff to retrieve the id because you can't use LAST_INSERT_ID()
.
这篇关于MySQL auto_increment与max的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!