MySQL database optimization Overview 2 _ MySQL

Source: Internet
Author: User
MySQL database optimization Overview 2 the EXPLAIN statement can be used as a synonym for DESCRIBE, or used to obtain information about the SELECT statement to be executed by MySQL. The EXPLAIN tbl_name syntax is the same as DESCRIBE tbl_name or show columns from tbl_name.
Ref_or_null
This connection type is similar to ref. The difference is that MySQL will search for records containing NULL values during retrieval. This connection type is optimized from MySQL 4.1.1 and is often used for subqueries. In the following example, MySQL uses the ref_or_null type to process ref_table:


SELECT * FROM ref_table
WHERE key_column = expr OR key_column is null;


Index_merge
This connection type means that the Index Merge optimization method is used. In this case, the key field includes all the indexes used, and the key_len includes the longest part of the key used. For details, see "7.2.5 How MySQL Optimizes OR clses ".

Unique_subquery
This type replaces ref with IN subqueries IN the following form:

Value IN (SELECT primary_key FROM single_table WHERE some_expr)

Unique_subquery is only used to replace the index lookup function of a subquery, which is more efficient.

Index_subquery
The connection type is similar to unique_subquery. It uses a subquery to replace IN, but it is used when there is no unique index IN the subquery, for example, the following form:

Value IN (SELECT key_column FROM single_table WHERE some_expr)

Range
Only records within a given range are retrieved and an index is used to obtain a record. The key field indicates which index is used. The key_len field contains the longest part of the key used. In this type, the ref field value is NULL. Range IS used to compare a field and a field with any of the following operators: =, <>,>, >=, <, <=, is null, <=>, BETWEEN, or IN
SELECT * FROM tbl_name
WHERE key_column = 10;
SELECT * FROM tbl_name
WHERE key_column BETWEEN 10 and 20;
SELECT * FROM tbl_name
WHERE key_column IN (10, 20, 30 );
SELECT * FROM tbl_name
WHERE key_part1 = 10 AND key_part2 IN (10, 20, 30 );

Index
The connection type is the same as that of ALL. The difference is that it only scans the index tree. It is usually faster than ALL because the index file is usually smaller than the data file. MySQL uses this connection type when the queried field knowledge is a separate part of the index.

ALL
Scan all the tables and combine the records obtained from the previous table. At this time, if the first table is not identified as const, it is not very good. In other cases, it is usually very bad. Normally, you can add indexes to quickly retrieve records from the table to avoid ALL.

Possible_keys

The possible_keys field indicates the index that MySQL may use when searching table records. Note that this field is completely independent from the table sequence displayed in the EXPLAIN statement. This means that the indexes contained in possible_keys may not be used in actual use. If the value of this field is NULL, it indicates that no index is used. In this case, you can check which fields in the WHERE clause are suitable for adding indexes to improve query performance. In this case, create an index and then use the EXPLAIN command to check the index. For details, see section "14.2.2 alter table Syntax ". If you want to see what indexes are available for the table, you can use show index from tbl_name.

Key

The key field shows the indexes actually used by MySQL. When no index is used, the value of this field is NULL. To enable MySQL to forcibly USE or IGNORE the INDEX list in the possible_keys field, you can USE the keyword force index, use index, or ignore index in the query statement. For MyISAM and BDB tables, you can use analyze table to ANALYZE which index is better. For tables of the MyISAM type, the same effect is achieved by running the command myisamchk -- analyze. For details, see chapter "14.5.2.1 analyze table Syntax" and "5.7.2 Table Maintenance and Crash Recovery ".

Key_len

The key_len field shows the index length used by MySQL. If the value of the key field is NULL, the index length is NULL. Note that the value of key_len tells you which indexes MySQL uses in the Union index.

Ref

The ref field shows which fields or constants are used together with keys to query records from the table.
Rows

The rows field shows the number of records that MySQL considers to be retrieved in the query.

Extra

This field displays the additional MySQL information in the query. The following is an explanation of several different values of this field:

Distinct

When MySQL finds the first record of the matching Union result of the current record, it no longer searches for other records.

Not exists

When MySQL performs a left join optimization during query, when it finds that the current table matches the left join condition with the previous record, it will no longer search for more records. The following is an example of this type of query:


SELECT * FROM t1 left join t2 ON t1.id = t2.id
WHERE t2.id is null;


If t2.id is defined as not null. In this case, MySQL scans table t1 and searches for records in T2. When a matched record is found in T2. this means that t2.id will not be NULL, and other records with the same id value will not be searched in T2. It can also be said that for each record in t1, MySQL only needs to perform a search in T2. no matter how many matching records actually exist in T2.

Range checked for each record (index map :#)

MySQL does not find an appropriate available index. Instead, for each row join in the previous table, it performs a test to determine which index to use (if any ), use this index to retrieve records from the table. This process is not very fast, but it is always faster than doing table join joins without any indexes.

Using filesort

MySQL needs to perform an additional step to obtain records in an ordered order. The ordering program traverses all records based on the connection type, and stores the keys to be sorted and pointers to records that meet the WHERE condition. These keys have been sorted out, and the corresponding records are also sorted out. For details, see "7.2.9 How MySQL Optimizes order ".

Using index

The field information is obtained directly from the index tree, instead of scanning the actual records. Fields used for query are part of an independent index.

Using temporary

MySQL needs to create a temporary table storage result to complete the query. This usually occurs when the query contains the group by and order by clauses, which list fields in different ways.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.