Method for allocating a unique player ID in a region (continued)

Source: Internet
Author: User


Method for allocating a unique player ID in a region (continued) Links: Method for allocating a unique player ID in a region http://www.bkjia.com/database/201212/173832.html In the past, due to the rush of time, the only ID generated in the region was simply a simple method. Today, taking the time to write out some of the processes, it is also a summary. Www.2cto.com first talked about the demand. Currently, databases in the game use the table sharding method to distribute hot spots. The previous AUTO_INCREMENT method of a table obviously did not work. Is it feasible to put the unique ID in the business process? This is also not good, because there are multiple deployments in the background, each process can ensure that the ID is unique, but it may be repeated when it is stored in the database, so we still have to make an article on the DB. Therefore, there is a solution in the previous article. A single table is used to generate a unique ID and provide a unique ID for multiple businesses, in multi-process scenarios, tables do not need to be locked, and the efficiency is relatively high. Is it very similar to the factory in the design mode. Next I will analyze this table. Let's take a look at the table structure and usage create table SeqTab (iSeqNo bigint (20) not null default 0, // indicates the unique ID iSeqType int (11) not null default 0, // indicates the Business ID primary key (iSeqNo, iSeqType); insert into SeqTab values (0, 1); // initialization, assume that a service number is 1 update SeqTab set iSeqNo = last_insert_id (iSeqNo + 1) where iSeqType = 1; select last_insert_id (); // these two sentences are used to obtain the unique ID of a service, used by business processes. In fact, the key to this table is the LAST_INSERT_ID () method. It has two forms: LAST_INSERT_ID () and LAST_INSERT_ID (expr ). This is characterized by: 1. each CONNECTION does not affect each other. does not belong to a specific table 3. returns the last INSERT AUTO_INCREMENT value 4. if INSERT is used to INSERT multiple rows of data, only the value generated by the first row of data is returned. if you UPDATE a value of AUTO_INCREMENT, the return value of LAST_INSERT_ID () is slightly different from that of LAST_INSERT_ID (expr). First, it returns the value of expr, its return value is recorded in LAST_INSERT_ID (). Here we mainly use the first and second features. Each process executes the update SeqTab set iSeqNo = last_insert_id (iSeqNo + 1) where iSeqType = 1 ;, obtain the iSeqNo of the process and record it in LAST_INSERT_ID. Obtain the value in the second sentence. Next, we will compare some other methods. The first is to directly create a table with an ID field set to AUTO_INCREMENT. This problem is that each Business ID is not continuous and discrete. The second method is to use GUID or UUID, but I personally think it is a difference in efficiency. The string is not as efficient as a number. In addition, the digit ID can be spliced with some information in the future, later, you can easily obtain the region where the object is located.

Related Article

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.