当前位置:网站首页>MySQL InnoDB Architecture Principle
MySQL InnoDB Architecture Principle
2022-07-05 21:09:00 【A little dog】
MySQL InnoDB Framework principle

1. Architecture analysis
InnoDB It is mainly divided into : Structure in memory and Structure on disk
The memory structure is mainly : Buffer Pool (change Buffer 、adaptive hash index) log buffer
The disk structure is divided into : Tablespaces and redo logs
Table space :InnoDB The data dictionary 、 Double write buffer 、 Modify cache 、Undo journal .
Redo log : What's stored is Log buffer Data flushed to disk .
(.idb、.frm、undo.log、( Double write buffer (doublewrite buffer), Failure recovery mechanism (crash recovery))、 Locking mechanism 、Redo.log、)
2. InnoDB The structure in memory
Buffer Pool、(Change Buuffer、Adaptive Hash Index Occupied memory belongs to Buffer Pool)、Log Buffer
2.1 Buffer Pool
Mysql The server 80% Of memory will be allocated to Buffer Pool.
InnoDB The minimum unit of data management in is page , Default per page 16KB.
Through a page linked list , Less pages accessed will pass through the cache pool LRU The algorithm is eliminated .
Realization LRU Linked list
But different from the normal linked list , The buffer pool chain table is divided into 2 part ,New sublist( Default occupation 5/8 The cache pool ) and Old sublist( Default occupation 3/8) The cache pool ( adopt innodb_old_blocks_pct modify , The default value is 37)
The newly read page will be added Old sublist, And if the Old sublist If you visit the page in times , Will move to New sublist The head of .
2.1.1 The page list is not full , Add a new page

2.1.2 Old sublist Page in Be visited here

2.1.3 The page list is full , Add a new page , Trigger LRU Algorithm

2.2 Change Buffer
To reduce nonclustered indexes ( Secondary indexes ), Each modification is written directly to disk , There will be a lot of frequent IO.
Change buffer take Not unique The operation of the auxiliary index page is cached . If the secondary index page is already in the cache , It can be modified directly ( There is no need to IO), If not , Save the changes to Change Buffer, So as to merge and write disk files regularly, such as .idb.

2.3 Adaptive Hash Index
Although database record storage is not used hash surface , However, adaptive hash index is used for page lookup in memory (AHI) The general time complexity is O(1), As a way to reduce query data ( Indexes ) Page method .
2.4 LogBuffer
Log Buffer yes Redo log (Redo log) Buffer in memory , The default is 16M
2.4.1 Log first strategy (Redo log The role of )
InnoDB The log first strategy is used , Before persisting data files , Ensure that transactions are recorded as Redo log Written to disk .
Problem description : After logging to the database , The corresponding transaction can be returned to the user , Indicates that the transaction is complete . actually , The data may only be modified in memory , Did not brush to disk . If the program is abnormal or fails , The data in memory will disappear .
solve the problem : InnoDB At this point through Redo log To ensure data consistency . Obviously, it is not realistic to record all redo logs . The checkpoint mechanism is introduced , Ensure that the log has been written to disk , The next reply starts from the checkpoint .
2.4.2 Recover data from downtime ( from Redo Log and Log Buffer)
When data is modified , Will write memory space first , And then write Log Buffer, Will be log buffer The log of changes recorded in is written to the physical disk redo log In file .
However, the downtime data is written into memory , But it is not stored in the physical storage structure . At this time, the checkpoint mechanism will be used checkPoint, take redo log The data in the file is loaded into redo log buffer In the memory . Let's talk about the latest data writing mysql Disk data file , To update the data .
3. InnoDB Physical disk structure
3.1 Table space
3.1.1 InnoDB The data dictionary
After creating a database and a table , You can see test Database directory .
db.opt file : Saved data test Default character set
t.frm file : Save the data dictionary information of the table
t.idb file : Table data and index ( initialization 96K, The default page is 16K)
3.1.2 Undo journal ( To achieve atomicity Transaction commit rollback , Realization MVCC)
MySQL Of MVCC( Multi version concurrency control ) rely on Undo Log Realization .Mysql Table space file of t.ibd The latest value of the stored record . Each record has a rollback pointer ( See the picture above Roll Ptr), Point to the last entry in the record Undo Record , And each one Undo The record will point to the previous one Undo Record .

Undo Log During the execution of a transaction , Transactions are persisted only after they are committed , If the transaction is rolled back Undo Log It will also delete .
3.1.3 Double write cache (Double-Write Buffer)
When InnoDB After the record is updated , Because the log first strategy will go first Log Buffer Write data in memory , And then write to disk Redo log in .
Then, before writing to the physical table disk, it will enter the double write cache , At this time, if the writing process fails ( For example, the machine power down or the operating system crashes ).
3.1.3.1 Error writing to double write cache

3.1.3.2 Error writing to physical table file

4. InnoDB ACID Model
4.1 Atomicity
Mainly through the transaction mechanism
COMMITHE ROLLBACK sentence ( adopt Undo log Realization )
4.2 Consistency
Consistency assurance
InnoDB Double write buffer for (double write buffer Consistency assurance of disk storage )
InnoDB The failure recovery mechanism of (Redo log Consistency guarantee of data submission ,Undo log Consistency guarantee of transaction submission ,bin log Ensure the consistency and persistence of master-slave data )
4.3 Isolation
Isolation,
The isolation level of the transaction
InnoDB Locking mechanism
4.4 Durability
Durability guarantee
redo log Cache to disk
double write buffer Double write cache guarantee Data to disk
bin log Ensure the consistency and persistence of master-slave data
Please correct me if there is any mistake
边栏推荐
猜你喜欢

EasyExcel的读写操作

示波器探头对测量带宽的影响

Open source SPL eliminates tens of thousands of database intermediate tables

【案例】定位的运用-淘宝轮播图

Clion-MinGW编译后的exe文件添加ico图标

leetcode:1139. The largest square bounded by 1

事项研发工作流全面优化|Erda 2.2 版本如“七”而至

leetcode:1755. Sum of subsequences closest to the target value

LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line

Clion configures Visual Studio (MSVC) and JOM multi-core compilation
随机推荐
SQL series (basic) - Chapter 2 limiting and sorting data
WPF gets the control in the datagridtemplatecolumn of the specified row and column in the DataGrid
hdu2377Bus Pass(构建更复杂的图+spfa)
AITM2-0002 12s或60s垂直燃烧试验
Aitm2-0002 12s or 60s vertical combustion test
Careercup its 1.8 serial shift includes problems
Generics of TS
selenium 查找b或p标签的内容
CLion配置visual studio(msvc)和JOM多核编译
postgres 建立连接并删除记录
启牛2980有没有用?开户安全吗、
EasyExcel的读写操作
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
Introduction of ArcGIS grid resampling method
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
selenium 获取dom内验证码图片
ts 之 类的简介、构造函数和它的this、继承、抽象类、接口
Test of incombustibility of cement adhesives BS 476-4
Comparison table of foreign lead American abbreviations
终端安全能力验证环境搭建和渗透测试记录