[MySQL] logical framework and SQL execution

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wrs120/article/details/80711131
Logic chart

Write pictures described here

  Design MySQL database is divided into four layers, it can design a program of thinking to think (UBD), we can put MySQL into four layers:

First layer: connection layer

  Comprising: a connection pool components, component management services and tools, are some of the top clients and connectivity services, including local sock communication TCP / IP communication and most client / server tool implementation. Mainly to complete some similar connection processing, authorization and authentication, and related security solutions. Introduces the concept of a connection pool of threads on the layer, threads provide secure access authentication by the client. Also on this level can be achieved based on a secure link SSL. The server also verify it has the operating authority for each client security access.

Second layer: Layer Service

  Comprising: SQL interface component, the query analyzer component, optimizer component, a buffer component, a second core layer mainly perform most services, such as SQL interface component, and complete the cache query, analysis and optimization of sql and some built-in functions carried out. All functions across storage engines also achieve this level, such as procedures, functions, and so on. In this layer, the server parses the query and creates internal analytical book, and its complete sequence corresponding optimization as a lookup table to determine whether the use of the index, and finally generate the corresponding operation is performed. If the select statement, the server will query cache inside, and if the cache is large enough so that the read operation to address a large number of environmental performance can be a good lift system

Third layer: Layer engine

  The real storage engine is responsible for the storage and retrieval of data in MySQL server to communicate with the storage engine via the API. Different storage engines have different functions, so we can choose according to their actual needs. MySQL storage engine is a plug-in storage engine query processing and other system tasks and storing the extracted data phase separation, this architecture can select an appropriate storage engine according to the actual needs and operational requirements

Fourth layer: storage layer

  Data storage layer, the interaction is primarily on the file system running on the bare device, and complete the data storage and the storage engine

select execution

Write pictures described here

第一步:发送sql语句给服务端, 通过服务器的权限检查(连接层功能)

第二步:查询缓存

  服务器通过权限检查之后(用户名和密码等),查询缓存,如果缓存时打开的,服务器首先检查查询缓存中的数据

  • 命中时,MySQL会立即返回结果,省去解析、优化和执行等阶段。否则,进入下一阶段
  • MySQL保存结果于缓存中,把select语句本身做hash计算,计算的结果作为key,查询结果作为value
  • 查询语句的大小写会影响缓存的存储和命中,故需保持查询语句的大小写一致性

第三步:解析器

   mysql解析器使用mysql语法规则验证和解析查询,通过关键字(select,insert等)将SQL语句进行解析,生成对应的“解析树”,此时的树是没有优化的,即检查你编写的sql语句是否有错误

第四步:预处理器

  预处理器根据mysql规则进一步检查解析树是否合法,即对解析书进行预处理(有计算的地方得到计算结果,变量的赋值,函数的执行

第五步:查询优化器

  当语法树被认为是合法的了,优化器便将其转化成执行计划,即将sql语句进一步优化,第一步做什么,第二步做什么,比如你的执行顺序是A,B,C但是经过查询优化器分析认为B,A,C执行顺序是最优的

第六步:查询执行引擎

  通过调用存储引擎实现的接口来完成引擎的调用,进而完成结果的查询,这些接口被称为handler API

第七步:返回结果

  查询执行的最后一个阶段就是将结果返回给客户端。即使查询不到数据,MySQL仍然会返回这个查询的相关信息,比如该查询影响到的行数以及执行时间等等

  相关介绍参见美文:https://blog.csdn.net/fuzhongmin05/article/details/70904190,此篇文章介绍了更多的知识点,很值得一读

缓存注意事项

1. What statement will not be cached

  • When the query has some uncertain data, will not be cached, such as now (), current_time (), etc.
  • If the query contains a user-defined functions, stored functions, user variables, temporary tables, mysql database system table, or any table contains the permissions are not normally cache, such as those containing CURRENT_USER or CONNECION_ID () query because of the different the user is returned different results, so the results will be cached no meaning

2. Cache will bring additional costs

  • Read queries must be checked before the start whether cache hit
  • If one reads the query can be cached and not cached, then when finished executing, MySQL query cache stores the result will be.
  • Write to also have an impact, because when data is written, MySQL must all corresponding to the cache tables are set failure, which will result in a cache memory system consumes a lot of time greater
  • Therefore, the query cache is not necessary, it all depends on the efficiency of queries larger overhead can query whether the cache hit

Guess you like

Origin blog.csdn.net/wrs120/article/details/80711131