当前位置:网站首页>Mysql45 lecture 01 | infrastructure: how is an SQL query executed?

Mysql45 lecture 01 | infrastructure: how is an SQL query executed?

2022-06-12 11:25:00 Blue cotton

MySQL 45 speak 01 | Infrastructure : One SQL How query statements are executed ?

MySQL Basic architecture diagram

  • In general ,MySQL Can be divided into Server layer and Storage engine layer Two parts .
  • Server layer Including connectors 、 The query cache 、 analyzer 、 Optimizer 、 Actuators etc. , cover MySQL Of Most core service functions , And all Built in functions ( Such as date 、 Time 、 Mathematics and cryptographic functions ), all Cross storage engine capabilities All implemented on this layer ( Like stored procedures 、 trigger 、 View etc. .)
  • Storage engine layer In charge of data Storage and extract . Its architecture pattern is plug-in , Support InnoDB、MyISAM、 Memory Wait for multiple storage engines . Now the most commonly used storage engine is InnoDB, It is from MySQL 5.5.5 The version began to become Default storage engine .
  • Different storage engines share one Server layer , From the connector to the actuator .

 Insert picture description here

The connector

  • The connector is responsible for establishing a connection with the client 、 Access permissions 、 Maintaining and managing connections .
  • The connection command is usually written like this :mysql -h$ip -P$port -u$user -p
  • Connect... In the command mysql yes Client tools , be used for Establish a connection with the server . In the completion of the classic TCP After shaking hands , The connector is about to start authentication Your identity , At this time, what you input is used User name and password .
  • If user name or password incorrect , I'll get one "Access denied for user" Error of , Then the client program End to perform .
  • If the user name and password Certification by , The connector will go to Permissions on the table Find out what you have jurisdiction . after , The permission judgment logic in this connection , Will depend on the permissions read at this time .
  • Even if you use Administrator account For this user Permission has been modified , also Does not affect the Permission to connect already exists . After the modification is completed , Only Create a new connection Will use the new permission settings .
  • When the connection is complete , If you don't follow up , This connection is in Idle state , You can show processlist life Let me see it .
     Insert picture description here
  • If the client does not move for a long time , The connector will Automatically disconnect it . By the parameter wait_timeout control Of , The default value is 8 Hours .

After the connection is disconnected , The client sends the request again , You'll get an error alert : Lost connection to MySQL server during query. At this time, if you want to continue , Just Need to reconnect to , Then the request is executed .

  • A long connection After the connection is successful , If the client continues to have requests , Always use The same connection .
  • Short connection It means that every time a few queries are executed, the connection is broken , Next time you inquire Re create another .
  • The process of establishing a connection is usually more complicated , Try to reduce the action of establishing connection in use , That is to say
    Try to use long connections .
  • problem : But after using all the long connections , You might notice , Sometimes MySQL Memory usage is rising very fast , This is because MySQL Memory temporarily used during execution is managed in connection objects . These resources will be disconnected when To release . So if long connections accumulate , May cause too much memory , Killed by the system (OOM), From now on It's like MySQL Abnormal restart .

  • solve :

    1. Regularly disconnect long connections . Use it for a while , Or it can be judged in the program that a large memory consuming query has been executed , disconnect , Then query and reconnect .

    2. If you're using a MySQL 5.7 Or later , You can do this after each large operation , Through execution mysql_reset_connection To reinitialize the connection resources . This process does not require reconnection and re-authorization , But it will restore the connection to The state when it was just created .

