Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PDO::lastInsertId reliable with very rapid inserts?

Tags:

php

mysql

pdo

yii

I'm using the Yii PHP framework which has a function PDO::lastInsertId that's apparently just an implementation of PDO::lastInsertId. If my application has very rapid inserts from possibly thousands of simultaneous users, will this function be reliable to get the Auto-incremented row ID of the data I just inserted?

I need to get the id of the row I just inserted to do a bit of work after the insert itself, but want to make sure that if the insert rate is very high, it wont cause inconsistent results.

Thanks!

like image 508
DJSunny Avatar asked Dec 17 '22 09:12

DJSunny


1 Answers

yes, of course, no worries about that, but you've to make sure to ask the lastInsertId just after the insert query. No other query should be executed on that connection in the meantime, each PHP process must have a separate connection.

Also if you think that a table is going to have hundreds or thousands of insert per second, consider to not use indexes or use the minimum amount of indexes. In case of MySQL favour MyISAM tables.

like image 95
stivlo Avatar answered Jan 03 '23 07:01

stivlo