Usage of auto_increment in mysql

Source: Internet
Author: User


Auto increment
You can use the following command to query the auto-increment step of mysql,


Mysql> show variables like 'auto _ inc % ';
+ -------------------------- + ------- +
| Variable_name | Value |
+ -------------------------- + ------- +
| Auto_increment_increment | 1 |
| Auto_increment_offset | 1 |
+ -------------------------- + ------- +

Here, auto_increment_increment is the step of auto-increment, value is 1 indicates + 1 each time, auto_increment_offset indicates the auto-increment offset, that is, the auto-increment starts, and value is 1 indicates increasing from 1.

The InnoDB auto-incrementing primary key is obtained through its own auto-incrementing counter, which is completed through the table lock mechanism.
The table lock is released only after the insertion is complete, that is, after the transaction is completed.

To solve the problem of auto-increment primary key lock tables, innodb_autoinc_lock_mode is introduced, which is achieved through the growth mechanism of lightweight mutex.


Mysql> show variables like 'innodb _ autoinc_lock_mode ';
+ -------------------------- + ------- +
| Variable_name | Value |
+ -------------------------- + ------- +
| Innodb_autoinc_lock_mode | 1 |
+ -------------------------- + ------- +
Innodb_autoinc_lock_mode has three values:

0. Table lock

1. The default value is mutex. It will "pre-apply" redundant values and may be discontinuous.
2. The auto-increment value is discontinuous and the performance is good.
When innodb_autoinc_lock_mode = 1, the redundant id (handler. cc: compute_next_insert_id). After the insert operation is complete, these reserved IDs are especially left blank, specifically, the current maximum id after the pre-application is written back to the table.

Recently, it was found that a data table is not continuous due to frequent insert on duplicate key update. The specific reason is that the data will be pre-allocated with an id, but if the insertion fails to perform the update operation, this id is discarded, and the value is skipped in the next insert operation.

Application

When synchronizing data between the mysql master and the Master (two machines synchronize data with each other), you need to set


Auto_increment_increment = 2
Auto_increment_offset = 1 and 2

In this way, we can avoid conflicts between the values of the auto-increment fields when the two servers perform updates at the same time.

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.