LAST_INSERT_ID () analysis in mysql

Source: Internet
Author: User

LAST_INSERT_ID () in mysql analyzes the correctness of LAST_INSERT_ID under high concurrency in php with a discussion today. At the beginning, we were all vague. Later, after mutual query, I have a clear understanding of this issue. I sent this article to commemorate it. First, let's look at the official statement of LAST_INSERT_ID () in mysql:
LAST_INSERT_ID () automatically returns the value of the first table set in the AUTO_INCREMENT column in The Last INSERT or UPDATE query. Notes for LAST_INSERT_ID of MySQL:
1. The Connection object used for query and insertion must be the same, otherwise the return value is unpredictable. Second, LAST_INSERT_ID is irrelevant to the table. If you insert data to table a and then insert data to table B, LAST_INSERT_ID returns the Id value of Table B. Www.2cto.com 3. If you use an INSERT statement to INSERT multiple rows, LAST_INSERT_ID () returns only the value generated when the first row of data is inserted. Fourth, if you use insert ignore and the record is ignored, the AUTO_INCREMENT counter will not increment, and LAST_INSERT_ID () will return 0, which reflects that no records are inserted. Based on these four principles, the inserted values taken from the Growth Value for highly concurrent Website access are mainly related to the first rule and the second rule. That is, to ensure the correctness of LAST_INSERT_ID, the same connection must be established, and the LAST_INSERT_ID must be executed in the insert statement. Therefore, a common connection in the database cache pool may cause problems. multi-threaded operations may also cause problems when other insert operations are performed after the insert operation. The php database connection itself is page-level, which ensures that no buffer pool is available, and the php page execution itself is also single-threaded, this ensures that the LAST_INSERT_ID In the sequential writing must be followed by the insert statement, and no insert statement is executed in the middle. According to www.2cto.com, the page-level and single-thread of php have actually ensured that the LAST_INSERT_ID of mysql is correct and has nothing to do with high concurrency. That is to say, in php, LAST_INSERT_ID of mysql is always correct and can be used as needed. Author Zhao Kaijin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.