Resolve database self-increment ID issues

Source: Internet
Author: User

(1) Setting the primary key self-increment why not to take
In this case, the database itself is a single point and cannot be detached because the ID is duplicated.

(2) Rely on the database self-increment mechanism to reach the global ID unique
Use the following statement:
REPLACE into Tickets64 VALUES (' a ');
SELECT last_insert_id ();
This guarantees that the global ID is unique, but the Tickets64 table is still a single point.

(3) Rely on the database self-increment mechanism to reach the global ID unique and eliminate the single point
On the basis of 2, deploy two (multiple) DB instances,
Set the self-increment step to 2 (more than the number of instances), i.e. Auto-increment-increment = 2
Set Auto-increment-offset to .....
The first database server has an auto-increment ID of 1 3 5 7 9
Second set is 2 4 6 8 10

(4) solve the problem of excessive pressure on the read library for each request global ID
For example, the first time a business service is started, a unique ID of 3559 is requested.
If the method is 2, 3, then the ID is 3559, so every time the database is requested, the database pressure is larger
can use 3559 * 65536 (for example, and not necessarily 65536) + memory self-increment variable to be used as ID
Retrieve a self-increment ID from the database when the memory self-increment variable reaches 65535
This way, even if there are multiple business servers, the ID is not duplicated:
First set 3559 * 65536 + 1,2,3.....65535
Second Set 3560 * 65536 + 1,2,3.....65535
Then the first to 65535, for a database self-increment ID, this time may be 3561 * 65536 + ....

Resolve database self-increment ID issues

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.