Mysql structure (statement execution)

Generally, MySQL divided into two parts and the storage layer Server engine layer.

Server layer includes a connector, query cache, Analyzer, actuators, and all the built-in functions (such as date, time, and encrypted mathematical functions, etc.) and cross-functional storage engine (such as stored procedures, triggers, views)

Storage engine layer is responsible for data storage and retrieval, support for InnoDB, MyISAM, Memory, and other storage engine. After MySQL 5.5.5 version of the default storage engine is stored in InnoDB.

 

Connector (Connector)

Before the SQL query, certainly must first establish a connection with MySQL, which is done by the connector. The connector is responsible for establishing a connection with the client, access permissions, maintain and manage connections

If the client no movement too long, the connection will automatically be disconnected; wait_timeout This time is controlled by the parameters, a default value is 8 hours. If, after the connection is broken, the client sends a request again, it would receive an error reminder: Lost connection to MySQL server during query.

Long and short connection connector

Inside the database, long connection means that the connection is successful, if the client has continued to request, has been using the same connection.

Short connection refers to each execution of the query to finish a few times disconnected, the next re-create a query.

The process of establishing a connection is often more complex, it is recommended to use to try to reduce the act of establishing a connection, try to use long connection. But after all use long connection, MySQL sometimes take up memory rose much faster because MySQL temporary memory used during execution is to manage the connection object inside. These resources will be released only when disconnected. So if long connection accumulated, can lead to memory footprint is too large, the system is forced to kill (OOM), from phenomenon to see that MySQL abnormal restart.

How to solve this problem? Consider the following two scenarios:

1: long link is periodically broken. After a period of time, or a program executed which determines a large memory for queries, disconnect, then reconnect to query again.

2: MySQL 5.7 or later, may be executed after each of a relatively large operation to re-initialize the connection by performing mysql_reset_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.

 

Query cache (Query Cache)

After the connection is established, they begin to execute a select statement, will first query cache before execution.

After get MySQL queries, the query cache will first see is not executed this statement. The statement is executed and the results are saved in a certain area of ​​memory key-value pairs to form. key statement is a query, value is the result of the query. If you can find the key query directly in the cache, then the value will be returned directly to the client.

Cache misses reasons:

Query can not be cached, probably because the query contains a function of uncertainty, or query that is not too much cache

Mysql never been treated this query

Before the cache, but the query cache memory runs out, he was expelled

The query cache is not yet complete warm-up, that is not enough time to Mysql query results are cached

Cache invalidation operations too, such as caching debris, insufficient memory, data modification will result in a cache miss

optimization:

Reduce fragmentation, choose the right query_cache_min_res_unit can reduce memory fragmentation caused by waste

Improve query cache usage

In the appropriate particle size, with a plurality of small tables instead of one large table

Just do a batch cache invalidation writing, write better efficiency compared to single

Control the size of buffer space, because the cache to avoid too much space, when expired operation may cause the server dead

 

If the statement is not cached query execution phase, it will continue behind. After the execution is completed, the results will be stored in the query cache. If the query cache hit, MySQL does not need to perform complex operations later, you can return the results directly, will improve efficiency.

But the query cache expiration very frequently, as long as there is an update to a table, all of this will be on the table query cache is emptied. Update pressure for large databases, the query cache hit rate is very low. If you need to have a static list of business, it will be updated every time. For example, a system configuration table, query on this table that is suitable for use query cache. MySQL provides this on-demand use. Query_cache_type parameter can be set to DEMAND, for the default SQL statement will not use the query cache. For you sure you want to use the query cache statement, you can explicitly specify a SQL_CACHE, as follows:

Mysql> select SQL_CACHE * from user_info where id = 1;

 

Analyzer (Analyzer)

If the query cache miss, we must begin the sentence. First, MySQL needs to parse the SQL statement.

First analyzer will do lexical analysis. SQL statement is composed of a plurality of strings and spaces, MySQL need to identify what is inside the string, respectively, representative of what. MySQL identified from select keyword you enter, this is the query. It should also be recognized as a character string user_info table name, the string id identified as the column name. After parsing to do it. According to the results of lexical analysis, according to the rules of grammar parser will determine the input SQL statement meets the MySQL syntax.

If your SQL statement is wrong, you will receive You have an error in your SQL syntax errors reminder

 

Optimizer (Optimizer)

After lexical analysis and parsing analyzer, but also 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

 

Actuators (Actuator)

The beginning of the implementation, you must first determine what user_info not have permission to perform queries on this table, if not, it will return an error without permission

If you have permission, you open the table to continue. Open the table, the actuator will be defined according to the table of the engine, this engine provides the interface to use the

Published 50 original articles · won praise 2 · Views 2267

Guess you like

Origin blog.csdn.net/eafun_888/article/details/104738590