Mysql combat stress study details 45 ---- how a SQL query is executed?

In general, MySQL Server can be divided into two parts layer and the storage engine layer.

 

 

 

 

select * from T where ID=10;

Execution of this query:

External layer:

  Users interact with the server layer media

    A. [Client for connecting to the database, enter the command / statement]

      Interface of connecting to the database

      输入 select * from T where ID=10;

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

    II. The connector [connector is responsible for establishing a connection with the client, access permissions, maintain and manage connections. ]

      Enter the command connection authenticate the identity --mysql -h $ ip -P $ port -u $ user -p
        authentication: connector will query the user access rights table  

           P: After all jurisdictions this time-dependent determination logic will read permissions

              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.

        Authentication failed: Access denied for user

      Once connected, you can use the command: show processlist view status

      Command column reads "Sleep" in this line, it means that now the connection is idle

      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.

      Connection divided into two types: short and long connection [connection]

        definition:

          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 pros and cons:

          Establishing a connection process is complicated to make use of long connection, a connection operation to reduce

          All use excessive growth of long connection memory (PS: Memory Manager MySQL in the implementation of the temporary use in connection objects, these resources only released when the connection is lost, the cumulative long connection memory footprint is too large, the system will be killed off phenomenon is abnormal restart)

        Optimization of drawbacks:

          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.

          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.

    III. Query Cache

      After the connection is established, the implementation phase

        When MySQL got a query request, you will see the key value in the first-query cache, if hit directly back to return a result.

          PS: statement that was performed before the cache and results in the form of key-value pairs into memory, {key (query): value (query result)}

        Drawbacks:

          Query cache expiration - as long as table updates, all cached queries on this table will fail. . so only use half of the query cache is not updated in a very long time before the table, such as: system configuration table

          If you do not use the query cache function, DEMAND parameter query_cache_type provided, and for the need to use the statement may be used "SQL_CACHE" specified individually out: select SQL_CACHE * from T where ID = 10;

    IV. Analyzer

      Now is the query does not find the key value in the query cache, so it needs to parse the statement.

        ①, lexical analysis

          Statement entered by the strings and a plurality of spaces, MySQL need to identify what is inside the character string, respectively, representative of what

          For example: MySQL identified from your input "select" keyword, which is a query.

        ②, parsing

          Error You have an error in your SQL syntax, concern is immediately "use near"

    V. Optimizer

      The role of the optimizer is to determine the implementation of the program

      After analyzer, MySQL already know what you want to do, now need the optimizer to determine which implementation of the program, for example:

        select * from t1 join t2 using(ID) where t1.c=10 and t2.d=20;

        This statement is t1 and join t2 two tables, it will have to perform a variety of ways, although the same result but the efficiency will be very different

    VI. Actuators

      MySQL through the analyzer know what to do, we know how to do it by the optimizer, and now to the actuator, into the implementation phase of the statement

      ①, judging authority

        To determine whether you have the authority to query (at this time to determine the basis for reading the connector permissions), you can open the table if there privilege to continue.

      ②, judgment engine

        Open the table, the actuator will be defined according to the engine table, use the interface to the engine.

      ③, the statement is executed

          select * from T where ID = 10; execution of this query is - [table T, ID field is not indexed]

            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;

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

            Actuator traversal above all the rows satisfy the condition set record consisting of as the result set returned to the client.  

Storage layer:

  Storage and retrieval of data storage engine layer is responsible. Its architecture model is the plug-in, support for InnoDB, MyISAM, Memory, and other storage engine. Now the most commonly used storage engine InnoDB, MySQL 5.5.5 version of it from the beginning became the default storage engine.

  create not specify a default table engine engine used when construction of the table, can be added in the statement engine = memory specified engine

 

Additional:

   If an error T is not a field k, and you execute this statement select * from T where k = 1, it certainly will be reported "does not exist in this column" Table: "Unknown column 'k' in 'where clause'". Do you think this error is reported out of it at what stage we mentioned above?

[Understanding] personal

   Grammar and lexical analyzer will be analyzed to determine, at this stage will judge the correctness of the statement

Guess you like

Origin www.cnblogs.com/lvzhenhua/p/12620880.html
Recommended