Mysql structure
How a SQL query statement is executed?
Mysql infrastructure map
mysql architecture relations
service layer:
Connectors: Connectors to verify permissions management links directly linked to the cache and an analyzer
Query cache: cache hit directly return
Analyzer: mixed village failed to hit when using lexical analysis syntax analyzer
Optimizer: generating an index execution plan to select data
Actuator: link engine operating data
Engine layers:
Innodb
Myisam
。。。
Service Layer
Server layer including connectors, query cache, analyzer, optimizer, actuators, etc., covering MySQL 's most core services
Cover all Function: Date Time mathematics string
Across storage engine features: Stored Procedures Triggers view
Different storage engines share a service layer
Storage Engine layer
Responsible for access to data
Plug-mode support different storage engines
Default innodb engine
Press the inquiry process to understand the role of individual components
1 connector link management database permissions verification
Connect statement: MySQL -h ip -P $ $ $ the User Port -u -p
A connection command mysql client tools, to establish a connection with the server
B to complete the TCP handshake, authenticate identity began connector, enter a user name and password
If a user name or password is wrong, you will receive an "Access denied for user" error, and the client program ends 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 permissions
That is, after a user connection is successfully 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 editing, then only the new connection will use the new permissions settings
If the client is not too long movement, the connection will automatically be disconnected. This time is set by the parameter wait_timeout control, the default value is . 8 hours
Links on Database
Long connection: Once connected, the client sustained if there is a request, then always use the same connection
Short link: Each executing the query a few times to disconnect and re-establish a next query
Link establishment procedure is complex, requires a lot of time if the operation of accessing data using the long link
The disadvantages of using long link:
Easily take up memory, because MySQL memory temporarily used during execution is to manage the connection object inside. These resources will be released only when disconnected. Therefore, if the cumulative length of the connection, may result take up too much memory, the system is forced to kill ( the OOM )
Resulting in: from phenomenon to see that MySQL abnormal restart
Solve problems connected memory for long, tedious short links
-
Regular long connection is disconnected. After a period of time, or a program executed which determines a large memory for queries, disconnect, then reconnect to query again.
-
If you are using MySQL 5.7 or later, you can perform a relatively large after each operation, by performing mysql_reset_connection to re-initialize the connection resources. This process does not require rewiring and re-do the verification authority, but the connection will be restored to the state when just finished creating.
2 query cache query returned no hits into the analytical queries
After the beginning of the second step to establish the connection: inquiry
MySQL After getting a query request, the query cache will first look is not performed before this statement.
It was performed before the statement and the result may be to key-value pairs form, directly cached in memory.
key is the query statement,
value is the result of the query.
If your query can be found directly in the cache Key , then the value will be returned directly to the client
If the query is not in the back of the stage will continue. After the execution is completed, the results will be stored in the query cache. This will greatly improve query efficiency
The shortcomings of the query cache
Build query cache easily be emptied data table updates, for more frequently updated data is not suitable for use query cache
For static table, you can use the query cache speed up the search
3 analyzer
In the second step, the query if there is no cache hit, will enter the analyzer, it began to really execute the statement
First of all,. Mysql to understand your operations, so they use the query analyzer to analyze
Analyzer analyzes identified
Lexical analysis: identification key
First analyzer will do "lexical analysis." Your input is a SQL statement from a plurality of strings and spaces, MySQL need to identify what is inside strings are what representatives.
MySQL identified from your input "select" keyword, which is a query. It also should string "T" is identified as "table T", the character string "ID" identified as "column ID"
Parsing: The syntax judge
According to the results of lexical analysis, according to the rules of grammar parser will judge you enter this SQL statement meets the MySQL syntax
If the syntax is incorrect, an error will receive a reminder, the general syntax error will prompt the first error to appear
That attention being given in use near
4 Optimizer
After analysis analyzer, mysql know what you have to do things the
However, before you begin, you need to be treated optimizer.
Optimization table there is a plurality of time indexes, determines which index is used; or when multiple table associated with a statement (join), determines the order of connection of each table. For example, you execute a statement such as the following, this statement is executed two table join:
mysql> select * from t1 join t2 using(ID) where t1.c=10 and t2.d=20;
-
May be taken inside the table t1 start c = ID 10 values recorded, then according to the ID value association table t2, then t2 is determined value which is equal to 20 d.
-
May be taken inside the table t2 start d = ID 20 values recorded, and then the value t1 according to the correlation ID, and then determines the value of t1 which is equal to 10 c.
The logical result of the implementation of these two methods is the same, but the efficiency of the implementation will be different, and the role of the optimizer is to decide which scheme to use
The current global notes of any course is to understand the state, so for specific content optimizer, there will be specific chapter
5 Actuator
After filtering through the optimizer to optimize the query, they enter the actuator stage, begin statement
Steps
1 judge Permissions
The beginning of the implementation, first determine what you do not have permission to perform a query on the table T, and if not, it will return an error without permission, as shown below.
mysql> select * from T where ID=10;
ERROR 1142 (42000): SELECT command denied to user 'b'@'localhost' for table 'T'
If you have permission, you open the table to continue. Open the table, the actuator will be defined according to the engine table, use the interface to the engine.
For example, in our example table T, ID field is not indexed, then the execution flow actuator is this:
-
The first line of the engine interface calls taken InnoDB table determines the value of ID 10 is not, skip if not, then if this line is present in the result set;
-
Interface call engine take "next row", the logic repeats the same determination until get to the last row of the table.
-
Actuator traversal above all the rows satisfy the condition set record consisting of as the result set returned to the client.
So far, this statement is executed is complete.
For tables with indexes, logic performed about the same. The first call was "taken to meet the conditions of the first line," this interface, after taking the cycle "to meet the conditions of the next line," the interfaces that are the engines has been defined.
You will see a field rows_examined in the slow query log database, indicating that the statement execution scanning process the number of lines. This value is in the actuator each call to get the engine when the accumulated data lines.
In some scenarios, the actuator called once inside the engine is a multi-line scan, so scan engines are not identical with the number of lines of rows_examined