Turing College Road -VIP java architecture of the (three) Mysql use of Explain Detailed

  Part blog, we have a detailed description of the index mysql storage structure, that is, our B + tree variants, is a doubly linked list with a B + tree. So today I look at in detail, how to use and how to view the usage index index.

Let's take a simple set up several tables.

  

Only ID plus an index, let's run explain, look at the results.

Which contains the id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra columns, let's go over how these are what use it.

id:

id number of the column is select the serial number, there are a few select few id, id and order is the order of appearance of select growth.
id column the greater the higher execution priority, id from executing the same downward, id finally performed to NULL.

 例如:EXPLAIN SELECT r.id FROM (SELECT id  from student)  r

We can see from the sql statement, we have two select, that is, there are two id, then, we will give priority to run red color select, which is id 2, and then run the id to select 1.

select_type列:
select_type indicates that the corresponding line is a simple or complex queries; can be divided into the following categories:
  1) simple: simple query. Query does not contain sub-queries and union
  2) primary: a complex query in the outermost select
  3) subquery: select included in the sub-queries (not in the from clause)
  4) derived: included in the from clause subqueries. MySQL will store the result in a temporary table, also called a derived table (the English meaning derived)
  5) union: select the second and subsequent union of the
table column:
This column shows which table to explain the line being accessed.
When there is a subquery in the from clause, table columns are <derivenN> format, showing the current query dependent query id = N, so the first query execution id = N.
When a union, the value table column UNION RESULT <union1,2>, 1 and 2 represent the row select id involved in the union.
type columns:
This column shows the type of association or the type of access that MySQL decides how to find rows in the table, find the approximate range of rows of data.
In order from best to worst are: system> const> eq_ref> ref > range> index> ALL in general, have to ensure that the query reaches the level range, preferably up to ref
Here briefly explain again.
Which system, const is the main quality. Using the primary key index is generally const, system generally do not need to look-up table sql can be obtained again.
Eq_ref is all part of the primary key or unique key index to be connected using, it will only return a qualifying record. This is probably the best connection type, simple select query does not appear in this type outside const.
ref compared eq_ref, without the use of a unique index, but use some common prefix index or unique index, the index and a value to be compared may find more qualifying rows.
Find a range usually in the range of use of the index, index: full table scan index, which is usually faster than ALL few. ALL: namely, a full table scan, which means mysql need to find the line from start to finish required. Often this requires adding indexes to optimize.
possible_keys column:
This column shows the query which may use the index to find.
There are columns when possible possible_keys explain, while the case key display NULL, this situation is not much because the data in the table, mysql think this query index of little help, choose a full table query. If the column is NULL, no relevant index. In this case, you can see if you can create an appropriate index to improve query performance, and then view the results by checking with explain the where clause.
The key column:
This column shows which index mysql actually used to optimize access to the table.
If you do not use the index, the column is NULL. If you want to force mysql to use or ignore possible_keys column index, the use of force index in the query, ignore index.
key_len columns:
This column shows the number of bytes in the index mysql used, this value can be calculated by the specific use in which the column index.
Calculated and set your column type has a lot
char (n): n byte length
varchar (n): 2-byte string length storage, if it is utf-8, the length 3n +2
tinyint: 1 byte
smallint: 2 bytes
int: 4 bytes
date: 3 bytes
timestamp: 4 bytes
datetime: 8 bytes
 
The maximum length of index is 768 bytes, when the string is too long, mysql will do processing a prefix index similar to the left, the first half of the character is extracted indexed.

The ref column:

This column shows the index key columns of records in a table lookup column values ​​or constants used in common are: const
rows columns:
This column is to be read and mysql estimate the number of rows of detection, note that this is not the number of rows in the result set. Just an estimate.
Extra column:
This column shows the extra information.
Briefly about what has probably common
Using index: Use a covering index
Using where: using the where clause to process the results, the query column is not covered by the index
Using index condition: column of the query is not completely covered by the index, where a range of conditions leading column;
Using temporary: mysql need to create a temporary table to process the query. This happens generally is to be optimized, first thought of using the index to optimize.
There are many, many, you can go on their own official document viewing.
 
Some of these concepts are not recommended to recite down and began to look up documents can blog, with more naturally remember.
 
  Here I look at the most left-prefix principle, we mentioned a moment on a blog leftmost prefix of principle, but we did not elaborate.
  I look, suppose we now have a list student, there id (primary key index), name, Age, address, class (joint index), CreateTime
If the index used in combination as the bridge, not breaking in the middle continuing to use the bridge as a springboard assumed name A, address springboard B, class C. springboard In other words, we use ABC, A must give priority to the use of re-using the B final is C, and every rational use.
For example select * from student where name = "Joe Smith" and class = "three shifts" At this time we only use a name index, and the rest were not used, we can try to explain himself above test, the count value ken_len.
Us a few special cases, select * from student where name = "Joe Smith" age> 18 and address = "Beijing" and class = "three shifts", in this case, in fact, the two are not used to the index, you can look up your own brain index of B + tree on her own look.
Questions remain a why we have built the table when the need to set the field is not empty.
  Index Tuning long way to go, I will detail later said mysql index optimization.

Guess you like

Origin www.cnblogs.com/cxiaocai/p/11374729.html