Home  >  Article  >  Database  >  MySQL调优--Usingfilesort_MySQL

MySQL调优--Usingfilesort_MySQL

WBOY
WBOYOriginal
2016-05-27 13:46:241019browse

出现这个问题的原因在于 MySQL 每次查询只能使用一个索引, 而你的 SQL 语句 WHERE 条件和 ORDER BY 的条件不一样, 索引没建好的话, 那么 ORDER BY 就使用不到索引, 出现了 Using filesort 问题。
解决这个问题就是建立一个包含 WHERE 和 ORDER BY 条件的混合索引。

比如原来 SQL 语句是:

SELECT * FROM user u where u.id=100 order by u.update_time

而索引是 idx_user_id(id)
现在重新建立索引为 idx_user_id_update_time(id,update_time) 再使用 EXPLAIN 命令查看, 如果 key 使用的是上述新建的 idx_user_id_update_time 索引, 则可以看到 Using file sort 问题消失了, 如果 key 不是使用新建 idx_user_id_update_time 索引, 可以使用 force index() 方法强制使用这个索引, 此时 using filesort 问题就解决了。
SELECT * FROM user u force index(idx_user_id_update_time) where u.id=100 order by u.update_time


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn