How in a SQL query in MySql

1.1 MySQL basic architectural overview

  • Connectors: authentication and permissions related (when the MySQL login).
  • The query cache: When execute a query, it will first check the cache (after removal of MySQL version 8.0, because this function is not practical).
  • Analyzer: if there is no cache hit, the SQL statement will be after analyzers, analyzers bluntly, is to look at your SQL statement to be doing, and then check your SQL statement syntax is correct.
  • Optimizer: According to MySQL think the best solution to do it.
  • Actuator: execute a statement, then returns the data from the storage engine.

 

 

Simply put MySQL Server is divided into layers and the storage engine layer:

  • Server layer: including connectors, query cache, analyzer, optimizer, actuators, etc., all across storage engine functions are realized in this layer, such as stored procedures, triggers, views, functions, etc., there is a common the log module binglog log module.
  • Storage Engine: mainly responsible for the data storage and retrieval, can replace the use of plug-in architecture that supports InnoDB, MyISAM, Memory, and other storage engine, InnoDB engine which has its own logging module redolog module. Now the most commonly used storage engine InnoDB, it will be treated as the default storage engine of MySQL 5.5.5 version from the start.

1.2 Server layer substantially Introducing

1) The connector

The main connector and authentication, and rights related functions, like the same level of a high guard.

It is responsible for the database user login, user authentication, including operating check account password, permissions, etc. If the user account password has passed, the connector will go to all the rights privileges table query the user's permissions after the connection in the logical decision at this time will always depend on the permission to read data, that is, as long as the follow-up connection is not immediate administrator changes the user's permissions, the user is also unaffected.

2) After the query cache (MySQL 8.0 version removed)

The main query cache used to cache SELECT statement we executed and the result set the statement.

After the connection is established, the time to execute a query, will first query cache, MySQL will first check whether this sql executed, the cache in the form of Key-Value in memory, Key is expected query, Value is the result set. If the cache key is hit, it will return directly to the client, if not hit, will perform follow-up operation, after the completion of the results will be cached, the next call convenient. Of course, when you actually execute the query cache will still check the user's permission, if there is query the table.

MySQL query cache is not recommended, because the query cache invalidation in actual business scenarios can be very frequent, if you update a table, then all of this will be on the table query cache is emptied. For not frequently updated data, the use of cache is still possible.

So, in general, in most cases we are not recommended to use the query cache.

After MySQL 8.0 version removed the cache function, the official is considered less functional in practical application scenarios, so just directly deleted.

3) Analyzer

MySQL did not hit the cache, it will enter the analyzer, the analyzer is mainly used to analyze the SQL statement is doing here, the analyzer will be divided into steps:

The first step, lexical analysis, a SQL statement has multiple strings, you must first extract keywords, such as select, raise queries, field names put forward proposed query and so on. And after that operation, they will enter the second step.

The second step, parsing, mainly sql judge you entered is correct, whether the MySQL syntax.

After completing these two steps, MySQL is ready to begin, but how to perform, how to perform the best result? This time we need to play the optimizer.

4) Optimizer

The role of the optimizer is that it considers the optimal implementation of the program to execute (and sometimes may not be the best, this article involves in-depth knowledge of this part of the explanation), multiple indexes such as how to choose the time of the index, multi-table how to choose the time of the query associated with the order and so on.

It can be said, after a optimizer can say that this statement has been executed specifically how to settle down.

5) Actuator

When you select the implementation of the program, MySQL is ready to begin, and before the first execution will verify that the user has no rights, if do not have permission, it will return an error message, if you have permission, will be to call the engine interface, the interface returns result of the execution.

2.1 query

Having said so much, then what a sql statement is how to implement it? In fact, our sql can be divided into two types, one is the query, one is update (add, update, delete). We first analyze the next query statement, the statement is as follows:

select * from tb_student  A where A.age='18' and A.name=' 张三 ';

In conjunction with the above description, we analyze the flow of execution of this statement:

  • Check whether the statement has authority, if no authority, directly returns an error message, if you have permission, in MySQL8.0 previous version, will first query cache to this key sql statement query results in memory if there is, if there is direct cache, if not, the next step.

  • Lexical analysis performed by the analyzer, extracting sql statement key elements, such as extracting a query statement above select, extract tables need, called tb_student, need to query all the columns, this query is a table id = '1'. Then determine whether the sql statement has a syntax error, such as whether the correct keywords, etc., if the check is no problem on to the next step.

  • Then there is the optimizer determined implementation of the program, the above sql statement, there are two implementation of the program:

      a.先查询学生表中姓名为“张三”的学生,然后判断是否年龄是 18。
      b.先找出学生中年龄 18 岁的学生,然后再查询姓名为“张三”的学生。
    

    The optimizer to choose the best execution efficiency of a program according to their own optimization algorithm (optimizer thinks, sometimes not necessarily the best). So after confirming the execution plan ready to be implemented.

  • Permissions check, without permission will return an error message if there privilege is invoked database engine interface, the results returned engine.

2.2 update statement

Above is a sql query execution process, and then the next we take a look at how an update statement is executed it? sql statement is as follows:

update tb_student A set A.age='19' where A.name=' 张三 ';

Let's change to Joe Smith at age certainly does not set the actual age of the field in the database, or to be the technical director of the play. In fact, the statement also basically go along on a process of inquiry, but will certainly be the time to perform the update log it, this module will be the introduction of a log, MySQL comes with a log modular binlog (archive log), all the storage engine can be used, we used InnoDB engine also comes with a log module redo log (redo log), we have to discuss InnoDB pattern down the flow of execution of this statement. Process is as follows:

  • Joe Smith to a first check this data, if there is a cache, the cache will be used also.
  • Then get the query statement, the age was changed to 19, then call the engine API interface, this line of data is written, InnoDB engine data stored in memory, also recorded redo log, redo log at this time prepare to enter the state, then informed the , perform complete, ready to be submitted.
  • Actuator after receiving the notification record binlog, and then call the engine interface, submit redo log for the submission of state.
  • update completed.

III summarizes

  • MySQL Server is divided into layers and the engine layer, layer mainly includes a connector Server, the query cache, the analyzer, optimizer, executor, as well as a log module (the binlog), the log module can share all execution engine, redolog there are only InnoDB.
  • Engine layer is plug-in, now including, MyISAM, InnoDB, Memory and so on.
  • Query execution process is as follows: Permission check (if a cache hit) --- "query cache ---" --- analyzer "optimizer ---" Permission check --- "Actuator ---" engine
  • Update statement execution process is as follows: Analyzer ---- "---- permissions check" Actuator --- "Engine --- redo log (prepare status ---" binlog --- "redo log (commit state )

Guess you like

Origin www.cnblogs.com/bulrush/p/12534278.html