MySql infrastructure, and SQL statement execution process

01. mysql Infrastructure

How SQL statement is executed

Learn about infrastructure mysql from a sql statement is to learn how to perform.

Generally, we write a query like the following:

select user,password from mysql.user;

This will return a result, but I do not know the internal flow of execution of this statement.

The following is the logic chart mysql:

1567066796748

Mysql Server can be divided into two parts and the storage layer engine layer.

Server layer connector / cache / analyzer / optimizer / actuator cover many of the core functions mysql.

Storage engine layer is responsible for data storage and retrieval, support Innodb, MyIsam, Memory, BlackHole, etc., after the default storage engine version Mysql5.5 is Innodb.

Next, layer by layer we look at the implementation process SQL statements.

1) The connector

First connect mysql client is connected to the connector, the connector is responsible for establishing the connection / verify user identity with the client to get permission. Connection command is generally as follows:

mysql -h ip地址 -P 端口 -u 用户 -p

When a client enter a user name and password over the beginning of the connection, the connector will check:

  • If a user name or password is incorrect, the client will receive an "Access denied for user" error.
  • If the correct user name and password verification, the connector will check the permissions the user has. Thereafter, in the connection permission logic, read at this time are dependent rights.

This means that after a user connection is successfully established, even if you use administrator privileges to the user's changed, it will not affect the connected user, unless the user connections and disconnections. Let connector can only be re-read permission.

2) query cache

After the connection is established, you can select to perform other statements, and then it will be the second step: query cache

Mysql sql after receipt of a request, first check the cache prior to see if it is enforced. If executed and the cache has not expired, the result will be the key-value stored in memory, key is the query, value is the result of the query. If there is a cache, directly to the corresponding value is returned to the client.

If the statement is not in the query cache, will perform down the following stage, after the execution is completed, will result into the cache.

Query cache expiration very ordinary, because as long as update a table, then the table of results for all query cache will be cleared, so the frequently changing table, query cache hit rate is very low. Unless the table data is relatively stable, it does not change often, only for query cache.

Under understand: after Mysql8.0 version, query cache function was deleted.

3) Analyzer

If the cache hit useless, analyzer began to work on the sql statement parsing.

First analyzer will do "lexical analysis", plus more characters you type sql statement of spaces, the analyzer need to analyze each character inside out anything representatives.

As the "select" the keywords you enter start, mysql know this is a query, and then analyze that is the table name, is a condition that you enter, and so on.

Lexical analysis done, start doing, "parsing", according to the results of lexical analysis, parsing will judge you enter this sql statement meets Mysql syntax.

If your statement is wrong, will receive the "You have an error in you SQL syntax" error reminder, if this statement select few dozen at the beginning of the letter "s".

mysql> elect * from stu;
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 stu' at line 1

The general syntax error will prompt the first error to appear, so you have to focus on is immediately behind the content of the "use near".

4) Optimizer

After the analyzer, Mysql already know what you have to do, and before starting, also need to be processed optimizer.

Optimization table there is a plurality of time indexes, determines which index is used; or when a plurality table associated statement determines the connection order of each table.

5) Actuator

Mysql through the analyzer you know what to do, know how to do, the next step is execution begins executing the statement by the optimizer;

Before you begin, you will first have to determine the list or library does not have permission to operate, if there is no return privilege error.

If you have permission, you open the table to continue. Open the table when the engine actuator based on the definition of the table, use the interface to the engine.

The select statement such as: select * from db1 where ID = 100;

  • The first row first call Inodb engine interface to obtain table ID value is not determined 100 if it is not skipped, and if the result is present in the result set;
  • Engine interface call access "next row", the logic repeats the same determination until the last row of this table is read.
  • The actuator will traverse above all to meet the required process back to the client.

So far, this select statement to be executed finished.

Slow query log database will see the field rows_examined, the statement indicates that the implementation process scans the number of lines. This value is in the actuator each call to get the engine when the accumulated data lines.

In some cases, the actuator called once inside the engine is a multi-line scan, the scan engines are not exactly the same number of rows with rows_examined.

Mysql such a logical structure and processes over again, I also have a preliminary understanding of the various stages of the implementation process of the entire sql statement. I hope you can help us.

Guess you like

Origin www.cnblogs.com/Tao9/p/11431014.html