Home Database Mysql Tutorial Super detailed explanation of mysql storage engine-InnoDB

Super detailed explanation of mysql storage engine-InnoDB

Aug 27, 2019 pm 03:15 PM
innodb

If you want to see the storage engine used by your database by default, you can use the command:

SHOW VARIABLES LIKE 'storage_engine';
Copy after login

1. InnoDB storage engine

1. InnoDB is the preferred engine for transactional databases

Supports transaction security tables (ACID)

ACID properties of transactions: atomicity, consistency, isolation, and durability

a.Atomicity: Atomicity means that this set of statements is either all executed or not executed at all. If an error occurs halfway through the execution of the transaction, the database will be rolled back to the place where the transaction started. .

Implementation: Mainly based on the redo and undo mechanism of the MySQ log system. A transaction is a set of SQL statements that have functions such as selection, query, and deletion. There will be one node for each statement execution. For example, after the delete statement is executed, a record is saved in the transaction. This record stores when and what we did. If something goes wrong, it will be rolled back to the original position. What I have done has been stored in the redo, and then it can be executed in reverse.

b.Consistency: Before and after the transaction starts and ends, the integrity constraints of the database are not violated. (eg: For example, if A transfers money to B, it is impossible for A to deduct the money but B not receive it)

c.Isolation: At the same time, only one transaction is allowed to request the same data. Different transactions do not interfere with each other;

If isolation is not considered, several problems will occur:

i. Dirty read: refers to reading in a transaction Data in another uncommitted transaction is read during processing (when a transaction is modifying a certain data multiple times, and the multiple modifications in this transaction have not yet been committed, then a concurrent transaction comes to access This data will cause the data obtained by the two transactions to be inconsistent); (read the uncommitted dirty data of another transaction)

ii. Non-repeatable read: In the database For certain data, multiple queries within a transaction range returned different data values. This is because it was modified and submitted by another transaction during the query interval; (the data submitted by the previous transaction was read, and all the queried data were are the same data item)

iii, Virtual reading (phantom reading) : It is a phenomenon that occurs when transactions are not executed independently (eg: transaction T1 reads all rows in a table A data item was modified from "1" to "2". At this time, transaction T2 inserted a row of data items into the table, and the value of this data item was still "1" and submitted to the database. If the user operating transaction T1 looks at the data just modified, he will find that there is still one row that has not been modified. In fact, this row was added from transaction T2, as if he was hallucinating); (the data submitted by the previous transaction is read. , for a batch of data as a whole)

d.Persistence: After the transaction is completed, all updates to the database by the transaction will be saved to the database and cannot be rolled back

2.InnoDB is the default storage engine of mySQL

The default isolation level is RR, and one step further under the RR isolation level, through multi-version concurrency control ( MVCC) solves the non-repeatable read problem, and adds gap lock (that is, concurrency control) to solve the phantom read problem. Therefore, InnoDB's RR isolation level actually achieves the effect of serialization level while retaining better concurrency performance.

MySQL database provides us with four isolation levels:

a, Serializable (serialization): which can avoid dirty reads, non-repeatable reads, and phantom reads occurs;

b, Repeatable read (repeatable read): can avoid the occurrence of dirty reads and non-repeatable reads;

c, Read committed (read committed): can avoid the occurrence of dirty reads;

d, Read uncommitted (read uncommitted): the lowest level, cannot be guaranteed under any circumstances;

From a----d isolation level from high to low, the higher the level, the lower the execution efficiency

3.InnoDB supports row-level locks.

Row-level locks can support concurrency to the greatest extent. Row-level locks are implemented by the storage engine layer.

Lock: The main function of the lock is to manage concurrent access to shared resources and to achieve transaction isolation.

Type: shared lock (read lock), exclusive lock (write lock)

Strength of MySQL locks: table-level locks (low overhead, low concurrency), usually implemented at the server layer

Row-level locks (high overhead, high concurrency), only at the storage engine level Implementation

4. InnoDB is designed for maximum performance for processing huge amounts of data.

Its CPU efficiency may be unmatched by any disk-based relational database engine

5. InnoDB storage engine is fully integrated with the MySQL server

The InnoDB storage engine maintains its own buffer pool for caching data and indexes in main memory. InnoDB places its tables and indexes in a logical table space, and the table space can contain several files (or original disk files);

6, InnoDB supports complete foreign keys Sexual restraint

When storing data in a table, each table is stored in the order of the primary key. If the primary key is not displayed in the table definition, specify the primary key. InnoDB will generate a 6-byte ROWID for each row and use it as the primary key

7. InnoDB is used in many large database sites that require high performance

