MySql Performance Tuning three (explain / desc implementation plan)

Foreword

   explian / desc can help us analyze sql statement, write efficient sql statement, let mysql query optimizer can better work.
   The more mysql query optimizer can use the index as much as possible, the optimizer exclude rows of data, mysql find the matching rows of data faster.

usage

   explain/desc + sql

  explain select * from tbl_chain_bill where billid = 6

   

 
key value means
id 1 Query serial number
select_type SIMPLE Query Type
table tbl_chain_bill Tables or derived table name
type const Scan Type
possible_keys PRIMARY May use the index, more than
key PRIMARY The actual use of the index
key_len 8 Index length for use, without loss of accuracy, a length as short as possible
ref const Which column shows the index is used
rows 1 Scanning the statement how many rows
Extra (Null) sql statement additional information, such as sorting grouping, etc.

   analysis

     id: identification select clause indicates execution order. The same id, sequentially from top to bottom; the greater the id, the first run.

     select_type
  • SIMPLE : simple SELECT, not using UNION or subqueries;
  • A PRIMARY : If the query contains any complex sub-portion, the outermost layer is marked as a PRIMARY select;
  • The UNION : the UNION second or later SELECT statement;
  • The RESULT UNION : UNION results;
  • SUBQUERY : the first sub-query SELECT;
  • DERIVED : Derived table SELECT, FROM sub-clause of a query;
  • SUBQUERY Uncacheable : the result set can not be cached sub-queries.

     table: indicate which data from tables or derived tables.

     

     type: from top to bottom, from the poor to good performance

  • ALL : from start to finish full table scan;
  • Index : full-tree index scan, only traversing the index tree;
  • The Range : retrieve only given range, it is necessary to use an index to select the rows, the representative key index used;
  • Ref : Index matching columns for connection is not unique, such as where bill.billNo = billDetail.billNo;
  • REF EQ : columns for connection to match the index is a unique or primary key, such as where bill.id = billDetail.billID;
  • const : When the primary key by a constant to compare it, for example where primary key = 123;
  • System : a lookup table is only one row, when compared with constant primary key.

     extra: The following two means MYSQL extra appear can not use the index, the efficiency would be significantly affected, this should be optimized as much as possible.

     Using filesort represents MySQL will sort the results of an external index, rather than the order of the index read content from the table. It may be sorted in memory or on disk.

     Using temporary represents MySQL use temporary tables when sorting query results. Common in order by sorting and grouping query group by.

     Test instruction

    

     It should be noted that the above example of type = ref or eq_ref because of the presence of a unique index, if the connection conditions are not unique constraint, it is still less recorded on the left join query.

 


Guess you like

Origin www.cnblogs.com/mzhaox/p/11297932.html