MySQL implementation plan (Using where, Using index and Using index condition)

Extra fields on the implementation of the plan, some of these values ​​have some doubts, I talk about my general understanding.

  1. Using where: represents the optimizer needs to query data through the index back to the table;

  2. Using index: the index represents a direct access would be sufficient to obtain the required data is not required by the index back to the table;

  3. Using index condition: new features (Index Condition Pushdown) after the 5.6 version added;
     the Using index for condition Condition will first filter condition index, the index After filtration find all the data rows that meet the index criteria, then go with the other conditions in the WHERE clause filtering the data lines;

  4. Using where && Using index: this does not understand the difference between it and the Using index condition.

Then I did some testing in the sample database Sakila MySQL, but the results made me feel very confused:

Testing using Sakila.rental table, table customer_id established called idx_fk_customer_id index.

 
  1. -- 第一个 SQL 语句

  2. EXPLAIN SELECT customer_id FROM rental WHERE customer_id>=300;

The result is the use of idx_fk_customer_id index, but turned out to Extra information Using where; Using index; in this SQL statement, SELECT clause and the WHERE clause is not filtered and can be obtained from the index it, why Extra value is not Using index it?

 
  1. -- 第二个 SQL 语句

  2. EXPLAIN SELECT * FROM rental WHERE customer_id>=373;

This time even more surprising because the SELECT statement includes a data index does not exist, it is necessary to query data through the index back to the table, so as Extra Using where I can understand, but here even went so far as to type ALL, also said that the implementation of the plan use a full table scan! This is why?

 
  1. -- 第三个 SQL 语句

  2. EXPLAIN SELECT customer_id FROM rental WHERE customer_id>=373 AND customer_id<400;

This statement is the same as the Extra Value Using where; Using index

 
  1. -- 第四个 SQL 语句

  2. EXPLAIN SELECT * FROM rental WHERE customer_id>=373 AND customer_id<400;

After this statement the execution plan is better understood, the first use cusomter_id> 373 or customer_id a condition of <400 filtered indexes, filtration finished index, and filtered through the index back to the table scan again off part of the information, and then returns the final result, Extra for the Using index condition.

Great God also hope you will not hesitate to answer, or you can pointing three issues I care most about: the difference between 1.Using where && Using index and Using index condition of;

2. Why EXPLAIN SELECT customer_id FROM rental WHERE customer_id> = 300; will use the index, while EXPLAIN SELECT * FROM rental WHERE customer_id> = 300; does not use the index it?

EXPLAIN SELECT * FROM rental WHERE customer_id> = 300 AND customer_id <= 350; will use the index
EXPLAIN SELECT * FROM rental WHERE customer_id> = 300 AND customer_id <= 476; does not use the index
index values range RANGE requirements have it?
customer_id is SMALLINT (5) of the type

  • I understand Using index condition is, er, first of all mysql server and storage engine is a two component, server responsible parse sql, implementation; storage engine to really make data / index read / write used to be like this: server command storage engine according to the corresponding index data read from the data table, passed server, according to the server where the conditions to choose; ICP is now possible in the case, so that storage engine to make a judgment according to the index, if the condition does not meet the there is no need to read the data sheet. this saves IO Disk. 
    https://dev.mysql.com/doc/refman/5.6/en/index-condition-pushdown-optimization.html

You are using a key with low cardinality (many rows match the key
value) through another column. In this case, MySQL assumes that by
using the key it probably will do many key lookups and that a table
scan would be faster.

  • The following investigation  customer_id>=300 AND customer_id<=350; and  customer_id>=300 AND customer_id<=476; the respective numbers of data. I think the first data may be less.

3Q, it is almost clear, only Using where; Using index Using index condition and there is some doubt, wait for me to look up the manual. .

1.Using index condition and Using where; Using index difference of
http://stackoverflow.com/questions/28759576/mysql-using-index-condition-vs-using-where-using-index

Using index and Using where; Using index of difference;
http://stackoverflow.com/questions/25672552/whats-the-difference-between-using-index-and-using-where-using-index-in-the

2. It was almost tell you, but not because where is> =;

Optimizer in a case where index exists, by selecting the ratio of the total number and in line with the number of range RANGE use an index or table walk full

For example, in the rental table, the number of rows in the table is 16,044 rows;

 
  1. -- 不使用索引

  2. EXPLAIN SELECT * FROM rental WHERE customer_id>492;

  3. -- 使用索引

  4. EXPLAIN SELECT * FROM rental WHERE customer_id>493;

  5. -- 其中 id > 492 的行数为 2772, id > 493 的行数为 2749

 
  1. -- 不使用索引

  2. EXPLAIN SELECT * FROM rental WHERE customer_id<103;

  3. -- 使用索引

  4. EXPLAIN SELECT * FROM rental WHERE customer_id<102;

  5. -- 其中 id < 103 的行数为 2767, id < 102 的行数为 2734

 
  1. -- 不使用索引,count(*) 为 2758 条

  2. EXPLAIN SELECT * FROM rental WHERE customer_id>100 AND customer_id < 202;

  3. -- 使用索引, count(*) 为 2733 条

  4. EXPLAIN SELECT * FROM rental WHERE customer_id>100 AND customer_id < 201;

Conclusion: When the data to be read exceeds a critical value, the optimizer will give up reading from the index and replaced by a full table scan, which is to avoid too many random disk.

Published 447 original articles · won praise 71 · Views 400,000 +

Guess you like

Origin blog.csdn.net/w892824196/article/details/104031878