Explain MySQL performance optimization

Source: Internet
Author: User

1 useexplainStatement to see the results of the analysis, such as
Explain select * from Test1 where id=1;
will appear:
ID selecttype Table type Possible_keys keykey_len ref rows extra Columns

wherein, type=const means that through the index once found, key=primary words, the use of the primary key Type=all, expressed as a full table scan, Key=null is not useful to the index; Type=ref, because this is considered to be multiple matching rows, in a federated query, is generally ref


2 Combined index in MySQL
Assuming that the table has Id,key1,key2,key3, the three are formed into a composite index, then
Such as:
Where key1= ....
where Key1=1 and key2=2
where Key1=3 and Key3=3 andkey2=2
According to the leftmost principle, these are indexes that can be used.
Such as
From Test where key1=1 by Key3
With explain analysis, only the Normal_key index is used, but only the WHERE clause works, and the subsequent order by needs to be sorted


3 Using slow Query analysis:
in the My.ini:
Long_query_time=1
Log-slow-queries=d:\mysql5\logs\mysqlslow.log
record more than 1 seconds in the slow query log
You can use Mysqlsla to analyze it. can also be in the mysqlreport, like
DMS separately analyzes the percentage of select, Update,insert,delete,replace, etc.

4 Locking of MyISAM and InnoDB
MyISAM, note is a table lock, such as after more than one update operation, then select, you will find that the select operation is locked, must wait until all the update operation is complete, and then can selectInnoDB words are different, with a row of locks, there is no problem above.
5 MySQL Transaction configuration entries
Innodb_flush_log_at_trx_commit=1
Indicates that the transaction log is immediately written to disk when the transaction commits, and the data and index are also updated

Innodb_flush_log_at_trx_commit=0
The transaction log is not written to disk immediately when the transaction is committed, written every 1 seconds
innodb_flush_log_at_trx_commit=2
When a transaction commits, it is immediately written to the disk file (this is written to the kernel buffer, but not immediately to the disk, but is refreshed every 1 seconds to the drive, updating the data and indexExplain usage

EXPLAIN Tbl_name
Or:
EXPLAIN [EXTENDED] SELECT select_options

The former can draw a table of the field structure and so on, the latter is mainly to give some relevant index information, and today the focus is the latter.

Example
Mysql> Explain select * from event;
+--+ ————-+ ——-+--+ ————— +--+ ——— +--+--+ ——-+
| ID | Select_type | Table | Type | Possible_keys | Key | Key_len |ref | Rows | Extra |
+--+ ————-+ ——-+--+ ————— +--+ ——— +--+--+ ——-+
| 1 | Simple | Event | All | NULL | NULL | NULL | NULL | 13 | |
+--+ ————-+ ——-+--+ ————— +--+ ——— +--+--+ ——-+
1 row in Set (0.00 sec)

The meaning of each property
Id
Serial number of the select query

Select_type
The type of select query is mainly the difference between common queries and complex queries such as federated queries and subqueries.

Table
The table that is referenced by the output row.

Type
The type used by the union query.
Type shows the type of access, which is an important indicator, and the resulting values from good to bad are:
System > Const > Eq_ref> ref > Fulltext >ref_or_null > Index_merge >unique_subquery > Index_subque Ry >range > Index > All
in general, it is best to ensure that the query reaches at least the range level, preferably ref.

Possible_keys
Indicates which index MySQL can use to find rows in the table. If it is empty, there is no index associated with it. To improve performance, you can examine where clauses to see if some fields are referenced, or check that the fields are not appropriate for the index.

Key
Displays the keys that MySQL actually decides to use. If no index is selected, the key is null.

Key_len
Displays the key lengths that MySQL decides to use. If the key is null, the length is null. Documentation tips pay particular attention to this value to derive a multi-primary key in what part of MySQL is actually used.

Ref
Shows which field or constant is used together with the key.

Rows
This number indicates how much data MySQL will traverse to find and is inaccurate on InnoDB.

Extra
If it is only index, this means that information is retrieved only from the information in the index tree, which is faster than scanning the entire table.
If it is a where used, the where limit is used.
If it is impossible where means no where, it is generally not found out what.
If this information shows the using Filesort or using temporary, it will be very laborious, where and the index of the order is often not the case, if the index is determined by where, Will inevitably cause usingfilesort, it depends on whether the first filtering and sorting cost-effective, or first sort and then filter cost-effective.

Some common noun explanations
Using Filesort
MySQL requires an extra pass to find out how rows are retrieved in sorted order.

Using Index
Retrieves column information from a table by using only the information in the index tree without requiring a further search to read the actual rows.

Using Temporary
To resolve the query, MySQL needs to create a temporary table to accommodate the results.

Ref
For each row combination from the preceding table, all rows with matching index values are read from this table

All
With no index at all, the performance is very poor.

Index
Same as all except that only the index tree is scanned. This is usually faster than all, because the index file is usually smaller than the data file.

Simple
Simple select (Do not use union or subquery)

Explain MySQL performance optimization

Related Article

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.