The query cache

  • MySQL After getting a query request , It will arrive first. The query cache have a look , Have you executed this statement before . Previously executed statements and their results may be key-value Yes In the form of , Is directly cached in memory .

    • If the query can be found directly in the cache key, So this value Will be Directly back to the client .
    • If the statement is not in the query cache , Will continue to follow Execution phase . After execution , The execution results are stored in the query cache in .
  • however In most cases, it is not recommended to use query caching , Why? ? Because query caching often does more harm than good .

    • Query cache failures are very frequent , As long as there is an update to a table , All query caches on this table will be cleared . therefore
      It's likely that you struggled to save the results , It's not in use yet , It's all cleared by an update .

    • For databases that are under pressure to update , The hit rate for the query cache will be very low . Unless your business is to have one Static table , It takes a long time to update . such as , A system configuration table , Then the query on this table is suitable for the query cache .

    • MySQL This is also provided “ According to the need to use ” The way . You can set the parameters query_cache_type Set to DEMAND, So for the default SQL Statements do not use the query cache . And for the language you decide to use query caching sentence , It can be used SQL_CACHE Explicitly specify , Like the following statement :

      mysql> select SQL_CACHE * from T where ID=10;
      
  • Be careful :MySQL 8.0 Version directly queries the entire block of the cache Delete the function 了 , in other words 8.0 At first, there was no This function .

analyzer

  • If the query cache is not hit , It's about time to actually execute the statement . The first thing we need to do is SQL Sentences do analysis .

  • The analyzer will do **“ Lexical analysis ”**. What you enter is a string with multiple Spaces SQL sentence ,MySQL You need to identify the strings in it , What is the .

  • Then do it **“ Syntax analysis ”**. According to the result of lexical analysis , The parser will follow the grammar rules , Judge the one you typed SQL Does the statement satisfy MySQL grammar .

    • If your statement is wrong , Will receive “You have an error in your SQL syntax” Error warning

    • General syntax errors will prompt The first place where the error occurred , So what you want to focus on is immediately “use near” The content of .

Optimizer

  • Before we start executing , It's also handled by the optimizer .

  • The optimizer has... In the table Multiple indexes When , Decide which index to use ; Or there is... In a statement More than a table (join) When , Determine the... Of each table Connection sequence .

    Let's say you execute the following statement , This statement executes two tables join:

    mysql> select * from t1 join t2 using(ID)  where t1.c=10 and t2.d=20;
    
    • You can start from the table t1 The inside out c=10 The record of ID value , According to ID Values are associated to tables t2, To determine t2 Inside d The value of is No equals 20.
    • You can also start from the table t2 The inside out d=20 The record of ID value , According to ID The value associated with the t1, To determine t1 Inside c Is the value of be equal to 10.
  • Of these two execution methods The logical result is the same Of , however The efficiency of execution will vary , The role of the optimizer is to decide which scheme to use .

actuator

  • MySQL adopt analyzer I know you want to What do you do , adopt Optimizer I see. It's time How do you do it? , So I entered actuator Stage , Start Execute statement .

  • At the beginning of execution , Let's first judge what you do to this watch T There is no way to execute the query jurisdiction

    • without , Will return no Permission error , 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 authority , Open the table and continue . When I open my watch , The actuator will follow the table engine Definition , Use this guide
      The interface provided by Qing .

  • The execution flow of the actuator :

    • For example, the table in our example T in ,ID Field has no index :
    1. call InnoDB Engine interface take The first row of this table , Judge ID Value is 10, If not, skip , If so Save this row in the result set ;
    2. Call the engine interface “ The next line ”, Repeat the same logic of judgment , Until you get to the last row of the table .
    3. The actuator consists of all the rows that meet the conditions in the above traversal process Recordset As Result set Return to the client .
  • about Indexed tables , The execution logic is similar . The first time I call this “ Let's take the first row that satisfies our condition ” This interface , after Recycle “ That satisfies the next row ” This interface , These interfaces are all defined in the engine .

  • Database Slow query log I saw one in the rows_examined Field of , Indicates this statement How many lines were scanned during execution . Every time the executor calls the engine to get a data row Cumulative .

  • In some cases , The executor is called once , Inside the engine, it scans multiple lines , therefore The number of engine scan lines follows the number of engine scan lines rows_examined rows_examined It's not exactly the same .

Summary

  • The connector : Establishing a connection
  • The query cache (8.0 Deleted )
  • analyzer : What do you do
  • Optimizer : How do you do it?
  • actuator : Start execution
原网站

版权声明
本文为[Blue cotton]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121120532189.html