简体   繁体   English

如何避免在 "!=" mysql 中使用 Filesort

[英]How to avoid Using Filesort in "!=" mysql

Please, help me!请帮我! How to optimize a query like:如何优化查询,如:

SELECT idu
FROM `user`
WHERE `username`!='manager'
  AND `username`!='user1@yahoo.com'
ORDER BY lastdate DESC

This is the explain:这是解释:

explain SELECT idu FROM `user` WHERE `username`!='manager' AND `username`!='ser1@yahoo.com' order by lastdate DESC;
+----+-------------+-------+------+----------------------------+------+---------+------+--------+-----------------------------+
| id | select_type | table | type | possible_keys              | key  | key_len | ref  | rows   | Extra                       |
+----+-------------+-------+------+----------------------------+------+---------+------+--------+-----------------------------+
|  1 | SIMPLE      | user  | ALL  | username,username-lastdate | NULL | NULL    | NULL | 208478 | Using where; Using filesort | 
+----+-------------+-------+------+----------------------------+------+---------+------+--------+-----------------------------+
1 row in set (0.00 sec)    

To avoid file sorting in a big database.避免在大型数据库中进行文件排序。

Since this query is just scanning all rows, you need an index on lastdate to avoid MySQL from having to order the results manually (using filesort, which isn't always to disk/temp table).由于此查询只是扫描所有行,因此您需要在lastdate上建立索引以避免 MySQL 必须手动对结果进行排序(使用文件排序,这并不总是到磁盘/临时表)。

For super read performance, add the following multi-column "covering" index:为了获得超级读取性能,添加以下多列“覆盖”索引:

user(lastdate, username, idu)

A "covering" index would allow MySQL to just scan the index instead of the actual table data. “覆盖”索引将允许 MySQL 只扫描索引而不是实际的表数据。

If using InnoDB and any of the above columns are your primary key, you don't need it in the index.如果使用 InnoDB 并且上述任何列是您的主键,则索引中不需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM