MySQL auto_increment vs max

Ok, which is faster and better to use?

auto_increment

or

Call sql that does max and then add one

So the question ultimately comes up, should I use mysql for this or do it myself?

+2


source to share


3 answers


The problem with using MAX is the isolation level - at best, the probability of exceeding the value is overestimated due to reading records, this may be incorrect. In the worst case, the query to select the current maximum value reads the table before the previous insert occurs, causing a collision.



Another consideration is that you need to roll your own id generator and stuff to get the id, because you cannot use LAST_INSERT_ID()

.

+2


source


Definitely an autoincrement. The SQL you will write is not thread safe and will only apply to data that comes in through your specific stored procedure / sql.



+4


source


auto_increment will be faster than the max aggregate, which at least has to index at least, or a full table scan in the worst case. Also, max () cannot be thread safe unless you are very careful.

+3


source







All Articles