MySQL architecture and storage engine

Magic conch 2021-01-21 00:37:32
mysql architecture storage engine


MySQL Architecture

It's divided into two layers :

  1. MySQL Server layer : adjoining course ( signal communication 、 Threads 、 Password authentication ) and SQL layer ( Authority judgment 、 The query cache 、 Parser 、 Preprocessing 、 cache 、 Implementation plan )
  2. Storage engine layer

For a SQL To deal with

Authority judgment , Whether you have access to a library or table , The query cache , If in Query Cache in , Then return to the client directly , Otherwise, use Parser Judge the correctness of grammar , Preprocessing Dealing with semantics that cannot be parsed , Generate the best Implementation plan , Access data through the storage engine layer .

Query Cache stay 5.6 after , Off by default .

Database stress testing tools :sysbench

Storage engine comparison

InnoDB And MyISAM contrast

difference InnoDB MyISAM
Transaction support Support transactions Unsupported transaction
Lock granularity Row lock Table locks
concurrency High concurrency Low concurrent
Structure and indexing mechanism Data and indexes exist .idb file , And they are all cached in memory data .MYD Indexes .MYI, Cache index only files
select count(*) Need a full table scan , The statistical number of rows Just read the number of trips from the counter

InnoDB Architecture

The architecture consists of Disk files Memory structure Threads It's made up of three layers .

InnoDB Storage structure

Table space

Define table space path 、 Initial size ( Default 10m)、 Automatic expansion strategy ( Default 64m)

Innodb_data_file_path

The initial size is recommended to be 1G.

mysql> show variables like '%file_path%'
-> ;
+----------------------------+------------------------+
| Variable_name | Value |
+----------------------------+------------------------+
| innodb_data_file_path | ibdata1:12M:autoextend |
| innodb_temp_data_file_path | ibtmp1:12M:autoextend |
+----------------------------+------------------------+

By default, the current version uses Independent tablespace files , That is, each table has its own table space file , It's not stored in ibdata1 in . For example, establish test-lgl library , And new student surface , In the path of more test@002dlgl Folder , among student.ibd Corresponding student Independent table space file for tables :

[root@localhost db]# pwd
/home/lgl/docker/mysql/mysql_3306/db
[root@localhost db]# ll
 Total usage  188480
-rw-r-----. 1 polkitd ssh_keys 56 9 month  6 17:29 auto.cnf
-rw-------. 1 polkitd ssh_keys 1676 9 month  6 17:29 ca-key.pem
-rw-r--r--. 1 polkitd ssh_keys 1112 9 month  6 17:29 ca.pem
-rw-r--r--. 1 polkitd ssh_keys 1112 9 month  6 17:29 client-cert.pem
-rw-------. 1 polkitd ssh_keys 1676 9 month  6 17:29 client-key.pem
-rw-r-----. 1 polkitd ssh_keys 387 1 month  2 12:32 ib_buffer_pool
-rw-r-----. 1 polkitd ssh_keys 79691776 1 month  19 22:39 ibdata1
-rw-r-----. 1 polkitd ssh_keys 50331648 1 month  19 22:39 ib_logfile0
-rw-r-----. 1 polkitd ssh_keys 50331648 9 month  6 17:29 ib_logfile1
-rw-r-----. 1 polkitd ssh_keys 12582912 1 month  19 22:38 ibtmp1
drwxr-x---. 2 polkitd ssh_keys 4096 9 month  6 17:29 mysql
drwxr-x---. 2 polkitd ssh_keys 4096 9 month  6 17:29 performance_schema
-rw-------. 1 polkitd ssh_keys 1680 9 month  6 17:29 private_key.pem
-rw-r--r--. 1 polkitd ssh_keys 452 9 month  6 17:29 public_key.pem
-rw-r--r--. 1 polkitd ssh_keys 1112 9 month  6 17:29 server-cert.pem
-rw-------. 1 polkitd ssh_keys 1680 9 month  6 17:29 server-key.pem
drwxr-x---. 2 polkitd ssh_keys 12288 9 month  6 17:29 sys
drwxr-x---. 2 polkitd ssh_keys 4096 1 month  19 22:39 test@002dlgl
[root@localhost db]# ll test@002dlgl/
 Total usage  112
-rw-r-----. 1 polkitd ssh_keys 67 1 month  19 22:23 db.opt
-rw-r-----. 1 polkitd ssh_keys 8586 1 month  19 22:39 student.frm
-rw-r-----. 1 polkitd ssh_keys 98304 1 month  19 22:39 student.ibd

Using independent table space can realize the transfer of table space , Reclaiming table space is also convenient , Use

alter table student engine=innodb;

mysql> use test-lgl;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> alter table student engine=innodb;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0

paragraph

from N District and 32 It's made up of two scattered pages . A table space has 4 Segments .

District

