mysql index optimization and key fields explain explanation

 

A, explain explain keywords

        

1、id

    The serial number of MySQL QueryOptimizer selected query execution plan, represent the query execution select  the order of clauses or operating table. id  value greater the higher priority , the first to be executed. id  same , execution order from top to bottom.

2、select_type

    (1) SIMPLE

        Simply select queries (not using union and sub-query).

    (2) PRIMARY

        The outermost layer of select query.

        If two query table exists, the operation of the outer table PRIMARY, an inner layer (subquery) is operative to SUBQUERY.

        If the two tables do union, the operation of the outer table PRIMARY, an inner layer (hereinafter union) operation of union.

       

    (3)SUBQUERY/ DEPENDENT SUBQUERY

       SUBQUERY: subquery in the first SELECT (if multiple sub-queries exist), does not depend on the outer table. In addition to sub-clause contains a query from the outside , elsewhere in sub-queries may be subquery.

       DEPENDENT SUBQUERY: the first sub-query select (if multiple sub-queries exist), but depends on the outer table.

      

    (4)UNION/ DEPENDENT UNION

      UNION: union operation, in the inner layer (the second or subsequent) select a query, the result set does not rely on external queries.

      DEPENDENT UNION: union operation, in the inner select queries, but the inner and outer SELECT statement SELECT statement has dependencies.

      UNION RESULT: The results of the union operation, id value is usually NULL.

     

   (5)UNCACHEABLE SUBQUERY/UNCACHEABLE UNION

     MATERIALIZED: materialized subqueries

     UNCACHEABLE SUBQUERY: For outer main table, the subquery is not materialized, needs to be calculated each time (time-consuming operation).

     UNCACHEABLE UNION: union operation, the inner layer can not be materialized subqueries.

   (6)DERIVED

     Driven SELECT subquery for  the from clause, there are sub-query conditions. MySQL will recursively these sub-queries, the results in a temporary table (from the words that appear in a subquery, also called derived tables, other databases may be called an inline view or nested select).

     

3、table 

     Output lines referenced table. Shows a lookup table name, if the query using an alias, then here is an alias, do not involve the operation of the data table, then this display is null, if the display is enclosed in angle brackets of <derived N> would indicate that a temporary table, behind the N is the implementation plan id, show the results from this query to produce. If the angle brackets <union M, N>, and <derived N> Similarly, a temporary table also shows the results from the query id union of M, N of the result set.

 4、type

     Excellent to poor sequence is as follows :( red logo is a common level.) In addition to all, the other type can be used to index, in addition to index_merge, the other type can only be used in an index.

System-->const-->eq_ref-->ref--> fulltext-->ref_or_null-->index_merge-->unique_subquery-->index_subquery-->range-->index-->all.

Their meanings are as follows:

(1)system

        Table only one row (= system table). This is a special type of connection const. Only one row of data in the table or the table is empty, and only a memory table and myisam. If the engine is Innodb table, type columns or index usually are all in this situation.

(2)const

       PRIMARY KEY const for comparison with a constant value. Or using a unique primary key index , the records must be returned where conditions 1 equivalent of the rows, typically type is const. Also known as the only other database index scans. When only one row table query, use System.

 

    (3)eq_ref

      Appear to be connected in this table query plan , jointly for each record are read from the table in front of a record from the table, driving table returned only one row of data, and this line of data is the primary key of the second table or unique index, and must not null, it uses an index-based query all the time or unique key is used. A primary key is a unique index and multi-column, all columns are used as the only appears when comparing eq_ref .

      For example: a left join b, the conditions must be coupled to the primary key of the table or unique index b. If the primary key indexes are combined, the sequence of connections with the main key. Dim_company_app primary key is (company, merchant_mo)

       

(4)ref

     Unlike as in claim eq_ref connection order, did not require a primary key and a unique index can occur as long as the condition of equal use to retrieve, and the auxiliary common to find equivalent index. Or multi-column primary key, unique index, the column other than the first column will appear as the equivalent to find, in short, not only to return to find the equivalent of data can occur.

        

(5)fulltext

        Full-text index search, to note the high priority the full-text index, if the full-text index and the general index exist, mysql regardless of the price, prefer to use full-text indexing.

(6)ref_or_null

        Like ref, but MySQL null entries must be found in the results of the initial find, and then find the secondary. Compared with ref methods, just adds a null value comparison, with much practical.

(7) index_merge

       Description Index Merge optimization is used. It indicates that the query uses more than two indices, finally taking the intersection or union, common and, or the conditions of use of a different index, the official sort after ref_or_null, but in fact due to be read more than one index, the performance may be large not as part of the time range.

(8)unique_subquery

       For where the child in the form of a query, sub query returns duplicates of unique values . Use of this type in certain IN query, rather than the conventional ref: valueIN (SELECT primary_key FROM single_table WHERE some_expr).

(9)index_subquery

       Is used in the form of a subquery or a secondary index to a constant in the list subquery may return duplicate values , you may be used to re-index the subquery. Use of this type of species, and unique_subquery similar, but non-unique index in certain queries IN query.

(10)range

      Index range scan, common in use>, <, is null, between, in, like other operators query. Only retrieves a row of a given range, using an index to select the rows. column shows which key to use the index. When use =, <>,>,> =, <, <=, IS NULL, <=>, BETWEEN or IN operator, comparing the keyword column with a constant, it may be used range.

        

