Home Database Mysql Tutorial Detailed example of innodb_autoinc_lock_mode method

Detailed example of innodb_autoinc_lock_mode method

May 24, 2017 pm 01:47 PM
innodb mode

The following editor will bring you an introduction to MySQL innodb_autoinc_lock_mode. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

innodb_autoinc_lock_mode This parameter controls the behavior of related locks when inserting data into a table with an auto_increment column;

By setting it, you can achieve performance and Balance of security (master-slave data consistency)

[0] Let’s classify insert first

First of all, insert can generally Divided into three categories:

1. Simple insert such as insert into t(name) values('test')

2. Bulk insert such as load data | insert into. .. select .... from ....

3. mixed insert such as insert into t(id,name) values(1,'a'),(null,'b'),(5,'c');

【1】Description of innodb_autoinc_lock_mode

innodb_auto_lockmode has three values :

1. 0 means tradition.

2. 1 means coherent.

3. 2 means interleaved.

##【1.1】tradition(innodb_autoinc_lock_mode=0) mode:

1. It provides a backward compatibility capability


2. In this mode, all insert statements ("insert like") must obtain a table-level auto_inc lock at the beginning of the statement, and release the lock at the end of the statement. Pay attention. , what we are talking about here is statement level rather than transaction level. A transaction may contain one or more statements.


3. It can ensure the predictability, continuity and repeatability of value distribution. This also ensures that the insert statement can be generated the same as the master when copied to the slave. value (which ensures the safety of statement-based replication).


4. Since the auto_inc lock is maintained until the end of the statement in this mode, this affects concurrent insertion.

【1.2】consecutive(innodb_autoinc_lock_mode=1) Mode:

1. In this mode, simple insert has been optimized. Since simple insert The number of values ​​inserted at one time can be determined immediately, so MySQL can generate several consecutive values ​​at a time for this insert statement; in general, this is also safe for replication (it ensures the safety of statement-based replication)


2. This mode is also the default mode of mysql. The advantage of this mode is that the auto_inc lock does not remain until the end of the statement. As long as the statement gets the corresponding value, the lock can be released in advance

【1.3】interleaved(innodb_autoinc_lock_mode=2) mode

1. Since there is no auto_inc lock in this mode, so in this mode The performance is the best; but it also has a problem, that is, the auto_incremant values ​​it obtains for the same statement may not be continuous.

[2] If your binary file format is mixed | row, then any of these three values ​​​​is copy-safe for you.

Since mysql now recommends setting the binary format to row, it is best to use innodb_autoinc_lock_mode=2 when binlog_

format is not a statement. This may be known. Better performance.

Finally end with an example about auto_increment

Example:

Don’t worry about itUpdateThe value of an auto_increment column

Step 1: Reproduce the scene


create table t(x int auto_increment not null primary key);
insert into t(x) values(0),(null),(3);
select * from t;
+---+
| x |
+---+
| 1 |
| 2 |
| 3 |
+---+
Copy after login

Step 2: Reproduce the SQL that caused the problem


update t set x=4 where x=1;
select * from t;
+---+
| x |
+---+
| 2 |
| 3 |
| 4 |
+---+
Copy after login

The third step: Reproduce the usual expression


insert into t(x) values(0);
ERROR 1062 (23000): Duplicate entry '4' for key 'PRIMARY'
Copy after login

Step 4: Summary of the problem

After executing the first step, mysql knows that the next auto_increment value is 4.

After executing the second step, mysql did not know that 4 had been artificially occupied, so an error occurred when executing the third step.

[Related recommendations]

1.

Mysql free video tutorial

2.

Detailed explanation of the error of table primary key conflict when innodb_index_stats imports data

3.

Detailed example of innodb_autoinc_lock_mode in mysql

4.

Detailed example of adding new user permissions in MySQL

5.

Detailed example of init_connect method in mysql

The above is the detailed content of Detailed example of innodb_autoinc_lock_mode method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

what is mysql innodb what is mysql innodb Apr 14, 2023 am 10:19 AM

