search
HomeDatabaseMysql TutorialMySQL storage engine detailed explanation of InnoDB architecture

This article brings you relevant knowledge about mysql, which mainly introduces the relevant content about the storage engine InnoDB architecture. InnoDB is the default engine of MySQL, a storage engine that supports transaction security. , let’s take a look at it, I hope it will be helpful to everyone.

MySQL storage engine detailed explanation of InnoDB architecture

Recommended learning: mysql video tutorial

The current MySQL8.x version database already supports many storage engines, but generally we There are only a few commonly used ones. It is easy to form a fixed mindset and not easily adopt other storage engines, thus missing many functions of optimizing storage. Therefore, it is worth learning to have a clear understanding of the functions of the nine currently supported database storage engines. This article clearly explains the functions, functions and usage scenarios of these eight database storage engines.

This series of articles will be included in my column - Quickly Learn Various SQL Database Operations, which basically covers all aspects of using SQL to handle daily business, conventional query database analysis, and complex operations. From the basic steps of building databases and tables to handling various complex database operations, as well as professional explanations of common SQL functions, a lot of time and effort have been spent on creation. If you have friends who need to engage in data analysis or data development, I recommend subscribing to the column. Learn the most practical and commonly used knowledge in the first time. This blog is quite long and deserves careful reading and practice. I will select the best parts and explain the practice in detail. The blogger will maintain the blog post for a long time. If you have any errors or doubts, you can point them out in the comment area. Thank you for your support.

1. Supported storage engines

Enter the MySQL database to view the storage engine and you can see all the storage engines supported by the MySQL database:

SHOW ENGINES

MySQL storage engine detailed explanation of InnoDB architecture

Currently there is an engine Federated does not support, we only need to be clear about the other eight database storages.

Common database engines in MySQL include MyISAM, InnoDB, and Memory. So let’s first understand these three engines.

2. InnoDB engine

InnoDB is the default engine of MySQL, a storage engine that supports transaction security. Data in MySQL is stored on the physical disk, and the actual data processing is performed in memory. Since the read and write speed of the disk is very slow, if the disk is frequently read and written for each operation, the performance will be very poor.

In order to solve the above problems, InnoDB divides the data into several pages, using pages as the basic unit of interaction between disk and memory. The general page size is 16KB. In this case, at least 1 page of data is read into memory or 1 page of data is written to disk at a time. Improve performance by reducing the number of interactions between memory and disk.

This is essentially a typical cache design idea. Generally, cache design is basically considered from the time dimension or space dimension:

  • Time dimension: If a piece of data is being used, it will most likely be used again in the next period of time. It can be considered that hotspot data caching belongs to the implementation of this idea.

  • Spatial dimension: If a piece of data is being used, there is a high probability that the data stored near it will also be used soon. InnoDB's data pages and operating system's page cache are the embodiment of this idea.

The following is the official InnoDB engine structure diagram, which is mainly divided into two parts: memory structure and disk structure.

MySQL storage engine detailed explanation of InnoDB architecture

The memory structure mainly includes four components: Buffer Pool, Change Buffer, Adaptive Hash Index and Log Buffer.

1.Buffer Pool

Buffer Pool consists of data, index, insert buffer, adaptive hash index, lock information and data dictionary. Buffer pool, referred to as BP. BP is based on Page, with a default size of 16K. The bottom layer of BP uses a linked list data structure to manage Pages. When InnoDB accesses table records and indexes, they will be cached in the Page page. Later use can reduce disk IO operations and improve efficiency.

The buffer pool is simply a memory area that uses the speed of memory to compensate for the impact of slow disk speed on database performance. When reading a page in a database, the page read from the disk is first stored in the buffer pool. This process is called "FIX" the page in the buffer pool. The next time the same page is read, first determine whether the page is in the buffer pool. If it is in the buffer pool, the page is said to be hit in the buffer pool. Read the page directly. Otherwise the page on disk is read. For the modification operation of pages in the database, the pages in the buffer pool are first modified, and then refreshed to the disk at a certain frequency. What needs to be noted here is that the operation of flushing pages from the buffer pool back to the disk is not triggered every time the page is updated, but is flushed back to the disk through a mechanism called Checkpoint. Again this is to improve the overall performance of the database.

Traditional LUR algorithm

The buffer pool is managed through the LRU (Latest Recent Used, least recently used) algorithm, that is, the most frequently used pages At the front of the LRU list, and the least used page is at the end of the LRU list. When the buffer pool cannot store the newly read page, the page at the end of the LRU list is first released:

(1) page If it is already in the buffer pool, then only the action of "moving" to the LRU head is performed, and no page is eliminated;

(2) The page is not in the buffer pool, except for "putting" the page into the LRU head. Action, but also to "eliminate" the LRU tail page;

But InnoDB's LUR algorithm is not a traditional LUR algorithm.

