Daily Learning 20170224-Library sub-table global ID generation

Source: Internet
Author: User
Tags unique id

Due to the data volume and IO efficiency factors, many projects to the data-supported database will take the form of a library. One problem that needs to be resolved after using the sub-Library is the generation of primary keys. Primary keys between multiple tables cannot be supported with the database itself, since primary keys generated between tables are duplicated. So there is another way to get the primary key ID.

Generally speaking, there are three kinds of solutions: Oracle sequence: Seq.nextval based on third party Oracle to gain an ID advantage: simple and available disadvantage: need to rely on third party Oracle database MySQL ID interval isolation: Different libraries set different starting values and step lengths, such as 2 MySQL, you can set one to generate only odd, and another generation even. or 1 units with 0~10 billion, another one with 10~20 billion. Advantages: Use MySQL self-increasing ID disadvantage: high operation and maintenance costs, data expansion needs to reset the step. Based on database update + memory allocation: Maintain an ID in the database, get the next ID, the database will be id=id+100 where id=xx, get 100 IDs, the allocation advantage in memory: simple and efficient disadvantages: unable to guarantee the order of self-increase

In view of extensibility and maintainability, we have adopted a third scenario. The implementation is: we set get ID step is L, a total of n table allocation ID. After initialization, the ID values in n tables are 0,l, 2l,3l ... When an application obtains an ID from any table, the ID in the table adds a NL to the original ID value.

For example, the step is 100, a total of 4 ID tables initialized, 4 table values are, 1:0, 2:100, 3:200, 4:300 assuming that the application gets the ID from table 2, then the four table values become, 1:0, 2:500, 3:200, 4:300

This has several benefits: implementing a globally unique ID. Does not affect the expansion of the business database. Getting IDs is disaster-tolerant, and a single table cannot be accessed without affecting the global.

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.