MySQL SQL statement optimizer explain keywords

Outline

At this great data, flow of the times, our website is not only traffic flow, the amount of data will be very great. We need to find out the exact data of the huge amount of data, this database relatively great pressure on us. And when we do not consider the operation of sub-library sub-table, a SQL statement optimization is a good solution, here explainkeyword analysis SQL statements and use indexes to optimize queries.

explainKeyword

explainUse the format

	EXPLAIN SELECT
	*
	FROM
		SCORE
	WHERE
		CNO = '3-105'
	AND DEGREE > (
		SELECT
			DEGREE
		FROM
			SCORE
		WHERE
			SNO = 109
	)

search result

id select_type table type possible_keys key key_len ref rows extra
1 PRIMARY SCORE ALL null null null null 12 Using where
2 SUBQUERY SCORE ALL null null null null 12 Using where

explainForm each attribute analysis

  • id: Select the query sequence number, contains a set of numbers indicating the order of clauses or select a query execution operation table
  1. id is the same, the order from top to bottom
  2. id not the same, if a sub-query number is incremented id, id greater the value the higher the priority, the first to be executed,, id value greater in all groups higher priority
  • select_type: Sort, main difference is normal query, the union query, sub query and other complex queries
  1. simpleA simple query, does not contain a subquery
  2. primary: Contains sub-queries, queries for the outermost
  3. subquery: Subquery
  4. derived: Derived table, virtual table
  5. union: Keyword appears union
  6. union select: Union query results
  • table: Table name query
  • type: The information contained in the implementation plan, showing what type of query uses
type类型从最好到最差

system > const > eq_ref > ref > range >index > all
  1. system: The table is only one record (equal to the system tables), which is constkind of a special case, usually does not occur, this can be ignored.
  2. const: Represents the time by an index to find, constfor comparison primary key, or unionthe index, because only one row of data match, so soon, as will be placed where the primary key.
  3. eq_ref: Unique index scan, for each index key, only one record in the table match. Common primary key or unique index scan
  4. ref: Non-unique index scan returns all rows that match the conditions. In essence is a kind of index, he returns all matching an individual worthy of all the lines, however, he may have to find more qualified row, so he should belong to finding and scanning to obtain a mixed body.
  5. range: Retrieve only a given range of the line, using the index to select the rows. key column shows which index to use, generally appear on your statement where the between, <,>, in other inquiries, such a range scan is better than a full table scan, because a certain point in the index only needs to start, and a certain end point, not a full table scan.
  6. index: Full index scan, indexand allthe difference is indexthe type of traversing the index tree only. This ratio is usually allfaster, because the index file is usually smaller than the data file (that is to say though alland indexare full table scan, but indexis scanned from the index file allis read by the hard drive)
  7. all: Full table scan
  • possible_keys: Displays the index may apply to the query, one or more. Queries related to the existence of the index fields, will be listed in the index, but not necessarily used in the query.
  • key: Actual query the index to use. If it is null, then do not use the index, if used to cover the index, the index only appears in keythe list.
  • key_len: Shows the number of bytes used in the index, the column can be calculated by using the query to the index length. Without loss of accuracy, a length as short as possible. key_lenThe maximum length value is displayed in the index field, not the actual length, i.e., key_lenis calculated on the basis table definition, have not retrieved by the table.
  • ref: Columns show which indexes are used, if possible, it is a constant. Which columns or constants are used to find the value of the index on the column.
  • rowsAccording to statistics the number of rows and table selection index case, a rough estimate of the required record is found to be read:
  • extra: Not suitable for display in the other columns, but important information
  1. using filesort: MySQL will be described using an external data ordering index, instead of reading according to the table index. MySQL can not use indexes to complete a sort operation called 文件排序. If this happens, the query will be very slow
  2. using temporary: Note use temporary tables to hold intermediate results, MySQL in the query results are sorted using a temporary table. Common in sorting order byand group by. This occurs inquiry will be very slow.
  3. using index: Indicates that the corresponding select operation using a covering index (covering index), prevent access to the data lines, good efficiency! If the same time using where, shows that the index is used to perform the index lookup; if not occur simultaneously using where, indicating that the index used to read to find non-executive action.
  4. using where: Use wherekeyword search
  5. impossible where: The total value of false clause, can not be used to obtain any tuple

Index optimization case

  • Single table indexing, query fields do not appear index range otherwise the situation will fail
  • Two tables join query indexing, the index generally built in from the table, left connection, the establishment of the right; the right connections, built on the left.

Failure index case (should be avoided)

  • Full match the value of my favorite (query in full compliance with indexed sequential)
  • Best left-prefix rule: If a multi-column index, the most left-prefix to comply with the law. It refers to a query from the leftmost column index of the beginning and not skip the index column (category trains, locomotives)
  • Not do anything (calculation function (automatic or manual) type conversion) on all columns, the index will lead to failure, resulting in a full table scan
  • Storage engine can not use the index in the right column of the range conditions
  • Try to use a covering index (an index only access queries), reducing select *
  • Is not equal to the use of (! = Or <>) can not be used when the index resulting in a full table scan
  • It is null or is not null can not use the index
  • begins with a wildcard like ( '% abcd'), MySQL index table scan will fail becomes perfected
  • Single string without quotation marks index will fail
  • Less or, with its connections can cause failure of the index

Alibaba specification

  • Recommended]: SQL performance tuning goal: to reach at least ranglevel requirement is reflevel, if you can be conststhe best.
    Explanation: constsSingle table can only have a matching row (primary key or unique index), data can be read in the optimization stage.
    refIt refers to the use of an ordinary index (normal index)
    rangof the index range search
    Counterexample: The results explain the table, type = index, the index of physical files full scan, speed is very slow, the index level is also relatively low range, and a full table scan is trivial
Published 27 original articles · won praise 28 · views 10000 +

Guess you like

Origin blog.csdn.net/justLym/article/details/105076001