Mysql's explain the Extra field explain

Read a little explain the Extra field, the first two (order select * from t where a =? And b>? By c limit 0,100) answers questions about how to add a sql index finally surfaced

Using index

Covering indication index, the performance will be much faster
coverage index refers to information sufficient to meet the index query request, no need to go back to fetch the primary key index.

Using index condition

Using index difference between that, and use the index (using the index to complete the screening filter fields), but the index column can not contain all the required fields queries, need to return to the table to complement the field
back to the table means that, according to the index found to meet the conditions of after id, using the primary key index id back to remove the entire row or taken required fields

Using filesort

Express is the need for sorting, MySQL will give each thread allocates a block of memory for sorting, called sort_buffer.
"Sort" this action may be completed in memory, you may need to use an external sort, depending on the desired memory and sorting parameters sort_buffer_size.
sort_buffer_size, it is the size of MySQL as a sort of open memory (sort_buffer) of. If the amount of data to be sorted is less than sort_buffer_size, sorting is completed in memory. But if ordering too much data, the memory does not fit, you have to use temporary disk files a secondary sort.

  • Full field sort
    can view the results by OPTIMIZER_TRACE
    "sort_mode": "<fixed_sort_key, additional_fields>"

  • Sort rowid
    If the number of fields sort_buffer stored in too much will cause memory at the same time the number of rows can put down a little, it will use temporary files to be sorted, then sorting efficiency will be poor
    control sequencing of single line length settings ( Referring to table for t, max_length_for_sort_data value equals 22, are used to sort the whole field) specific syntax SET max_length_for_sort_data = 21;
    can view the results OPTIMIZER_TRACE
    "sort_mode": "<fixed_sort_key, rowid>"

Using where

The first thing you need to know is Mysql database contains sever layer (connectors, query cache, analyzer, optimizer, executor) and the engine layer (InnoDB, MyISAM, Memory)
the Using the WHERE indicates the row sever layer in the layer return receipt engine after the filtration (i.e., filtration application WHERE condition). That will filter the result set the query conditions.

in conclusion

Above 3's performance overhead is sequentially amplified, that is to say as far as possible the situation were occurring in Extra

Using index > Using index condition > Using filesort

There is no sort of plus Using where, because at this stage if there will be a condition where there is sql words

With After this theory, let's answer the conclusion of the previous two articles (the same test sample before the two articles, the first to be resolved here only one conclusion)

idx_cab (c,a,b) / Using where; Using index

The best, not back to the table, without sorting

idx_ac (a,c) / Using where

Need to return to the table, do not need to sort (completion b> back to the filter condition table 5000)

idx_abc (a,b,c) / Using where; Using index; Using filesort

Do not need back to the table, need to sort

idx_ab (a,b) / Using index condition; Using filesort

Need to return to the table, to be sorted (completion b> before returning the filter condition table 5000)

idx_a (a) / Using where; Using filesort

Need to return to the table, to be sorted (completion b> back to the filter condition table 5000)

PRIMARY KEY (id) / Using where; Using filesort

Full table scan, and the need to sort

Conclusion Update (2019-09-14)

Before proceeding to test the conclusion front idx_cab (c, a, b) optimal, idx_ac (a, c) followed.
In fact, there is a test not done, idx_acb (A, c, b), in this way is truly the best.
Analysis:
SQL (SELECT * WHERE from T = a and B> C Order limit by 0,100??) On the subject a
comparison to equivalent, so idx_acb (a, c, b) , will be placed in a first one, in after the conditions are satisfied a, c is still orderly, not specifically to sort, then just turn were taken out value b (b>?) compared to the condition of the direct return, when the number reached 100 bar return At the end of traversal.

Reference Knowledge

The aforementioned "full-field sort" "rowid sort" in the end using a sql which can be viewed by way of the following ways:

/* 打开 optimizer_trace,只对本线程有效 */
SET optimizer_trace='enabled=on'; 

/* @a 保存 Innodb_rows_read 的初始值 */
select VARIABLE_VALUE into @a from  performance_schema.session_status where variable_name = 'Innodb_rows_read';

/* 执行语句 */
select * from t where a=10 and b>50000 order by c limit 0,100 ;

/* 查看 OPTIMIZER_TRACE 输出 */
SELECT * FROM `information_schema`.`OPTIMIZER_TRACE`\G

/* @b 保存 Innodb_rows_read 的当前值 */
select VARIABLE_VALUE into @b from performance_schema.session_status where variable_name = 'Innodb_rows_read';

/* 计算 Innodb_rows_read 差值 */
select @b-@a;
Published 100 original articles · won praise 64 · Views 250,000 +

Guess you like

Origin blog.csdn.net/hl_java/article/details/100054998