MySQL infrastructure

TCP / IP stand it anymore, I think it is to start from the application layer, the next two months I will focus on learning algorithms and database-related knowledge, the network I have been unfamiliar, it looks professional books very difficult, it can be said to be a waste of time , the learning objectives database is the first direction to correct, then one of the data structures and algorithms to master, and then to learn about the underlying relational database.

 

Let's talk about from one line sql:

mysql> select * from T where ID=10;

 

 

MySQL can be separated into two layers, Server Engine layer and the storage layer.

Server layer including connectors, query cache, analyzer, optimizer, actuators, etc., covering most of MySQL's core service functions, as well as all of the built-in functions (such as date, time, math and encryption functions, etc.), all cross-Storage Engine functions are implemented at this layer, such as stored procedures, triggers, and other views.

The storage engine layer is responsible for data storage and retrieval. Its architecture model is the plug-in, support for InnoDB, MyISAM, Memory, and other storage engine.

In other words, when you execute create table construction of the table, if you do not specify the type of engine, the default is to use InnoDB. However, you can also select other engine types specified by the storage engine, such as the use of engine = memory in the create table statement to create a table using the specified memory engine.

It is obvious from the figure, different storage engines share a Server layer, i.e. to the actuator from the connector portion.

 

Connector

mysql -h$ip -P$port -u$user -p
  • 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 permission.

This means that a user after successful connection is 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.

Once connected, if you do not follow-up action, the connection is idle, you can see it in show processlist command. This is a result of the text FIG show processlist, wherein the Command column reads "Sleep" to this line, it means there is now a free connection system.

 

If the client is not too long movement, the connection will automatically be disconnected. This time is controlled by the parameter wait_timeout, default is 8 hours.

  msyql> set global wait_timeout=28800;

  msyql> set global interactive_timeout=28800;

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. At this time if you want to continue, we need to reconnect, and then perform the request.

Long database connection is a client request after a successful connection has been made and used for the same connection, the short link is the time to request a few times and then disconnect again next request will re-create the connection. To establish a connection is complex, it is a TCP connection, so try with a long connection.

But after all use long connection, you may find that there are times when MySQL memory for up 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.

way of solving the problem?

  1. 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.

  2. 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.

 

Query Cache

After MySQL to get a query request, the query cache will first look is not performed before this statement. It was performed before the statement and its results may be key-value pairs to form, directly cached in memory. 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.

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. It is likely that the results you struggling to keep up, not to use it, it was a full update emptied. Update pressure for large databases, the query cache hit rate is very low. Unless your business is to have a static table, it will be updated every time. For example, a system configuration table, query on this table that is suitable for use query cache.

The default MySQL query cache is not open, but you can specify the sql show

mysql> select SQL_CACHE * from T where ID=10;

MySQL 8.0 version directly query cache piece features deleted, that began 8.0 completely free of this function.

 

Analyzer

If you do not hit the query cache, you must start to really execute the statement. First, MySQL need to know what you have to do, and therefore needs to be done to resolve the SQL statement.

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."

After identifying these done, it is done "parsing." 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 your statement is wrong, you will receive the "You have an error in your SQL syntax" error reminder

"Lexical analysis" and "parsing"

Analyzer is also responsible for analyzing sql statement table name, the column name exists and so on.

 

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;

A detail that is using and on the difference between the latter more freedom.

  • 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 program to use.

After optimizer phase is complete, the implementation of the program statement to be finalized, and then enter the actuator stage.

 

Actuator

MySQL through the analyzer you know what to do, we know how to do that by the optimizer, so he entered the actuator stage, begin statement.

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 (in the project implementation, if the query cache hit, will query cache when the results back, do permission validation. precheck inquiry will call to verify permission before optimizer).

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:

  1. 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;

  2. Interface call engine take "next row", the logic repeats the same determination until get to the last row of the table.

  3. 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, the scan engine with rows_examined the number of lines is not exactly the same. As we will have a special internal mechanism for storing an article in terms of engine, which will be described in detail.

 

"MySQL combat" - Lin Xiaobin

 

Guess you like

Origin www.cnblogs.com/CherryTab/p/12008073.html