8. The number of rows in the table is not saved in InnoDB (eg: when selecting count(*) from table, InnoDB needs to scan the entire table to calculate how many rows there are); when clearing the entire table, InnoDB is one row Deletion of one row is very slow;

InnoDB does not create a directory. When using InnoDB, MySQL will create a 10MB automatically extended data file named ibdata1 and two named ib_logfile0 in the MySQL data directory. and the 5MB log file of ib_logfile1

2. The underlying implementation of the InnoDB engine

InnoDB has two storage files, and the suffix names are .frm and .idb. ; Among them, .frm is the definition file of the table, and .idb is the data file of the table.

1. The InnoDB engine uses the B Tree structure as the index structure

B-Tree (balanced multi-path search tree): a balanced search tree designed for external storage devices such as disks

When the system reads data from the disk to the memory, the basic unit is the disk block. Data located in the same disk block will be read out at once, rather than on demand.

InnoDB storage engine uses pages as data reading units. Pages are the smallest unit of disk management. The default page size is 16k.

The storage space of a disk block in the system is often not that large. , so every time InnoDB applies for disk space, it will use several consecutive disk blocks with addresses to reach the page size of 16KB.

InnoDB will use pages as the basic unit when reading disk data into the disk. When querying data, if each piece of data in a page can help locate the location of the data record, this will reduce The number of disk I/Os improves query efficiency.

The data in the B-Tree structure allows the system to efficiently find the disk block where the data is located.

Each node in the B-Tree can contain a large amount of keyword information and branches according to the actual situation. Example

Super detailed explanation of mysql storage engine-InnoDB

Each node occupies one disk block of disk space. There are two ascending-order keys on a node and three pointers to the root node of the subtree. The pointers What is stored is the address of the disk block where the child node is located.

Take the root node as an example. The keywords are 17 and 35. The data range of the subtree pointed by the P1 pointer is less than 17. The data range of the subtree pointed by the P2 pointer is 17----35. The data range of the P3 pointer is 17----35. The data range of the pointed subtree is greater than 35;

Simulate the process of searching for keyword 29:

a. Find disk block 1 according to the root node and read it into the memory. [Disk I/O operation for the first time]

b. Compare keyword 29 in the interval (17,35) and find the pointer P2 of disk block 1;

c. Find it based on the P2 pointer Disk block 3, read into memory. [Disk I/O operation for the second time]

d. Compare keyword 29 in the interval (26, 30) and find the pointer P2 of disk block 3;

e. Find based on the P2 pointer Disk block 8, read into memory. [The third disk I/O operation]

f. Find keyword 29 in the keyword list in disk block 8.

MySQL’s InnoDB storage engine is designed to use the root The node is resident in memory, so the depth of the tree should not exceed 3, that is, I/O does not need to exceed three times;

Analyzing the above results, it is found that three disk I/O operations and three memory searches are required operate. Since the keywords in the memory are an ordered list structure, binary search can be used to improve efficiency; three disk I/O operations are the decisive factor affecting the entire B-Tree search efficiency.

B Tree

B Tree is an optimization based on B-Tree, making it more suitable for implementing external storage index structures. Each B-Tree There are keys and data in the nodes, and the storage space of each page is limited. If the data data is large, the number of keys that can be stored in each node (that is, one page) will be very small. When the amount of data stored is large, the depth of the B-Tree will also be larger, which will increase the number of disk I/Os during query, thus affecting query efficiency.

In B Tree, all data record nodes are stored on leaf nodes of the same layer in order of key value. Only key value information is stored on non-leaf nodes, which can greatly increase the storage capacity of each node. The number of key values ​​reduces the height of B Tree;

Super detailed explanation of mysql storage engine-InnoDB

Usually there are two head pointers on B Tree, one points to the root node and the other points to the leaf node with the smallest key , and there is a chain ring structure between all leaf nodes (ie data nodes).

Therefore, two search operations can be performed on B Tree, one is a range search and paging search for the primary key, and the other is a random search starting from the root node.

B Tree in InnoDB

InnoDB is a data storage indexed by ID

There are two data storage files using the InnoDB engine, one is the definition file and the other is the data document.

InnoDB builds an index on the ID through the B Tree structure, and then stores the record in the leaf node

Super detailed explanation of mysql storage engine-InnoDBIf the indexed field is not the primary key ID, create an index for the field, then store the primary key of the record in the leaf node, and then find the corresponding record through the primary key index.

For more related questions, please visit the PHP Chinese website: PHP Video Tutorial

The above is the detailed content of Super detailed explanation of mysql storage engine-InnoDB. 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

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

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

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