(11)index

      Full table scan index, the index scan again from beginning to end, the use of common index column can process the data file does not need to read the query, the query may be used to sort the index or packet. Full table scan, but when scanning the table rather than in a row by index order. The main advantage is that it avoids the sort, but the cost is still very large.

(12)all

Full table scan data file, and then returns the filtered recording layer to meet the requirements in the server. The worst case, from start to finish full table scan.

5、possible keys

     Indicates which index to use in this table can help query, the query might use the index will be listed here. If it is empty, indicating that the index is not available.

 6、key

     Select the index from the actual use of possible_key, if NULL, then do not use the index. When select_type is index_merge, there may be more than two indices appear, others will only be here a select_type. In rare cases, MYSQL will choose less than the index optimization. In this case, use USE INDEX (indexname) to force the use of an index or with IGNORE INDEX (indexname) to force the index ignored MYSQL SELECT statement.

7, key_len

     Index length for processing of the query, without loss of accuracy, a length as short as possible. If it is a separate index, it is the entire length of the index included, if it is a multi-column index, the query may not be able to use all of the columns, the specific use to how many index columns, it will calculate where to go, not to use column, there will not be calculated into it. Pay attention to the value of this column, you count the total length of the multi-column index will know that there is no use to all the columns. To note, ICP characteristics mysql to use the index will not be included. Further, only the index length calculated key_len where conditions used, the sorting and grouping even if the index is used, it is not counted in the key_len.

 8、ref

     Display index which column was used. If the query is a constant equivalent to use, will be displayed here const, if the query is connected, will be displayed here is associated field driving table driving table execution plan, if the condition of the expression or function used, the condition or columns have an internal implicit conversion, there may be displayed as func.

9、rows

      You must check that the number of rows to return the requested data, i.e., the number of lines to be scanned.

10、extra

       The information in this column can show very much, there are dozens of commonly used are the following categories. Which, if Using filesort appear, Using temporary two means you can not use the index, the efficiency would be significantly affected. This should be optimized as much as possible.

(1) distinct: Use the keyword select distinc part.

(2) no tables used: without words from the query or query From dual.

(3) not in use () subqueries exists or not join query operators, this is called the reverse connection. That is, queries are usually connected to a query in the table, then check the appearance of anti connection is the first appearance of the query, then the query in the table.

(4) using filesort: Unable to use the index when ordering, this will appear. Common in order by and group by statement. There is no way to sort the existing index, the need for additional sorting recommended: according to sorting needs, to create the corresponding appropriate index.

(5) using index: back to the table does not need to query a query, you can get direct data queries through the index. Covering the use of the index, no data back to the table to get results, this result is good.

(6) using join buffer (block nested loop), using join buffer (batched key accss): BNL later versions 5.6.x optimization associated with the query, BKA characteristics. Mainly to reduce the number of cycles in the table are sequentially scanned and comparing the query.

(7)using sort_union,using_union,using intersect,using sort_intersection:

using intersect: when indicates the conditions employed and each index, which is to obtain information indicating a processing result from the intersection;

using union: indication or condition of each connection using the index, the information indicating the processing result acquired from the union;

using sort_union and using sort_intersection: Similar to the previous two corresponding, but they appear in the query with and and or informative, first check the primary key, and then sort-merge, in order to read the record and return.

(8) using temporary: indication of the temporary table to store intermediate results. Temporary tables can be temporary memory tables and disk temporary tables, the implementation plan does not show up, need to see the status variables, used_tmp_table, used_tmp_disk_table to see it. Need temporary table storing the result set, usually because no group by an index on columns. There also may be because while there are group by and order by, but the group by and order by column is not the same

(9) using where: represents a storage engine returns not all of the records that satisfy the query are, needs to be filtered at the server level. Query criteria and constraints into the inspection condition, prior to 5.6, according to the restriction condition storage engines can only scan data and return, then filtered according to the server layer back to check the condition data truly meet the query. After the ICP 5.6.x support characteristics, can also be pushed down to check the condition of engine storage layer, the data does not meet the inspection conditions and limitations, not directly read, thus greatly reducing the number of records stored scan engine. extra column displays using index condition

firstmatch (tb_name) (10): One of the new features were introduced in 5.6.x optimization sub-queries, common in the where clause contained in () types of sub-queries. If a greater amount of data in the table, this can occur

(11) loosescan (m..n): When one of the new features introduced after 5.6.x sub-optimized query, in in () subquery type, there may be duplicate records returned by the subquery, this can occur

In addition to these, there are many query the data dictionary database, execute the planning process will find some tips impossible results.

 

Summary: One important number is the key, type, rows, extra, where the key is null, the description is not used to the index, the index needs to be adjusted. type of local all, need to be optimized. Generally need to achieve ref, eq_ref level, find the range needed to reach range. There are extra Using filesort, Using temporary necessarily optimized, the optimization can visually see the results according to the rows.

 

Reprinted from above and finishing:

https://www.cnblogs.com/xiaoboluo768/p/5400990.html

https://blog.csdn.net/HXNLYW/article/details/82979088

https://www.cnblogs.com/danhuangpai/p/8475458.htm

 

Guess you like

Origin www.cnblogs.com/dlp-527/p/11824467.html