1.MySQL Infrastructure

Long time no post to a blog, and finally finished school a little knowledge and were consolidated. From the beginning of this series we will continue to adhere to output MySQL few month.

Statement about this series is the MySQL have a foundation for the junior partner, use the SQL on some of the concepts introduced here will no longer write.


First we look at a simple picture, learning MySQL infrastructure:

   MySQL architecture of the body can be divided into: Server layer and the storage engine layer. Server layer includes a connector, query cache analyzer, optimizer, executor. All across storage engine functions are implemented in this layer.

We first introduce the statement of a query, MySQL implementation process:

  1. Application server to establish a connection to the database server
  2. Sql database processes to get the request
  3. Parse and generate execution plan, execute
  4. Data is read into memory and logic
  5. By connecting, the step of sending the result to a client
  6. Turn off the connection, the release of resources

According to this process, we have to introduce in detail the concept and meaning of each component.


 Connector:

  Responsible for establishing a connection with the client, access permissions, maintain and manage connections. After the connection is established via TCP handshake, the connection starts to authenticate users.

  Each client will have a connecting thread in the server process, this connection will only execute a query in this separate thread. After MySQL5.5, thread pool to support plug-ins, you can use a small amount of thread pool to service the large number of connections, do not need to create or destroy threads for each new connection.

Connector authentication:

  Username + Password + original host information

  Connect using SSL, you may also be used X.509 certificate authentication

Verify the results and treatment:

  The validation fails, returns Access denied for user error, the client program ends execution.

  Authentication is successful, the connector will detect the user has rights in the rights table. After that, the authority to determine where in this connection will depend on the authority now read. A successful connection, even if the administrator modifies the user rights, does not affect the rights existing connections.

Added: connection establishment process is more complex, should minimize the operation of the link, the use of long connection. Note: MySQL temporary memory used during execution is to manage the connection object inside, to release these resources when a connection is dropped, a long connection may lead to cumulative memory footprint is too large, the system is forced to kill.

Solve this problem, there are two common scenarios:

  After long connection periodically disconnected, or a program is judged too much memory for queries, disconnect and then reconnect.

  After MySQL5.7, may be performed each time after a large operation, performed mysql_reset_connection to reinitialize the connection resources. The reconnection process without permission and check, but the connection will be restored to the state just finished creating a state.


 Query cache:

  MySQL query to get a request, if the query cache open, MySQL will give priority to check whether the query cache miss data.

  This check is accomplished by a case-sensitive lookup hash. Queries and query cache, even if only one byte different, it will not match the cached results. If you hit the cache, MySQL checks the user rights once before returning the result, no problem, skip the other stages, to get the results directly from the cache and returned to the client. After the no subsequent logic, and to get the result is stored in the query cache DB returned from the client.

 Queries use the cache:

  Updates to the table will result in all of the query cache is emptied on the table, for frequently updated database to find the cache hit rate is very low

  For static table, a very long time to update a table, query cache can be suitably employed.

After MySQL8, query caching function is deleted, the previous version by: query_cache_type = DEMAND, set whether to use the query cache, or displayed on a query using SQL_CACHE

SELECT SQL_CACHE * FROM dual

Query optimization:

  When there is no query cache hits to start the real implementation of the statement. At this stage SQL statement will be converted to an implementation plan, MySQL interact According to this plan and execute storage engine. The process involves a number of sub-phases: SQL parsing, preprocessing, optimization of SQL execution plans. We introduce Division:

Analyzer:

  MySQL statements must first know what to do, so will the SQL parsing, lexical analysis and parsing first. MySQL SQL statements at this stage will be resolved by keywords and generate a corresponding parse tree. It will use the grammar rules and resolve queries at this stage MySQL.

  It do include the following:

  •   Verify that using the wrong keywords
  •   Use keywords in the correct order
  •   Verify that the correct match of quotation marks around
  •        Determining whether the parse tree legitimate: to check the data tables and data columns exist, will parse the alias name and whether there is ambiguity

Optimizer:

  When the analyzer after the implementation, the syntax tree is now considered legitimate, the optimizer will convert it to an implementation plan. A query can have a lot of implementation, and finally return the same results. The main role of the optimizer is to find one of the best execution plan.

Other details about the query optimization phase, there will be a special subsequent articles are introduced.


 Actuator:

  MySQL statement by the parser know what to do, know how to do by the optimizer, after entering the actuator to start executing the statement.

  It will judge whether the user has permission to the table at the start of the implementation of appropriate action, if not, then do not have permission to return. On the other hand, continue to open the table. At this time, the actuator based on the table defined in the engine, the engine provides the interface to use, and then obtain the results returned to the client.

Guess you like

Origin www.cnblogs.com/zhangbLearn/p/12096840.html