简体   繁体   English

在MySQL多行插入中获取插入ID

[英]Getting the insert ids in a MySQL Multi-Row Insert

I am importing contacts from GMail, contacts can run into several hundreds and thousands. 我正在从GMail导入联系人,因此联系人可能会成百上千。

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. 我需要获取插入ID,以使我能够进一步将其详细信息插入另一个表中。

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. 考虑到我无法一次导入运行1,000个查询或更多查询,并且我的自动增量可能不一致,该如何处理。 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 考虑到我不能一次导入1,000个查询

Why such a consideration? 为什么要考虑? Neither PHP nor mysql have no such a limit on the number of queries. PHP和mysql都没有对查询数量的限制。

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. MySQL开发人员并不像您想象的那样愚蠢。 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. 只需创建一个队列,该队列要么是基于mysql表的自酿队列,要么是使用某些专用软件(如RabbitMQ)创建的,然后使某些后端服务器在后台进行所有导入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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