当前位置:网站首页>MySQL的体系结构
MySQL的体系结构
2022-06-10 10:13:00 【InfoQ】
1、MySQL的体系结构

客户层
- 客户层:进行相关的连接处理、权限控制、安全处理等操作
服务层
- 服务层负责与客户层进行连接处理、处理以及执行SQL语句等,主要包含连接器、查询缓存、优化器、执行器、存储引擎。触发器、视图等也在这一层
存储引擎层
- 存储引擎层负责对数据的存储和提取,常见的存储引擎有InnoDB、MyISAM、Memory等,在MySQL5.5之后,MySQL默认的存储引擎就是InnoDB,InnoDB默认使用的索引结构就是B+树,上面的服务层就是通过API接口与存储引擎层进行交互的
数据层
- 数据层系主要包括MySQL中存储数据的底层文件,与上层的存储引擎进行交互,是文件的物理存储层。其存储的文件主要有:日志文件、数据文件、配置文件、MySQL的进行pid文件和socket文件等。那么一条SQL语句在MySQL的整个体系结构是如何执行的呢?
2、SQL语句的执行过程
1、客户层
- 首先连接器与客户端进行连接、以linux系统为例,通过在Mysql服务启动成功之后通过一下命令进行数据库的登录
[[email protected] ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES
[[email protected] ~]# ps -ef |grep mysql
root 87531 1 0 Feb09 ? 00:00:00 /bin/sh /www/server/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --datadir=/www/server/data --pid-file=/www/server/data/bp18425116f0cojd1vnz.pid --sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
mysql 88147 87531 0 Feb09 ? 00:43:28 /www/server/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/www/server/mysql --datadir=/www/server/data --plugin-dir=/www/server/mysql/lib/plugin --user=mysql --sql-mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION --log-error=bp18425116f0cojd1vnz.err --open-files-limit=65535 --pid-file=/www/server/data/bp18425116f0cojd1vnz.pid --socket=/tmp/mysql.sock --port=3306
root 2725702 2724782 0 14:35 pts/0 00:00:00 grep --color=auto mysql
[[email protected] ~]# systemctl stop mysql
[[email protected] ~]# systemctl status mysql
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; generated)
Active: inactive (dead) since Thu 2022-06-09 14:36:55 CST; 2s ago
Docs: man:systemd-sysv-generator(8)
Process: 2725788 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
Feb 09 14:30:10 bp18425116f0cojd1vnz systemd[1]: Starting LSB: start and stop MySQL...
Feb 09 14:30:11 bp18425116f0cojd1vnz mysqld[1174]: /etc/rc.d/init.d/mysqld: line 244: my_print_defaults: command not found
Feb 09 14:30:11 bp18425116f0cojd1vnz mysqld[1174]: /etc/rc.d/init.d/mysqld: line 265: cd: /www/server/mysql: No such file or directory
Feb 09 14:30:11 bp18425116f0cojd1vnz mysqld[1174]: Starting MySQLCouldn't find MySQL server (/www/server/mysql/bin/mysqld_safe)[FAILED]
Feb 09 14:30:11 bp18425116f0cojd1vnz systemd[1]: Started LSB: start and stop MySQL.
Jun 09 14:36:52 bp18425116f0cojd1vnz systemd[1]: Stopping LSB: start and stop MySQL...
Jun 09 14:36:55 bp18425116f0cojd1vnz mysqld[2725788]: Shutting down MySQL..[ OK ]
Jun 09 14:36:55 bp18425116f0cojd1vnz systemd[1]: mysqld.service: Succeeded.
Jun 09 14:36:55 bp18425116f0cojd1vnz systemd[1]: Stopped LSB: start and stop MySQL.
[client]
#password = your_password
port = 3306
socket = /tmp/mysql.sock
[mysqld]
skip-grant-tables
port = 3306
socket = /tmp/mysql.sock
datadir = /www/server/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
[[email protected] ~]# systemctl start mysql
[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.50-log Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> use mysql;
Database changed
mysql> update mysql.user set authentication_string=password('your_password') where user='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
[[email protected] ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.50-log Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
2、查询缓存
3、解析器
mysql> select djglfdjg from user;
ERROR 1054 (42S22): Unknown column 'djglfdjg' in 'field list'
4、SQL执行器
- 在开始执行的时候,预处理阶段你对这个表有没有执行查询的权限,如若没有,就会返回相应的错误
- 检查查询的表或者字段是否存在,如若没有,也会返回相应的错误信息
mysql> explain SELECT Host FROM `user` where Host='localhost';
+----+-------------+-------+------+-------------------------+---------+---------+-------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+-------------------------+---------+---------+-------+------+--------------------------+
| 1 | SIMPLE | user | ref | PRIMARY,index_user_Host | PRIMARY | 180 | const | 3 | Using where; Using index |
+----+-------------+-------+------+-------------------------+---------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)
- id=1 SELECT识别符,查询序号即为sql语句执行的顺序
- select_type=SIMPLE 表示SQL查询语句走的是单表查询
- table=user 输出的行所用的表
- type=ref 显示了连接使用了哪种类别,有无使用索引,type扫描方式由快到慢
system > const > eq_ref > ref > range > index > ALLsystem:系统表,少量数据,往往不需要进行磁盘IOconst:常量连接eq_ref:主键索引(primary key)或者非空唯一索引(unique not null)等值扫描 ref:非主键非唯一索引等值扫描range:范围扫描index:索引树扫描all:全表扫描
- possible_keys 表示查询语句可能会用到的索引,在这里有两个,PRIMARY表示为主键索引,index_user_Host为另一个索引
- key 表示在查询语句时实际用到的索引,在这里为PRIMARY,那为什么这里只用到了PRIMARY这个索引呢,别急,后面会说到
- key_len 表示使用的索引长度
- ref 列显示使用哪个列或常数与key一起从表中选择行
- rows 显示MySQL执行查询的行数,简单且重要,数值越大越不好,说明没有用好索引
- Extra 该列包含MySQL解决查询的详细信息,Using index表示相应的select操作中使用了覆盖索引(covering index),避免访问了表的数据行,using where,表明索引被用来执行索引键值的查找Using where表明使用了where过滤Using join buffer使用了连接缓存Using temporary 使用了临时表保存中间结果,mysql在对查询结果排序时使用临时表。常见于排序order by和分组查询group by。group by一定要遵循所建索引的顺序与个数using filesort,using temporary,using index最为常见,出现前两种表示是需要优化的地方通过观察上面的执行语句,在查询时,有2个索引,但是只用到了PRIMARY这个索引,并没有用到index_user_Host,查询表所建立的索引
mysql> show index from mysql.user;
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| user | 0 | PRIMARY | 1 | Host | A | NULL | NULL | NULL | | BTREE | | |
| user | 0 | PRIMARY | 2 | User | A | 8 | NULL | NULL | | BTREE | | |
| user | 1 | index_user_Host | 1 | Host | A | NULL | NULL | NULL | | BTREE | | |
+-------+------------+-----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
user边栏推荐
- Neo hacker song's award-winning list has been announced. Who owns tens of thousands of dollars of gold?
- 【兴趣阅读】DeepInf: Social Influence Prediction with Deep Learning
- Use nsenter to enter netns to capture packets
- ADB 日志抓取
- Today, 19:30 | graphics special session - Gao Lin, teacher team of Institute of computing technology, Chinese Academy of Sciences
- kubernetes 设置 Master 可调度与不可调度
- NFT铸造交易平台开发市场详情
- In 2025, the output value, added value and other scale indicators of the construction industry will continue to remain at the forefront of the country
- 9、 Delegation mode
- “大写意花鸟画宗师李苦禅先生”重磅数字藏品全网首发
猜你喜欢

Personnalisation de l'équipement d'essai de Chengdu Introduction préliminaire aux types de données du langage C du micro - ordinateur à puce unique

2023 Wangdao C language training camp (binary search tree - sequential search - half search)

山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(十六)
![[cloud native weapon cilium] what is cilium](/img/e5/6faeb76d6c111afb974155a7e16400.png)
[cloud native weapon cilium] what is cilium
![[interesting reading] deepinf: social influence prediction with deep learning](/img/67/2a90dea2f129245c4a60a5e079340f.png)
[interesting reading] deepinf: social influence prediction with deep learning

5g Unicom network management design idea

Réflexions sur la conception de la gestion du réseau Unicom 5g

Neo 黑客松获奖名单揭晓,上万美金花落谁家?

Detailed steps for installing mysql+django under mac

混音器:视频会议录制不可或缺的组件
随机推荐
Uniapp message push (push by PHP server)
Personnalisation de l'équipement d'essai de Chengdu Introduction préliminaire aux types de données du langage C du micro - ordinateur à puce unique
Dr. jiangxiaowei, a member of hpca Hall of fame, is the chief scientist of Dayu smart core
Leetcode 2000. 反转单词前缀
Delphi中的冷门知识点
无心剑中译里尔克《秋日》
Qchart note 1: simple linear diagram lineseries
怎样简洁明了地说清楚产品需求?
解决Typescript文件被识别为视频的问题
七、策略模式
协程asyncio异步编程
【FAQ】运动健康服务REST API接口使用过程中常见问题和解决方法总结
[interesting reading] deepinf: social influence prediction with deep learning
The winning list of "talking tom cat family - time light chaser" 3D digital collection was announced
九、委托模式
Xcode8.3.2 自动打包脚本
62. different paths - Dynamic Planning
Today, 19:30 | graphics special session - Gao Lin, teacher team of Institute of computing technology, Chinese Academy of Sciences
Market development details of NFT casting trading platform
【兴趣阅读】DeepInf: Social Influence Prediction with Deep Learning