There are consecutive pages , That is, a continuous allocation of space on physical storage , The size of each area is fixed 1mb.

page

The minimum unit of physical storage allocation is page, namely disk IO The smallest unit of is page, The default page size is 16k, Express once IO Read 16k The data of . A district consists of 64 It's made up of two pages , So the area size is 16k*64=1mb.

Buffer State and its linked list structure disk IO The smallest unit of is a page page, Corresponding to the memory is buffer. buffer There are three states : 1.free buffer, Never used , Like a piece of white paper . 2.clean buffer, And disk page The data are consistent . 3.dirty buffer, And disk page The data are inconsistent , Dirty data .

flush list Will use lru The least recently used dirty buffer In series , After refreshing to disk , More free buffer.

That's ok

At least two lines of data can be stored in the page .

Memory structure

And Oracle similar , It is divided into SGA System global area and PGA Program cache .

Can pass show variables like '%buffer%' see .

mysql> show variables like '%buffer%';
+-------------------------------------+----------------+
| Variable_name | Value |
+-------------------------------------+----------------+
| bulk_insert_buffer_size | 8388608 |
| innodb_buffer_pool_chunk_size | 134217728 |
| innodb_buffer_pool_dump_at_shutdown | ON |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_dump_pct | 25 |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | ON |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 134217728 |
| innodb_change_buffer_max_size | 25 |
| innodb_change_buffering | all |
| innodb_log_buffer_size | 16777216 |
| innodb_sort_buffer_size | 1048576 |
| join_buffer_size | 262144 |
| key_buffer_size | 8388608 |
| myisam_sort_buffer_size | 8388608 |
| net_buffer_length | 16384 |
| preload_buffer_size | 32768 |
| read_buffer_size | 131072 |
| read_rnd_buffer_size | 262144 |
| sort_buffer_size | 262144 |
| sql_buffer_result | OFF |
+-------------------------------------+----------------+
24 rows in set (0.01 sec)

SGA Main memory area

innodb_buffer_pool

Used to cache InnoDB The data table 、 Indexes 、 Data dictionary and other information .

innodb_log_buffer

Buffering of transactions in memory , namely redo log buffer Size .

key_buffer_size

MyISAM Related index files

PGA Memory area

sort_buffer_size

join_buffer_size

read_buffer_size,MyISAM relevant

read_rnd_buffer_size

sql Temporary sorting of statements in memory .

Each big refresh thread and its function

There are mainly The main thread master thread and Four big IO Threads .

The main thread contains loop Main circulation , Divided into each 1s Operation and every 10s operation , The main operations include :

  1. Refresh buffer to disk
  2. Refresh dirty pages to disk
  3. produce checkpoint
  4. Delete useless undo page

Four threads :

  1. redo log thread Responsible for refreshing the log buffer to redo log In file .
  2. change buffer thread Responsible for flushing the inserted buffer memory to disk .
  3. read/write thread Is the database read and write request thread , The default is 4 individual .

Other threads :

  1. page cheaner thread The thread responsible for dirty page refresh . The default is 1 individual .
mysql> show variables like '%innodb_page%';
+----------------------+-------+
| Variable_name | Value |
+----------------------+-------+
| innodb_page_cleaners | 1 |
| innodb_page_size | 16384 |
+----------------------+-------+
  1. purge thread Responsible for deleting useless undo page .DML Sentences produce undo.
  2. checkpoint The thread function is redo log When switching occurs or when the file is almost full , Trigger dirty page refresh to disk .
  3. lock monitor thread Lock monitoring thread .

Memory refresh mechanism

There are mainly redo log buffer, binlog cache and data buffer Refresh mechanism of .

Oracle and MySQL Pay attention to the log first strategy .

redo log

redo log, Redo log file , Whether the transaction is committed or not is recorded . Such as database power down , Redoing the log will come in handy .

By default, there are two redo log file ,ib_logfile0 and ib_logfile1.redo log It's all happening Switch (redo log It's recycled ) when , Trigger checkpoint, Causes dirty pages to refresh .

[root@localhost db]# pwd
/home/lgl/docker/mysql/mysql_3306/db
[root@localhost db]# ll
 Total usage  188480
