Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id(), where they run as 2 separate queries?
I'm concerned that before the last_insert_id() can run, another insert will take place.
Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id(), where they run as 2 separate queries?
I'm concerned that before the last_insert_id() can run, another insert will take place.
Is using SELECT Max(ID) FROM table safer than using SELECT last_insert_id()
Definitely not, never! LAST_INSERT_ID()
exists exactly for the reason you state: Other clients could have made other inserts. LAST_INSERT_ID()
always gives you the last inserted ID on the current connection.
Using select max(id)
is just asking for trouble. It's only a matter of time before two inserts would happen near-simultaneously and you would start to see some craziness in your data.