Mysql indexing mechanism (reproduced) http://blog.csdn.net/wenniuwuren

MySQL Tuning - Using filesort
Tags: Index MySQL Performance Tuning Slow Query Using filesort
2015-12-22 15:16 897 people read Comments (0) Collection Report
Category  : MySQL (13)
Copyright Notice: This article is an original article by a blogger , may not be reproduced without the permission of the blogger. For forwarding, please indicate from http://blog.csdn.net/wenniuwuren
The reason for this problem is that MySQL can only use one index per query, and the WHERE condition of your SQL statement is different from the ORDER BY condition, and the index is not built If it is good, then ORDER BY will not use the index, and there will be the problem of Using filesort.

The solution to this problem is to create a mixed index that contains WHERE and ORDER BY conditions.

For example, the original SQL statement is:
[sql] view plain copy print? View code slice on CODE derived to my code slice
SELECT * FROM user u where u.id=100 order by u.update_time 

and the index is idx_user_id(id)

now Re-index as idx_user_id_update_time(id, update_time)
Then use the EXPLAIN command to check. If the key uses the newly created idx_user_id_update_time index, you can see that the problem of Using file sort disappears. If the key does not use the newly created idx_user_id_update_time index, you can use the force index() method to force the use of this index. using filesort problem is solved.
[sql] view plain copy print? View code slice on CODE derived to my code slice
SELECT * FROM user u force index(idx_user_id_update_time) where u.id=100 order by u.update_time 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326696657&siteId=291194637