There are two problems here:

(1) Pre-reading failure;

(2) Buffer pool pollution;

Let’s first understand what pre-reading is Read;

Read ahead

Disk reading and writing is not reading on demand, but reading by page, at least one page at a time Page data (usually 4K), if the data to be read in the future is in the page, subsequent disk IO can be omitted and efficiency improved. Data access usually follows the principle of "concentrated reading and writing". When using some data, there is a high probability that nearby data will be used. This is the so-called "locality principle", which shows that early loading is effective and can indeed reduce disk IO.

Read-ahead failure

Due to read-ahead (Read-Ahead), the page was put into the buffer pool in advance, but in the end MySQL did not Reading data from the page is called a read-ahead failure.

To optimize the read-ahead failure, the idea is:

(1) Let the page that failed to read-ahead stay in the buffer pool LRU for as short a time as possible;

( 2) Let the pages that are actually read be moved to the head of the buffer pool LRU;

to ensure that the hot data that is actually read stays in the buffer pool as long as possible.

The specific method is:

(1) Divide the LRU into two parts:

New generation (new sublist)

Old generation (old sublist) )

(2) The ends of the new and old generations are connected, that is: the tail of the new generation is connected to the head of the old generation;

(3) New page (for example, by When the pre-read page is added to the buffer pool, it is only added to the head of the old generation:

If the data is actually read (the pre-read is successful), it will be added to the head of the new generation

If the data has not been read, it will be eliminated from the buffer pool earlier than the "hot data pages" in the new generation

The improved version of LRU in the new and old generations still cannot solve the problem of buffer pool pollution.

2.Log Buffer

Log Buffer is used to cache redo logs.

InnoDB has two very important logs: undo log and redo log

(1) Through undo log, you can see earlier versions of data, implement MVCC, or rollback transactions and other functions.

(2) Use redo log to ensure transaction durability.

MySQL storage engine detailed explanation of InnoDB architecture

#The redo log buffer is a memory storage area used to save data to be written to the log file on disk. The log buffer size is defined by the innodb_log_buffer_size variable, and the default size is 16MB.

The contents of the log buffer are periodically flushed to disk. A larger log buffer allows large transactions to be run without redo log data being written to disk before the transaction commits. Therefore, if there are transactions that update, insert, or delete many rows, increasing the log buffer size can save disk I/O.

innodb_flush_log_at_trx_commit: Controls how the contents of the log buffer are written and flushed to disk.

innodb_flush_log_at_timeout: Control the log refresh frequency.

You need to observe transactions if disk I/O is causing performance issues, such as transactions involving many BLOB entries. The InnoDB log buffer is flushed to disk whenever it is full, so increasing the buffer size can reduce I/O.

The default number of log files is two: ib_logfile0 and ib_logfile1.

The log has a fixed size, and the default size depends on the MySQL version.

3.Adaptive Hash Index

Adaptive Hash IndexThe adaptive hash index is a key-value pair storage structure that stores the records where the hot pages are located. The InnoDB storage engine automatically creates hash indexes for certain pages based on the frequency and pattern of access.

MySQL storage engine detailed explanation of InnoDB architecture

#The above picture is the difference between B-tree index and adaptive hash index. Disable or enable this feature through the parameter innodb_adaptive_hash_index, which is enabled by default.

4.Change Buffer

Change Buffer: Data in MySQL is divided into two parts: memory and disk; cache hot data pages and index pages in the buffer pool to reduce disk reads; through change Buffer is a means to ease disk writing.

When a data page needs to be updated, update it directly if the data page is in memory. If the data page is not in memory. Without affecting data consistency, InooDB will cache these update operations in the change buffer, so that there is no need to read this data page from disk. When the next query needs to access this data page, read the data page into memory, and then perform operations related to this page in the change buffer. In this way, the correctness of the data logic can be ensured.

Although the name is called change buffer, it is actually data that can be persisted. In other words, the change buffer has a copy in memory and will also be written to disk (ibdata).

The process of merging the operations in the change buffer to the original data page and obtaining the latest results is called merge . The following situations will trigger merge:

  • Access this data page;

  • The background master thread will merge regularly;

  • When the database buffer pool is not enough;

  • When the database is shut down normally;

  • When the redo log is full;

The change buffer means that when a non-unique ordinary index page is not in the buffer pool and a write operation is performed on the page, the record change buffer will be buffered first, and then the change buffer will be changed when the future data is read. The technology in the operation merge to the original data page. Before MySQL 5.5, it was called insert buffer, and it was only optimized for insert. Now it is also valid for delete and update, and it is called change buffer.

Recommended learning: mysql video tutorial

The above is the detailed content of MySQL storage engine detailed explanation of InnoDB architecture. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金. If there is any infringement, please contact admin@php.cn delete
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool