并发数据插入中的mysql_insert_id问题

2021-12-28 00:00:00 concurrency mysql mysql-insert-id

mysql_insert_id() 在竞争条件下的可靠性如何?我的意思是当多个用户同时插入数据时,这个函数是返回真实的ID还是返回其他用户插入的数据ID?

How reliable is mysql_insert_id() in a competitive condition? I mean when multiple users are inserting data at the same time, will this function return the true ID or it will return other user's inserted data ID?

表引擎是 MyISAM.

The table engine is MyISAM.

推荐答案

mysql_insert_id 是完全多用户安全的,完全依赖数据库连接,每个连接只能有一个用户....所以,根据你的问题,它返回真实的 id....

mysql_insert_id is completely multi-user safe.It is entirely relies upon the database connection, and you can only have one user per connection....So, as per your question, it returns true id....

来自 MySql 文档,

生成的 ID 在服务器上维护每个连接的基础.这意味着返回的值给定客户端的函数是生成的第一个 AUTO_INCREMENT 值对于影响 AUTO_INCREMENT 列的最新语句客户.该值不会受到其他客户端的影响,即使他们生成自己的 AUTO_INCREMENT 值.这种行为确保每个客户端都可以检索自己的 ID 而不用关心其他客户端的活动,并且不需要锁或交易.

The ID that was generated is maintained in the server on a per-connection basis. 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. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. 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.

相关文章