当前位置:网站首页>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
边栏推荐
- Enclosed please find. Net Maui's latest learning resources
- Traps in the explode function in PHP
- Clion configures Visual Studio (MSVC) and JOM multi-core compilation
- 【案例】元素的显示与隐藏的运用--元素遮罩
- 股票开户选择哪家证券公司比较好哪家平台更安全
- Golang(1)|从环境准备到快速上手
- vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
- Opérations de lecture et d'écriture pour easyexcel
- 显示屏DIN 4102-1 Class B1防火测试要求
- 获取前一天的js(时间戳转换)
猜你喜欢

Research and development efficiency improvement practice of large insurance groups with 10000 + code base and 3000 + R & D personnel

leetcode:1139. The largest square bounded by 1

Opérations de lecture et d'écriture pour easyexcel

从架构上详解技术(SLB,Redis,Mysql,Kafka,Clickhouse)的各类热点问题

Introduction of ArcGIS grid resampling method

基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)

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

MySQL 千万数据量深分页优化, 拒绝线上故障!

XML modeling

ArcGIS栅格重采样方法介绍
随机推荐
Aitm 2-0003 horizontal combustion test
Careercup its 1.8 serial shift includes problems
Is Kai Niu 2980 useful? Is it safe to open an account
Realize the function of verifying whether the user has completed login when browsing the page
模式-“里氏替换原则”
Selenium finds the contents of B or P Tags
Hdu2377bus pass (build more complex diagram +spfa)
Binary search
[case] Application of element display and hiding -- element mask
Traps in the explode function in PHP
Introduction to TS, constructor and its this, inheritance, abstract class and interface
Deep merge object deep copy of vant source code parsing
vant 源码解析之 utils/index.ts 工具函数
【日常训练】729. 我的日程安排表 I
2022-07-03-CKA-粉丝反馈最新情况
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
Golang(1)|从环境准备到快速上手
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
使用WebAssembly在浏览器端操作Excel
基于 Ingress Controller 在集群外访问 Zadig 自测环境(最佳实践)