-rw-r-----. 1 polkitd ssh_keys 56 9 month  6 17:29 auto.cnf
-rw-------. 1 polkitd ssh_keys 1676 9 month  6 17:29 ca-key.pem
-rw-r--r--. 1 polkitd ssh_keys 1112 9 month  6 17:29 ca.pem
-rw-r--r--. 1 polkitd ssh_keys 1112 9 month  6 17:29 client-cert.pem
-rw-------. 1 polkitd ssh_keys 1676 9 month  6 17:29 client-key.pem
-rw-r-----. 1 polkitd ssh_keys 387 1 month  2 12:32 ib_buffer_pool
-rw-r-----. 1 polkitd ssh_keys 79691776 1 month  20 22:20 ibdata1
-rw-r-----. 1 polkitd ssh_keys 50331648 1 month  20 22:20 ib_logfile0
-rw-r-----. 1 polkitd ssh_keys 50331648 9 month  6 17:29 ib_logfile1
-rw-r-----. 1 polkitd ssh_keys 12582912 1 month  20 22:20 ibtmp1
drwxr-x---. 2 polkitd ssh_keys 4096 9 month  6 17:29 mysql
drwxr-x---. 2 polkitd ssh_keys 4096 9 month  6 17:29 performance_schema
-rw-------. 1 polkitd ssh_keys 1680 9 month  6 17:29 private_key.pem
-rw-r--r--. 1 polkitd ssh_keys 452 9 month  6 17:29 public_key.pem
-rw-r--r--. 1 polkitd ssh_keys 1112 9 month  6 17:29 server-cert.pem
-rw-------. 1 polkitd ssh_keys 1680 9 month  6 17:29 server-key.pem
drwxr-x---. 2 polkitd ssh_keys 12288 9 month  6 17:29 sys
drwxr-x---. 2 polkitd ssh_keys 4096 1 month  19 22:49 test@002dlgl

Refresh conditions

1. Through parameter control :

innodb_flush_log_at_trx_commit

mysql> show variables like '%commit%';
+-----------------------------------------+-------+
| Variable_name | Value |
+-----------------------------------------+-------+
| autocommit | ON |
| binlog_group_commit_sync_delay | 0 |
| binlog_group_commit_sync_no_delay_count | 0 |
| binlog_order_commits | ON |
| innodb_api_bk_commit_interval | 5 |
| innodb_commit_concurrency | 0 |
| innodb_flush_log_at_trx_commit | 1 |
| slave_preserve_commit_order | OFF |
+-----------------------------------------+-------+
8 rows in set (0.00 sec)

0-redo log thread every other 1S take redo log buffer write in redo log file . Best performance .

1- Each transaction commit triggers write redo log, and flush To disk , It's the safest mode , Ensure that data is not lost .

2- Each transaction commit triggers write redo log, But I don't flush To disk .

2.master thread: Refresh per second

3.redo log buffer: Use more than half to trigger refresh

binlog cache

yes MySQL Binary log file of . For backup recovery and master-slave replication .

Refresh conditions :

Through parameter control :

sync_binlog

mysql> show variables like '%sync_binlog%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| sync_binlog | 1 |
+---------------+-------+
1 row in set (0.01 sec)

0- At your own discretion binlog cache Refresh

1- Every time 1 Transaction commit , Force write to disk

The first mock exam mode : More secure data innodb_flush_log_at_trx_commit=1 sync_binlog=1

The difference between redo log and binary log :

  1. redo log It's recycled , When the last file is full , Write the first , So said redo log Switching occurs . binlog If you fill it up, you'll write something new binlog file .
  2. redo log It's abnormal downtime, etc , Return to use .

binlog To recover the data , Master slave replication uses .

data buffer

This means that the memory dirty page data will be flushed to disk .

Refresh conditions :

1. Through parameter control :

innodb_max_dirty_pages_pct, Represents the percentage of dirty pages .

Set to 25%-50%, Avoid the impact of later refresh TPS.

mysql> show variables like '%dirty_page%';
+--------------------------------+-----------+
| Variable_name | Value |
+--------------------------------+-----------+
| innodb_max_dirty_pages_pct | 75.000000 |
| innodb_max_dirty_pages_pct_lwm | 0.000000 |
+--------------------------------+-----------+
2 rows in set (0.01 sec)

innodb_adaptive_flushing, adaptive flushing , On by default .

mysql> show variables like '%flushing%';
+------------------------------+-------+
| Variable_name | Value |
+------------------------------+-------+
| innodb_adaptive_flushing | ON |
| innodb_adaptive_flushing_lwm | 10 |
| innodb_flushing_avg_loops | 30 |
+------------------------------+-------+
3 rows in set (0.01 sec)

2. I started when the redo log was full checkpoint, Starting dirty page refresh .

InnoDB Three characteristics of

from Insert cache (change buffer)、** Write twice (double write) and adaptive hash index (adaptive hash index)** Constitute the InnoDB Three characteristics of .

Insert cache : Improve DML Performance of operation .innodb_change_buffer_max_size, It is suggested to be changed to 50.

Write twice : Ensure data security , Prevent the disk page from being damaged redo log And it can't be recovered .

Adaptive index :InnodDB Note that queries can be created by Hash index Optimized , It's done automatically .innodb_adaptive_hash_index On by default .

Reference resources :《MySQL The way to the top 》 Zhang Su (sū)

Reprint please indicate the source : The first 3 Chapter MySQL Architecture and storage engine .md

author : Magic conch

版权声明
本文为[Magic conch]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210121003607577i.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云