Mysql optimization plan view of execution

 

We often say mysql optimization, optimization in a common way is to create an index field frequently queried. So what type of index mysql has it?

First, the classification index
1, the general index: the index contains only a single column, a table can have multiple separate index

2, the only index: the index column value must be unique, but allow free value

3, a composite index: the index comprises a plurality of columns

4, clustered index (clustered index): index is not alone type, but a data storage. Depending on implementation details, InnoDB clustered index is actually stored in the same B-Tree index in a structure (technically a B + Tree) and data lines.

5, non-clustered index: It is not a clustered index is non-clustered index

 

Second, the basic grammar

View index
SHOW INDEX FROM table_name

创建索引
CREATE [UNIQUE ] INDEX indexName ON mytable(columnname(length));
ALTER TABLE 表名 ADD [UNIQUE ] INDEX [indexName] ON (columnname(length))

Delete index
DROP INDEX [indexName] ON mytable;

Mentioned above the basic syntax for creating an index, now deal with talk about how to view the execution plan.


Implementation plan : EXPLAIN keyword can be simulated using the optimizer to perform SQL queries, so they know how to handle your MySQL is a SQL statement. Analysis of performance bottlenecks in your query or table structure

Action execution plan : 1, the reading order table 2, a data read operation of the operation type / 3, which indexes can be used, which is actually used indexes     4, 5 references between tables, each table row number the query optimizer

Information contained in the implementation plan are:

 

Implementation plan -ID

Select query sequence number, contains a set of numbers indicating the order of clauses or select a query execution operation table

There are three cases:
(1), the same id, execution order from top to bottom
(2), different id, if a subquery, id is incremented sequence number, id value is larger the higher the priority, the first is performed
(3 ), the same id different, exist

Complex query types of queries, mainly used to distinguish between ordinary query, the union query, sub query: implementation plan -select_type

 

 


Implementation Plan The -type
of the type shown is the type of access is a more important indicator, the resulting value from best to worst are:

system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL

Common is: system> const> eq_ref> ref> range> index> ALL

system: the table has only one row (equal to the system table), which is a special type const column, usually does not occur, this may be negligible

const: represented by the index once found, const for comparing the primary key or unique index. Because only one row of data match, and soon as will be placed where the primary key list, MySQL the query can be converted to a constant

eq_ref: unique index scan, for each index key, only one record in the table match. Common in the primary key or unique index scan

range: only retrieves a row of a given range, using an index to select the rows. column shows which key to use the index in general is present in your statement where the between, <,>, in other queries
              that range scan index scan is better than the full table scan, because it only needs to start at some point in the index while the Conclusion another point, do not scan the entire index.

all: Full Table Scan, will traverse the whole table to find matching rows

Implementation plan -possible_keys : index actually used. If NULL, then the index is not used, if the query using the cover index, the index query and select fields overlap

Program execution -key_len : number of bytes used in the index, said length of the index may be used in the query is calculated by the column. Without loss of accuracy, a length as short as possible
                              key_len maximum possible length values of the index fields displayed are not actual length, i.e. calculated on the basis key_len table definition is not retrieved by the inner

                             According to this value, you can determine the index usage, particularly when the composite index, to determine whether or not all the index fields are used in the query.
                              char and varchar with character encoding is also closely linked,
                              latin1 takes 1 byte, gbk 2 bytes, utf8 occupying 3 bytes. (Different character encodings take up storage space)

 

 

 

 

 

 

 

 

Character types: char + index field is not null:

 

Character types: char + index field is allowed to be null:

 

Index field is of type varchar + Not Null:

 


Index field is Null + type varchar Allow to:

 

 

 

 

 

Integer / floating point / time type index length  : NOT NULL = field length field itself, NULL = +1 field length field itself (because of the need for marking is empty, this tag needs to occupy one byte)
                                                       datetime type in the field length is 5 bytes 5.6, datetime 5.5 type field length is 8 bytes

key_len Summary:
variable length fields require an extra 2 bytes (the number of characters required to save only when VARCHAR values stored, plus a record length byte (column declared if longer than 255, the two bytes), Therefore VARCAHR time to calculate index length plus 2), fixed-length fields no additional bytes.
The NULL require additional space a byte, it is best not to index field is NULL, NULL because statistics make more complex and requires additional storage space.
There leftmost composite index characteristic prefix, if the composite index can all be used, the length of the composite index is the index field, and this can also be a composite index portion for determining whether to use or use all.

 

Implementation plan -ref : Which column index display is used, if possible, it is a constant. Which columns or constants are used to find the value of the index column

Implementation plan -rows : According to the statistics table and choose the index case, a rough estimate of the number of lines needed to find the record to be read

Implementation plan -Extra : unsuitable for display but additional information is very important in the other columns

 

 

Using filesort mysql will be described using an external data ordering index, instead of reading the index order in the table. MySQL can not use indexes to complete the sorting operation is called "File Sort"
the Using the Temporary: save intermediate results to make a temporary table, MySQL use temporary tables when sorting query results. Common in order by sorting and grouping queries group by

USING index: expressed using the corresponding select operation covering index (Covering Index), avoid accessing the data row of the table, good efficiency, using where if there is at the same time, it shows that the index is used to perform a lookup index key values,
if not at the same time appear using where, it shows that the index is used to read data rather than perform a lookup operation

 

 

 

    

 

Guess you like

Origin www.cnblogs.com/cheng21553516/p/11403378.html