InnoDB is one of the database engines of MySQL. It is now the default storage engine of MySQL and one of the standards for binary releases by MySQL AB. InnoDB adopts a dual-track authorization system, one is GPL authorization and the other is proprietary software authorization. InnoDB is the preferred engine for transactional databases and supports transaction security tables (ACID); InnoDB supports row-level locks, which can support concurrency to the greatest extent. Row-level locks are implemented by the storage engine layer.

How MySQL sees InnoDB row format from binary content How MySQL sees InnoDB row format from binary content Jun 03, 2023 am 09:55 AM

InnoDB is a storage engine that stores data in tables on disk, so our data will still exist even after shutdown and restart. The actual process of processing data occurs in memory, so the data in the disk needs to be loaded into the memory. If it is processing a write or modification request, the contents in the memory also need to be refreshed to the disk. And we know that the speed of reading and writing to disk is very slow, which is several orders of magnitude different from reading and writing in memory. So when we want to get certain records from the table, does the InnoDB storage engine need to read the records from the disk one by one? The method adopted by InnoDB is to divide the data into several pages, and use pages as the basic unit of interaction between disk and memory. The size of a page in InnoDB is generally 16

How to handle mysql innodb exception How to handle mysql innodb exception Apr 17, 2023 pm 09:01 PM

1. Roll back and reinstall mysql. In order to avoid the trouble of importing this data from other places, first make a backup of the database file of the current library (/var/lib/mysql/location). Next, I uninstalled the Perconaserver 5.7 package, reinstalled the original 5.1.71 package, started the mysql service, and it prompted Unknown/unsupportedtabletype:innodb and could not start normally. 11050912:04:27InnoDB:Initializingbufferpool,size=384.0M11050912:04:27InnoDB:Complete

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Jul 26, 2023 am 11:25 AM

MySQL storage engine selection comparison: InnoDB, MyISAM and Memory performance index evaluation Introduction: In the MySQL database, the choice of storage engine plays a vital role in system performance and data integrity. MySQL provides a variety of storage engines, the most commonly used engines include InnoDB, MyISAM and Memory. This article will evaluate the performance indicators of these three storage engines and compare them through code examples. 1. InnoDB engine InnoDB is My

How to solve phantom reading in innoDB in Mysql How to solve phantom reading in innoDB in Mysql May 27, 2023 pm 03:34 PM

1. Mysql transaction isolation level These four isolation levels, when there are multiple transaction concurrency conflicts, some problems of dirty reading, non-repeatable reading, and phantom reading may occur, and innoDB solves them in the repeatable read isolation level mode. A problem of phantom reading, 2. What is phantom reading? Phantom reading means that in the same transaction, the results obtained when querying the same range twice before and after are inconsistent as shown in the figure. In the first transaction, we execute a range query. At this time, there is only one piece of data that meets the conditions. In the second transaction, it inserts a row of data and submits it. When the first transaction queries again, the result obtained is one more than the result of the first query. Data, note that the first and second queries of the first transaction are both in the same

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How to use MyISAM and InnoDB storage engines to optimize MySQL performance How to use MyISAM and InnoDB storage engines to optimize MySQL performance May 11, 2023 pm 06:51 PM

MySQL is a widely used database management system, and different storage engines have different impacts on database performance. MyISAM and InnoDB are the two most commonly used storage engines in MySQL. They have different characteristics and improper use may affect the performance of the database. This article will introduce how to use these two storage engines to optimize MySQL performance. 1. MyISAM storage engine MyISAM is the most commonly used storage engine for MySQL. Its advantages are fast speed and small storage space. MyISA

Tips and Strategies to Improve MySQL Storage Engine Read Performance: Comparative Analysis of MyISAM and InnoDB Tips and Strategies to Improve MySQL Storage Engine Read Performance: Comparative Analysis of MyISAM and InnoDB Jul 26, 2023 am 10:01 AM

Tips and strategies to improve the read performance of MySQL storage engine: Comparative analysis of MyISAM and InnoDB Introduction: MySQL is one of the most commonly used open source relational database management systems, mainly used to store and manage large amounts of structured data. In applications, the read performance of the database is often very important, because read operations are the main type of operations in most applications. This article will focus on how to improve the read performance of the MySQL storage engine, focusing on a comparative analysis of MyISAM and InnoDB, two commonly used storage engines.

See all articles