SQL statement performance analysis

SQL statement performance analysis 
explain the implementation plan
Usage: explain select statement

command:
Show Database;
use MySQL
explain the SELECT * from the User;
results are as follows:
the above mentioned id SELECT_TYPE the Table of the type possible_keys Key key_len ref rows Extra
1 SIMPLE the User ALL NULL NULL NULL NULL 11

explain return results analysis
id
select identifier, the execution order on behalf of statements, usually when a nested query will select different
id columns larger the number the more the first implementation, if as large a number, then down sequentially executed from the

id as null would indicate this is a result set, do not need to use it to query.

SELECT_TYPE
Simple: indicates that no union operation or simply select the query does not include subqueries. When connected to a query, the outer query is
simple, and only one.
primary: a union operation or needs to select comprising subqueries, the outermost layer of select_type unit queries is the
primary. And only one.

union: two select union connection query, the first query is dervied derived tables, in addition to the first table, the second and subsequent
tables select_type are union
dependent union: the union, as appears in the union or union all statements in, but this query is subject to query external
influences.

union result: The result set contains the union, the union and the union all statements, because it does not participate in the query, so the id
field is null.
subquery: In addition to sub-sub from the words contained in the query, the query appearing elsewhere may It is a subquery.

dependent subquery: and dependent union similar, indicating that the subquery to be influenced by external query table queries.
derived: sub-queries from the words that appear, also called derived tables, other databases may be called an inline view or nested select.


table
lookup table name as it appears.
If the query uses an alias, so here is an alias.

If you do not involve the operation of the data table, then this is displayed as null.
If enclosed as angle brackets <derived N> display would indicate that the temporary table

type
in sequence from good to bad: system, const, eq_ref, ref , fulltext, ref_or_null, unique_subquery, index_subquery,
the Range, index_merge, index, ALL

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: only one row in the table or data table is empty, and only a memory table and myisam. If the engine is Innodb, this type listed
next are usually all or index

const: using a unique primary key or index, a return line record must be recorded contour. When the conditions where usually type is const. Other
databases also known as the only index scan.

eq_ref: appear in the query plan to connect multiple tables, the drive table returned only one row of data, and this line of data is the second table's primary key or
by a unique index, and must not null, unique index and primary key is a multi-column , only all columns as will appear when compared eq_ref.

REF: eq_ref not required as the connection order, did not require a primary key and a unique index, it is possible to use as long as the conditions of retrieval is equal to
Now, to find common equivalent auxiliary 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.

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

ref_or_null: Similar to the ref methods, only increased the value of comparing null . The actual use of much.

unique_subquery: where used in the form of subqueries, subquery returns a unique value unique values

index_subquery: subqueries for use in a secondary index to the list or in a constant, sub-query may return duplicates can
use the index to subquery deduplication.

range: scan index range, common in use>, <, is null, between , in, like other operators query.

index_merge: 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 ordering this after ref_or_null, but in fact due to be read multiple indexes, performance most of the time may not as the Range.

index: index full table scan, the index from start to finish sweep again, common in the column can use the index query processing does not need to read the data file,
you can use the query index sorting or grouping.

all: this is a full table scan data file, and then returns filtered to meet the requirements in the server recording layer.


possible_keys
inquiry into the possible use of the index will be listed here

key
when you query the index to actually use, select_type as index_merge, there may be more than two indices appear, other select_type
there will only be a.

key_len
for query processing index length, 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, where will be calculated in, no use to the column, there will not be calculated into it.

ref
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 expression or function of the conditions employed, or condition has occurred column internal implicit conversion, there may appear to be FUNC

rows
here is the number estimated in the implementation plan of the scan lines, not the exact value


Extra common return
distinct: use the keyword distinc (distinc in select parts: select only a single different values, you can go weight)
nO the Tables Used: from words without inquiry or inquiry from dual

using filesort: Unable to use the index when ordering, this will appear. Common in order by and group by statements
using index: do not need to return to the query table query, you can get the data directly to a query by index

using intersect: conditions of use and indicate when each index, which represents the information is obtained from the processing results intersection.
using union: when the indication or condition of each connection using the index, acquires the information indicating the processing result from the union.

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.



Guess you like

Origin www.cnblogs.com/jingzaixin/p/11424066.html