简体   繁体   English

我如何处理mysql表中的并发插入并获取正确的插入ID

[英]how do i handle concurrent inserts in mysql table and fetch the correct insert id

I am using adodb for PHP library. 我正在使用adodb for PHP库。

For fetching the id at which the record is inserted I use this function 为了获取插入记录的id,我使用此函数

"$db->Insert_ID()"

I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ? 我想知道是否有多个同时插入数据库表,这个方法会为每个插入的记录返回正确的插入ID吗?

The reason I am asking this is because I use this last insert id for further processing of other records and making subsequent entries in the related tables. 我问这个的原因是因为我使用这个最后一个插入id来进一步处理其他记录并在相关表中进行后续输入。

Is this approach safe enough or am I missing something. 这种方法是否足够安全,或者我错过了什么。

Please help me formulate a proper working plan for this so that I can use the last insert id for further inserts into the other table safely without having to mess up with the existing data. 请帮我为此制定一个正确的工作计划,以便我可以使用最后一个插入ID进一步插入到另一个表中,而不必弄乱现有数据。

Thanks 谢谢

Yes, it's safe for concurent use. 是的,对于使用它是安全的。 That's because LAST_INSERT_ID() is per-connection, as explained here : 这是因为LAST_INSERT_ID()是每个连接,如解释在这里

The ID that was generated is maintained in the server on a per-connection basis. 生成的ID在每个连接的基础上在服务器中维护。 This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. 这意味着函数返回给定客户端的值是为该客户端影响AUTO_INCREMENT列的最新语句生成的第一个AUTO_INCREMENT值。 This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. 此值不受其他客户端的影响,即使它们生成自己的AUTO_INCREMENT值。 This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions. 此行为可确保每个客户端都可以检索自己的ID,而无需关心其他客户端的活动,也无需锁定或事务。

$ db-> Insert_ID()将仅返回您的最后一个插入ID,因此如果您要插入许多记录并希望获取每个最后插入的行的ID,那么这将成功运行。

I want to know if there are multiple and simultaneous inserts into the database table, will this method return me the correct inserted id for each record inserted ? 我想知道是否有多个同时插入数据库表,这个方法会为每个插入的记录返回正确的插入ID吗?

It will return only the most recently inserted id. 它只返回最近插入的id。
In order to get ids for multiple inserts, you will have to call INSERT_ID() after each statement is executed. 为了获取多个插入的ID,您必须在执行每个语句后调用INSERT_ID() IE: IE:

INSERT INTO TABLE ....
INSERT_ID()

INSERT INTO TABLE ....
INSERT_ID()

...to get the id value for each insert. ...获取每个插入的id值。 If you ran: 如果你跑了:

INSERT INTO TABLE ....
INSERT INTO TABLE ....
INSERT_ID()

...will only return the id for the last insert statement. ...将仅返回最后一个insert语句的id。

Is this approach safe enough or am I missing something. 这种方法是否足够安全,或者我错过了什么。

It's safer than using SELECT MAX(id) FROM TABLE , which risks returning a value inserted by someone else among other things relating to isolation levels. 它比使用SELECT MAX(id) FROM TABLE更安全,它可能会返回由其他人插入的值以及与隔离级别相关的其他内容。

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

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