Take you to understand the database buffer pool (Buffer Pool) in MySQL
For tables using the InnoDB storage engine, storage space is managed in units of pages, as the basic granularity for swapping in and out between memory and disk. When we load a page from disk into memory, disk I/O will be performed. The overhead of disk I/O greatly affects the overall performance. If we read the corresponding page directly from the memory, wouldn't it reduce the performance loss caused by disk I/O and the efficiency will be improved a lot. Based on this, Buffer Pool (
Buffer Pool
) appeared, so next, let’s talk about the Buffer Pool in InnoDB.
Buffer Pool
Some people may think that since the buffer pool is so good, why not just store all the data in the buffer pool? No, no, no , The buffer pool is a continuous piece of memory allocated by the operating system. Memory has a much smaller capacity than disk and is expensive. So how much memory will the operating system allocate to the buffer pool?
- By default, the size of the buffer pool is 128MB;
Of course, if your machine has a very large memory capacity, you can configure the startup option parameters in the configuration file innodb_buffer_pool_size
The unit is bytes, and the minimum cannot be less than 5MB.
The internal structure of the buffer pool
The buffer pool divides the continuous memory allocated by the operating system into several pages (buffer pages) with a default size of 16KB [At this time, there is no actual The disk page is cached in the Buffer Pool]. When we swap a page from the disk into the buffer pool, how do we allocate the location? Therefore, some control information is needed to identify the buffer pages in these buffer pools. This control information is stored in a memory area called a control block and corresponds to the buffer page one-to-one. The size of the control block is also fixed. Therefore, in this continuous memory space, memory fragmentation will inevitably occur. In summary, the internal structure of the buffer pool is as follows:
- Buffer page
- Control block: page number, address of the buffer page in the buffer pool, linked list node information, etc.
- Memory fragmentation [If memory is allocated properly, memory fragmentation is dispensable]
Buffer pool management
Above The linked list node information is mentioned in the control block, so what are the linked list nodes used for? It is to better manage the pages in the buffer pool. The linked list is used to link control blocks, because there is a one-to-one correspondence between control blocks and buffer pages.
1) Free linked list
Links the control blocks corresponding to all free buffer pages to form a linked list.
Solution to the problem: When swapping a page from the disk into the buffer pool, how to distinguish which page in the buffer pool is free? With the free linked list, when a disk page is swapped into the buffer pool, a free buffer page is obtained directly from the free linked list, and the corresponding information in the disk page is filled in the control block corresponding to the buffer page, and then Just delete the control block from the free linked list.
2) Update linked list
If the data of the buffer page in the buffer pool is modified, causing it to be inconsistent with the data on the disk, the page is called a dirty page. Link the control blocks corresponding to all dirty pages to form an update linked list, and refresh the data of the corresponding cache page to the disk at a certain time in the future based on this linked list.
3) LRU linked list
The size of the buffer pool is limited. If the cached pages exceed the size of the buffer pool, that is, there are no free buffer pages. When there are new pages to be added, When entering the buffer pool, the LRU strategy is adopted to remove old buffer pages from the buffer pool, and then add new pages. Since the LRU linked list involves a lot of content, we will introduce it separately next.
The "philosophy" contained in the LRU linked list
Let me first mention the pre-reading mechanism
The optimization mechanism on I/O. As the name suggests, pre-reading will asynchronously These pages are loaded into the buffer pool and are expected to be needed soon. These requests introduce all pages in a range, which is the so-called locality principle
. The purpose is to reduce the disk I/O.
Before understanding the read-ahead mechanism, let’s review the InnoDB logical storage unit: tablespace → segment → extent → page. Specifically mention the area, which will be used later: an area is a continuous 64 pages
in physical location, that is, the size of an area is 1MB.
The pre-reading mechanism can be subdivided into the following two types:
- Linear read-ahead: A technique that predicts which pages may be needed soon based on sequentially accessed pages in the buffer pool. By configuring the parameter innodb_read_ahead_threshold, if the pages of a certain area accessed sequentially exceed the value of this parameter, an asynchronous read request will be triggered to read all the pages in the next area into the buffer pool.
- Random read-ahead: Can predict when pages may be needed based on pages already in the buffer pool, regardless of the order in which those pages are read. If 13 consecutive pages of the same extent are found in the buffer pool, InnoDB will asynchronously issue a request to prefetch the remaining pages of the extent. Random reading is controlled by configuring the variable innodb_random_read_ahead. How does traditional LRU manage buffer pages?
Use the LRU algorithm to manage the least recently used buffer pages and form a corresponding linked list for easy elimination.
When a page is accessed [that is, the most recent access]
The page is in the buffer pool, the corresponding control block is moved to the head of the LRU linked list- The page is not in the buffer pool In the buffer pool, the least recently used page at the end is eliminated, the page is loaded from the disk and placed at the head of the LRU linked list
- So why doesn't InnoDB use such an intuitive LRU algorithm? The reasons are as follows:
- Read ahead failure
The pages read ahead into the buffer pool will be placed at the head of the LRU linked list, but many of them The page may not be read.
- Buffer pool pollution
Loading many pages with low frequency into the buffer pool will remove pages with high frequency from the buffer Eliminated from the pool. For example,
full table scan How does the optimized LRU manage buffer pages?
Based on the above shortcomings, the specific optimized method divides the traditional LRU linked list into two parts: hot data area [young area] & cold data area [old area]
- Hot data area [Young area]
- : Buffer page with high frequency of use Cold data area [Old area]
- : Area with low use frequency
As shown in the figure, the hot data area and the cold data area occupy different proportions respectively, then we can start it through
innodb_old_blocks_pct option to control the proportion of cold data area
.
When a page is loaded into the buffer pool for the first time, the control block at the end of the cold data area is first eliminated (that is, its corresponding page is eliminated), and then the control block corresponding to the new page is eliminated. Blocks will be placed at the head of the cold data area first.
- If the page is not accessed subsequently, it will be slowly eliminated from the cold data area. Generally, it will not affect the frequently accessed buffer pages in the hot data area.
Let me talk about the conclusion first. This problem has not been well optimized. The reasons are as follows [take full table scan as an example]:
A page visited for the first time will also be Put it at the head of the cold data area, but subsequent access will put it at the head of the hot data area, which will also crowd out pages with higher access frequency.- So how to solve the problem of buffer pool pollution?
- Similarly, the window value can be set through the innodb_old_blocks_time
- parameter [unit ms]. The default is 1000ms, and 1s will filter out most operations such as full table scans. For example, during a full table scan, the time interval between multiple accesses to a page will not exceed 1 second.
The buffer pool will try to save frequently used data. When MySQL performs a page read operation, it will first determine whether the page is in the buffer pool. If it exists, it will be directly Read, if it does not exist, the page will be stored in the buffer pool through memory or disk and then read.
- Query caching is to cache the query results in advance, so that you can get the results directly without executing them next time. It should be noted that the query cache in MySQL does not cache the query plan, but the corresponding results of the query. The hit conditions are strict, and as long as the data table changes, the query cache will become invalid, so the hit rate is low.
- [Related recommendations:
The above is the detailed content of Take you to understand the database buffer pool (Buffer Pool) in MySQL. For more information, please follow other related articles on the PHP Chinese website!

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools