Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, C++ - Programmatically, How does MySQL Autoincrement Work?

From the latest source code (not certain if it's C or C++) of MySQL, how does it do an autoincrement? I mean, is it efficient in that it stores like a metadata resource on the table where it last left off, or does it have to do a table scan to find the greatest ID in use in the table? Also, do you see any negative aspects of using autoincrement when you look at how it's implemented versus, say, PostgreSQL?

like image 277
Volomike Avatar asked Nov 05 '11 20:11

Volomike


1 Answers

That will depend on which engine the database is using. InnoDB is storing the largest value in memory and not on disk. Very efficient. I would guess most engines would do something similar, but cannot guarantee it.

InnoDB's Auto Increment Is going to run the below query once when DB is loaded and store the variable in memory:

 SELECT MAX(ai_col) FROM t FOR UPDATE;

Comparing that to PostgreSQL's complete lack of an auto_increment depends on how you would implement the field yourself. (At least it lacked it last time I used it. They may have changed) Most would create a SEQUENCE. Which appears to be stored in an in memory pseudo-table. I'd take InnoDBs to be a simpler better way. I'd guess InnoDB would be more efficient if they are not equal.

like image 135
Joe McGrath Avatar answered Oct 02 '22 19:10

Joe McGrath