[A little bit of progress every day] mysql finishing notes (a)

How a sql mysql is executed

image-20200324134345377
image-20200324134345377

Description: mysql8.0 version has removed the query cache.

letter of agreement

mysql support multiple communication protocols, synchronous / asynchronous mode may be used to support long links / short links.

The type of communication

  • Synchronize
  • Isochronous communication depends on the callee, the performance is limited by the callee. When applied to operate the database, thread blocks, waiting for the database to return results.
  • Generally only one way to achieve synchronization.
  • asynchronous
  • Asynchronous thread to avoid blocking wait, but can not reduce the execution time of sql.
  • Asynchronous In the case of high concurrency, a connection will perform each of sql create, to avoid data confusion. The disadvantage is that the pressure becomes large server (switching between threads will take up cpu resources).

Because of the asynchronous mode is more complex, greater pressure on the server, the synchronization manner are generally employed.

Connection

  • Long connection: connection can remain open longer, reduce the consumption of server creation and release of connections. But they consume memory does not close the case for a long time, mysql default timeout is 28800s.
  • Short connection: connection is short after the operation is completed immediately close out.

way of communication

  • Simplex: when communication between two computers, the data transmission is one-way.
  • Half: When the communication between the two computers, data transmission is bidirectional, but at the same time, only one data transmission, a data receiving station, the two can not simultaneously transmit or receive data.
  • Full duplex: Full duplex is simultaneously transmit or receive data can be transmitted.

Mysql communication system uses a half-duplex communication.

Query Cache

Mysql query cache is built inside of a cache module. mysql cache module is off by default, is not recommended, mainly due to the limited mysql built-in caching scenarios, it requires sql statement must be exactly the same, followed by the table inside the data changes, the cache this table will all fail. Under normal circumstances are cached to the ORM framework or other independent cache services. mysql8.0 version has removed the cache module.

Parsing and preprocessing

Parsing (Parser)

Parsing The first step is a complete break sql become a word, this step is also referred to as lexical resolution.

然后才会进行语法解析,语法解析会对sql做一些语法检查,然后根据sql的语法规则,生成一个数据结构,也就是解析树(select_lex

预处理(Preprocessor)

预处理会检查生成的解析树,解决解析器无法解析的语义。例如:它会检查表和表的列是否存在,检查别名等。

查询优化器

一条sql语句会存在多种执行方式,最终以那种方式来执行是查询优化器来决定的。

查询优化器的目的就是根据解析树生成的不同的执行计划(Execution Plan),然后选择一种最优的执行计划,mysql里面使用的是基于开销(cost)的优化器,那种开销最小,就使用那种。

优化器最终会把解析树变成一个查询执行计划,查询执行计划一个数据结构。这个执行计划不一定是最优的结果,因为mysql也有可能覆盖不到所有的执行计划。

注意:当使用Explain查看执行计划的时候,这个结果不一定是最终的执行方式。

存储引擎

在关系型数据库里,数据是存放在Table里的,这个Table在存放数据的同时,还要组织数据结构,这个存储结构是由存储引擎决定的,所以我们也可以把存储引擎叫做表类型。

在mysql里是支持多种存储引擎的,他们是可以替换的,所以也叫做插件式的存储引擎。

存储引擎列表

image-20200324130409507
image-20200324130409507

注意:这里引用mysql官网

MyISAM

通常情况下应用于只读或者以读为主的工作。适合只读类的数据分析项目。

特点
  1. 支持表级别的锁,插入和更新都会锁表,所以限制了读/写的性能。
  2. 拥有较高的插入和查询速度。(例如,可以先设定存储引擎为MyISAM,然后插入100W的数据,然后在修改存储引擎为InnoDB来提高效率)
  3. 存储了表的行数,count速度快。
  4. 不支持事物。

InnoDB

The default storage engine Mysql5.7 version. For more frequent updates of the table, there is a concurrent read and write or have something of a scene.

Feature
  1. Support of things, foreign keys, so data integrity, higher consistency.
  2. Support row and table locks.
  3. Support concurrent read and write, write, do not block read ( MVCC ).
  4. Special index storage, reducing I / O, improve query efficiency

Memory

All data stored in RAM to increase the speed of data to find non-critical. Suitable for a temporary table.

Feature
  • Advantages: the presence of data in memory, read and write speed,
  • Cons: When the database is restarted or the case of power failure, the data will be lost.

Execution Engine

? Execution engine is to use the execution plan to operate the storage engine, which uses storage engine API provided to complete the operation, the last meal of data back to the client.

Guess you like

Origin www.cnblogs.com/fengfujie/p/12559104.html