简体   繁体   中英

Getting the insert ids in a MySQL Multi-Row Insert

I am importing contacts from GMail, contacts can run into several hundreds and thousands.

I have this query doing the INSERT

$sql="INSERT IGNORE INTO lists (account_id, createdby, first_name, last_name, email, phone, date_created, list_type) VALUES $bindparam";
$importObj= otherquery($sql, $bind, $params);

I need to get the insert ids to enable me further insert their details in another table.

How do I go about this considering that I cannot run 1,000 queries or more in one import and also my auto-increment may not be consistent. The application is also used by many people at the same time which may mean that more than two people may be running inserts at a time (Not only via import).

Yet another question right out of wrong assumptions.

considering that I cannot run 1,000 queries or more in one import

Why such a consideration? Neither PHP nor mysql have no such a limit on the number of queries.

also my auto-increment may not be consistent.

There are already hundreds of questions here from the same doubtful users, who never actually had any issue with auto-increment consistency, yet who worry of it just in advance. All with reassuring answers. Mysql developers are not that silly as you imagine. Nothing wrong will happen with your auto-increment.

I only hope that you are queuing your import in some background process , instead of making user to wait for it on-line. Having your import done this way, you should experience no problems with number of queries or script execution time or whatever. Just create a queue, either home-brewed one based on mysql table, or by using some dedicated software like RabbitMQ, and then make some of your backend servers to do all the import in background.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM