EXPLAIN MySQL basic explanation of the implementation plan

A, EXPLAIN use the unspoken rules

  explain + sql statement

  For example: EXPLAIN SELECT * FROM `t_user`;

  

 

 

Second, the header fields Detailed

  (1) id -----> sequence table read

    Select query sequence number, contains a set of numbers indicating the order of clauses or select a query execution operation table

    Its value in three ways:

      <1> same id, the order of execution (to be understood that the order for table mysql loading) from top to bottom

      <2> id are not the same, if a subquery, id is incremented sequence number, the larger the value of id, the first to be executed

      <3> the same id does not exist the same, if the same id, may be considered as a group, performed from the top down; where the group, the greater the first implementation id

  Operation Type (2) select_type ----> data read

    常见值: simple, primary,subquery,derived,union, union result

      <1> simple, easy to select, query or subquery does not contain union

      <2> primary, if the query contains subqueries any complex part, the outermost query were labeled primary

      <3> subquery, contains a subquery in the select list or where,

      <4> derived, fromg included in the list is marked as derived subquery (derivative), MySQL recursively executing the sub-queries, and places the result in the temporary table

      <5> union, when the second select occur after the union, were labeled union; if contained in the union of the from clause subquery, the outer select labeled derived.

      <6> union_result, obtaining results from the union table select.

   (3)table

   (4)  type

      Common values: all, index, range, ref, eq_ref, const, system, null

       It indicates that the query what type to use. Excellent to poor sequence: system> const> eq_ref> ref> range> index> all 

       Work, at least have to ensure that the range query level, most to be ref.

      <1> system, only one record in the table (corresponding to the system table), which is a special case of type const.

      <2> const, once through the index to find, const for comparing primarykey or unique index, only one row of data matching, all queries super fast. As will be placed where the primary key list, mysql will convert the query to a constant.

      <3> eq_ref, unique index scan, for each index key, the table is only one matching record, the primary key used with a unique index scan.

      <4> ref, non-unique index scanning, returns all rows matching a separate value. Is a kind of index access In essence, it returns all rows that match a single value, however, it is possible to find more qualified rows, so it should look for properties and mixing operations scan.

      <5> range, only to retrieve the specified range of rows, using an index to select the rows, key column shows the use of the index, is the general appeared between the where clause, <,>, in other inquiries. This scanning range than the full table scan better efficiency, because it starts with a certain value, ends at another value, not a full table scan.

      <6> difference index, Full Index Scan, index and all of that index only traversing the index tree, it is certainly faster than all, because the index file is usually smaller than the data file.

      <7> all, a full table scan, the amount of data we should consider adding millions indexed.

    (5) key / possible_keys -----> index for determining whether a failure, those with an index in the end

      possible_keys: this table shows the possible application to the index, one or more. Queries related to the field if there is an index, the index will be listed, but not necessarily the actual query.

      key: the index of actual use, if NULL, then do not use the index. If the query uses the coverage index, the index appears only in the key list.

   (6) key_len: Index field indicates the maximum possible length, not the actual length, in the case where considerable accuracy, the smaller value is better. So the query precision and size key_len is contrary.

   (7) ref: display column index that is used, if possible, is a constant. That is, which columns or constants are used to find the value of the index column.

   (8) rows: how many rows being queried, the smaller the better

   (9) Extra additional information

        <. 1> the Using filesort : MySQL will use an external data ordering index, rather than the order in the table by index reading. mysql can not be done using the index to sort the situation ... pit father called the "Sort files", internally generated secondary sort needs to be optimized.

      

 

       Analysis: Creating a composite index idx_col1_col2_col3, but the first query to use an index column order is col1, col3; second query using the index to the column order is col1, col2, col3, just indexed columns are all created with to, and the order are consistent. Contrast these two queries can be seen in the scene using filesort appear.

         <2> the Using tempoary   : Using filesort than even pit father, built a temporary table inside, common in order by and group by,

      

 

        group by either not indexed or indexed columns must be the same with the order, otherwise Using tempoary appeared.

    <. 3> the Using index   : represented by a corresponding select operation using a covering index (covering index), to avoid accessing the data row of the table, good efficiency!

             If there is a using where at the same time, shows that the index is used to perform a lookup index key values; if no using where at the same time, shows that the index is used to read data instead of performing a lookup operation.

      

 

       There have been using where, represents the index is used to perform a lookup index key value, the index is two col1, col2, only to find col2.

 

      

 

       Using where at the same time does not appear, indicating that the index is used to read data rather than performing a lookup operation. Read col1, col2 the data is in the index, so do a query from the table, the index can only direct reading.

      

      Added: coverage index

          Column select the data that can be acquired only from the index, without reading the data row, MySQL can use an index to return the select list of fields, without having to read the data file again according to the index. In other terms of the sentence is to be covered by a query column built index column. select * basic coverage index does not appear. . . .

      <4> using where: Filter indication where 

      <5> using join buffer: using the connection cache,

      <6> impossible where: where clause is always false, can not be used to obtain any data.

        explain select * from user  where gender = '男' and gender = '女';

      <7> select tables optimized away in the case where no group by clause, based on the index optimization min / max operations or for storage MyISAM SEO count (*) operation, without waiting for the execution phase and then calculated, i.e., the query execution plan generation stage complete optimization.

      <8> distinct: Optimization of distinct operations

        

      

    

 

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/11545575.html