FAQ: MySQL / Sort

MySQL sorted into two, and sorted by the sorting index scan operation.

Scan order of the index is a very efficient way, but more stringent conditions of use.

  1. If the index does not cover all of the columns required for the query, then you can not sort by using an index scan.
  2. Only when the index column order and order exactly the same order by clause, and sort all of the columns are the same direction when, mysql to use the index to sort the results. (Unless the forefront as a constant, but this operation is an index into force, different for each database is not recommended)
  3. When the association table query, only when the field order by clause reference in its entirety for the first table, the index will be used to do the sort.

For a constant forefront, the second example of the effective column index

-- 第一列提供常量条件,使用第二列进行排序,这两列组合在一起,形成索引最左前缀,所以可以索引排序,没有出现filesort
EXPLAIN SELECT rental_id, staff_id FROM rental
WHERE rental_date = '2005-05-25'
ORDER BY inventory_id DESC;

Guess you like

Origin www.cnblogs.com/cielosun/p/11502990.html