Implementation of auto-incrementing sequence generator

Source: Internet
Author: User


Implementation idea of auto-incrementing sequence generator l requirement u achieves the equivalent function of auto-incrementing Field Values in MySQL; the auto-incrementing Field Values in database tables in the same application cluster are globally unique; u supports horizontal sharding tables at the database level and horizontal sharding tables within the database. The IDS of these tables are from the same configuration record; www.2cto.com l storage design sequence generator configuration table increment_config structure: whether the column name data type is null default value auto-incrementing primary key/index remarks id int unsigned n AUTO_INCREMENT primary key (ID) Unique Identifier, meaningless TABLE_NAME VARCHAR (40) n unique index table name TABLE_TOTAL tinyint unsigned n 0 mark the number of table shards in the database COLUMN_NAME VARCHAR (40) N field name START_VALUE bigint unsigned n 1 start value OFFSET_VALUE smallint unsigned n 10000 growth step flag tinyint n 0 0 0-normal; 1-void value GMT_MODIFIED timestamp n database automatically updates this value
Note: 1>. GMT_MODIFIED is automatically filled in and updated through database features rather than automatically updated by programs or manually; 2>. for horizontal table shards in the database, that is, TABLE_TOTAL <> 0, it is differentiated by TABLE_NAME + TABLE_TOTAL; l for tables whose operations are initiated by www.2cto.com u, add a record to this configuration table, for example, insert into (TABLE_NAME, TABLE_TOTAL, COLUMN_NAME, START_VALUE, OFFSET_VALUE, FLAG) VALUES ('msg _ ', 23, 'msg _ id', 0); the SQL program operated by the u sequence generator obtains the sequence interval segment each time, and the SQL statement used to update the transaction for related data value operations. Assume that the corresponding records of the msg _ series tables are ID = 1 in the configuration table. Start trnsaction; select id, TABLE_NAME, COLUMN_NAME, START_VALUE, START_VALUE + OFFSET_VALUE AS your increment_config where id = 1 for update; UPDATE increment_config SET START_VALUE = START_VALUE + OFFSET_VALUE where id = 1; COMMIT; l program implementation we recommend that u improve the service processing performance by providing the speed at which the sequence is generated. The program needs to be implemented in the form of sequential intervals, instead of getting the serial number value from the database each time it is needed. During each startup of the u program, to initialize the sequence interval values of all valid records in the configuration table; u when the range value obtained by the program START_VALUE reaches: START_VALUE = END_VALUE after a period of use, the processing steps are as follows: www.2cto.com 1>. suspends the current sequential call request; 2>. perform the transaction to obtain the sequence range value; 3>. the START_VALUE of the newly obtained range value is sent to a pending call request. Conclusion: The maximum value (END_VALUE) of each interval value is always used as the value of the current period. Author snoopy7713

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.