Files
rikako-note/mysql/mysql文档/innodb体系结构.md
2024-11-19 12:49:31 +08:00

69 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# innodb体系结构
## innodb体系结构
innodb体系结构由如下部分构成
- innodb存储引擎内存池
- 后台线程
- 磁盘上的文件数据
innodb内存池由多个内存块构成内存快负责如下功能
- 维护进程、线程访问的内部数据结构
- 对磁盘上的文件数据进行缓存(加快读取速度),同时缓存对磁盘数据的修改
- redo log缓冲
在innodb存储引擎中后台线程主要负责刷新内存池中的数据保证内存缓存为最新状态。此时后台线程还负责将内存池中的修改刷新到磁盘中。
## 后台线程
innodb采用多线程模型存在多个后台线程每种后台线程负责不同的后台任务。
### Master Thread
Master Thread主要负责将内存池中的缓存数据异步刷新到磁盘中包括脏页的刷新、合并`插入缓冲`insert bufferundo页的回收等。
### IO Thread
innodb中使用AIO来处理IO请求IO Thread主要则用来处理异步IO的回调其中`innodb_read_io_threads``innodb_write_io_threads`默认均为4个。
```sql
show variables like 'innodb_%_io_threads'
```
|Variable_name|Value|
|-------------|-----|
|innodb_read_io_threads|4|
|innodb_write_io_threads|4|
### Purge Thread
当事务被提交之后其对应的undo log不再会被需要需要Purge Thread来回收已经使用的undo页。
可以设置多个purge threads默认情况下mysql 8的purge threads为4个
```sql
show variables like 'innodb_purge_threads'
```
|Variable_name|Value|
|-------------|-----|
|innodb_purge_threads|4|
### Page Cleaner Thread
page cleaner thread将脏页刷新的任务放到单线程中来完成从而减轻原Master Thread的工作以及减少对用户查询线程的阻塞。
## 内存
### 缓冲池
innodb存储引擎是基于磁盘存储的并将记录基于页的方式进行管理。为了提升数据库系统的读写性能通常采用缓冲池来提升数据库的整体性能。
> ### 缓冲池原理
> #### 读缓冲
> 在数据库读取磁盘上的页面时,会将丛磁盘上读取到的页存放到缓冲池中,后续再读取相同的页数据时,先丛缓冲池中查找。如果缓冲池中存在该页,直接从缓冲池中读取。
> #### 写缓冲
> 在数据库针对磁盘上的页数据进行修改时,首先会尝试修改缓冲池中的页数据,并且,缓冲池中的页数据会定期刷新到磁盘中。
>
> 缓冲池刷新页到磁盘中的操作由checkpoint机制进行触发并不会在每次更新缓冲池中的页数据后立马触发。
#### 缓冲池参数配置
对于innodb其缓冲池大小通过`innodb_buffer_pool_size`来配置。默认情况下,`innodb_buffer_pool_size`大小为128M.
```sql
show variables like 'innodb_buffer_pool_size'
```
|Variable_name|Value|
|-------------|-----|
|innodb_buffer_pool_size|134217728|