简体   繁体   English

如何通过使用临时文件排序来避免顺序

[英]How to avoid Order by using temporary,filesort

What index do i need to create to avoid order by using temporary, filesort 我需要创建什么索引来避免通过使用临时文件排序来进行排序

  EXPLAIN SELECT   tid, sum(count)    
              FROM test  
                 WHERE cid = 1
                 GROUP BY tid
                 ORDER BY sum(count) DESC

   1    SIMPLE  test    ref     PRIMARY,id_UNIQUE,cid   cid     4   const   2   Using where; Using index; Using temporary; Using filesort

Create table : 创建表:

   CREATE TABLE test(
           cid INT,
           tid INT,
            datedm INT,
           count INT,
            PRIMARY KEY(cid,tid,datedm),
           INDEX(cid,tid,count),
           UNIQUE INDEX id_UNIQUE(cid,tid,datedm)
          );

In this case you can't: the ORDER BY is on an aggregated value which can't be indexed as such. 在这种情况下,您不能:ORDER BY位于无法按此索引的聚合值上。

Note your last index is identical to the primary key. 请注意,您的最后一个索引与主键相同。 However, if you reverse it to tid, cid it may help because GROUP BY implies an ORDER BY too. 但是,如果将其反转为tid, cid可能会有所帮助,因为GROUP BY也暗含了ORDER BY。 But, you may then have issues with the WHERE clause because of how MySQL works 但是,由于MySQL的工作方式,您可能随后在WHERE子句中遇到问题

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

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