MySQL real - Infrastructure

This article is a personal memo, mainly geeks time "MySQL combat 45 stresses" the study notes.

MySQL architecture

image.png

MySQL Server can be divided into two parts layer and the storage engine layer. Different storage engines share a Server layer.

Server layer

Outline

Server layer includes

  • Connector
  • Query Cache
  • Analyzer
  • Optimizer
  • Actuator
  • ...

Server layer functions

  • MySQL most of the core services
  • All of the built-in functions, such as date, time, math and encryption functions, etc.
  • Across storage engine features such as stored procedures, triggers, views, etc.

Connector

What connector do?

  • The connector is responsible for establishing a connection with the client, access permissions, maintain and manage connections.

Connection command Example

mysql -h$ip -P$port -u$user -p

Which, mysql client tools, to establish a connection with the server. Upon completion of the classic TCP handshake, the connector will start certifying your identity, this time with is that you enter a user name and password.

  • If a user name or password wrong, will receive the "Access denied for user" error, the client ends the execution
  • If a user name and password authentication, permission to connect to the table will find out which permissions you have. Thereafter, the connection permission determination logic which, at this time will depend on the read permission.

    • This means that a user after successful connection is established, even if you use an administrator account on the user's permission has been modified, it will not affect the rights of existing connections.
    • After completion of editing privileges, then only the new connection will use the new permissions settings.
  • After the connection is completed, if there is no subsequent operation, the connection is in the idle state, the command can be used to see if the show processlist link state. as follows.

    • Wherein the Command column reads "Sleep" to this line, it means there is now a free connection system.
mysql> show processlist;
+----+----------+-----------------+---------+---------+--------+-----------+------------------+
| Id | User     | Host            | db      | Command | Time   | State     | Info             |
+----+----------+-----------------+---------+---------+--------+-----------+------------------+
| 28 | testuser | localhost:60453 | db_test | Sleep   |      8 |           | NULL             |
| 29 | testuser | localhost:53231 | db_test | Query   |      0 | starting  | show processlist |
+----+----------+-----------------+---------+---------+--------+-----------+------------------+
25 rows in set (0.00 sec)

If the client is not too long movement, the connection will automatically disconnect. This time is controlled by the parameter wait_timeout, default is 8 hours.

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

mysql>

If, after the connection is broken, the client sends a request again, you will receive an error: Lost connection to MySQL server during query. At this time if you want to continue, we need to reconnect after the execution of the request.

What is the long connection?

Long connection: refers to the connection is successful, if the client has continued to request, has been using the same connection.

The process of establishing a connection is often more complex, it is recommended to try to reduce the act of establishing a connection, that is, to make use of long connection.

什么是短连接?

短连接:指每次执行完很少的几次查询就断开连接,下次查询再重新建立一个。

长连接的影响

影响

  • 全部使用长连接后,可能会发现,有些时候MySQL占用内存涨得特别快。
  • 如果长连接累积下来,可能导致内存占用太大,被系统强行杀掉(OOM),看起来是MySQL异常重启。

原因

  • 是因为MySQL在执行过程中临时使用的内存是管理在连接对象里面的。这些资源会在连接断开的时候才释放。

解决办法

  • 定期断开长连接。

    • 使用一段时间,或者程序里面判断执行过一个占用内存的大查询后断开连接,之后查询时再重连。
  • 执行mysql_reset_connection重新初始化连接资源

    • MySQL >= 5.7版本
    • 在每次执行一个比较大的操作后,重新初始化链接资源
    • 这个过程不需要重连和重新鉴权,但是会将连接恢复到刚刚创建完时的状态

查询缓存

连接建立完成后,就可以进行查询了。MySQL接到一个查询请求后,会先查询缓存。

  • 如果语句在缓存中,则其对应的结果会被直接返回给客户端,以提高查询效率。
  • 如果语句不在缓存中,会继续后面的执行阶段。执行完成后,执行结果会被存入查询缓存中。

但是大多数情况下不要使用查询缓存。因为查询缓存往往弊大于利。

查询缓存的失效非常频繁,只要有对一个表的更新,这个表上所有的查询缓存都会被清空。

  • 不使用缓存

    • 对于更新压力大的数据库来说,查询缓存的命中率会非常低。
  • 可使用缓存

    • 业务是一张静态表,很长时间才会更新一次。
    • 比如,一个系统配置表,那这张表上的查询才适合使用查询缓存。

可通过设置参数query_cache_type来设置要不要使用查询缓存。

  • DEMAND

    • 默认都不使用查询缓存
  • SQL_CACHE

    • 显式指定使用查询缓存,如
mysql> select SQL_CACHE * from T where ID=10;

通常情况下,该参数设置为OFF,即关闭查询缓存即可。

mysql> show variables like 'query_cache_type';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| query_cache_type | OFF   |
+------------------+-------+
1 row in set (0.00 sec)

注意

  • MySQL 8.0版本直接将查询缓存的整块功能删掉了,即8.0开始彻底没有这个功能了。

分析器

分析器做什么?

  • 词法分析

    • 识别SQL语句各个字符串是什么、代表什么
  • 语法分析

    • 根据语法规则,判断SQL语句是否满足MySQL语法
    • 如果语句不对,会报“You have an error in your SQL syntax”错误
mysql> elect * from t where ID=1;

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'elect * from t where ID=1' at line 1

一般语法错误会提示第一个出现错误的位置,需要关注紧接“use near”的内容。

优化器

优化器做什么?

  • 在表里面有多个索引的时候,决定使用哪个索引
  • 或者在一个语句有多表关联(join)的时候,决定各个表的连接顺序

比如执行下面这样的语句,这个语句是执行两个表的join:

mysql> select * from t1 join t2 using(ID)  where t1.c=10 and t2.d=20;

两种执行方法

  • 先从表t1里面取出c=10的记录的ID值,再根据ID值关联到表t2,再判断t2里面d的值是否等于20
  • 先从表t2里面取出d=20的记录的ID值,再根据ID值关联到t1,再判断t1里面c的值是否等于10

这两种执行方法的逻辑结果是一样的,但是执行的效率会有不同,而优化器的作用就是决定选择使用哪一个方案。

执行器

分析器:要做什么
优化器:怎么做

执行步骤

  • 鉴权

    • 没有:返回无权限错误
    • 有:继续
  • 打开表执行

    • 打开表的时候,执行器就会根据表的引擎定义,去使用这个引擎提供的接口。

有索引查询

  • 第一次调用的是“取满足条件的第一行”这个接口
  • 之后循环调用“满足条件的下一行”这个接口

无索引查询

  • 调用InnoDB引擎接口取这个表的第一行,判断ID值是不是10

    • 如果不是则跳过
    • 如果是则将这行存在结果集中
  • 调用引擎接口取下一行,重复相同的判断逻辑,直到取到这个表的最后一行
  • 执行器将上述遍历过程中所有满足条件的行组成的记录集作为结果集返回给客户端

扫描了多少行?

  • 数据库慢查询日志中有一个rows_examined字段,表示这个语句执行过程中扫描了多少行
  • 这个值就是在执行器每次调用引擎获取数据行的时候累加的

存储引擎层

  • 存储引擎层负责数据的存储和提取
  • 架构模式是插件式的,支持InnoDB、MyISAM、Memory 等多个存储引擎
  • 目前最常用的存储引擎是InnoDB,它从MySQL 5.5.5 版本开始成为了默认存储引擎

比如,执行create table建表时,

  • 如果不指定引擎类型,默认使用的就是 InnoDB
  • 使用engine=memory 来指定内存引擎创建表

Guess you like

Origin yq.aliyun.com/articles/745253