繁体   English   中英

MySQL使用文件排序平均慢速最小最大

[英]MySQL slow avg min max using filesort

我如何优化查询,它已经花费了大约2秒钟的时间,并且在“结果”表中有大约50万条记录,但是我希望这个麻烦会增长到数十亿。

 SELECT
     hopcount, hop, round( avg( rtt ) , 2 ) AS avg, min( rtt ) AS min, max( rtt ) AS        max
FROM results
JOIN traces ON id = trace
WHERE target =9
AND rtt > -1
GROUP BY hop`  

解释输出:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra   
1   SIMPLE  traces  ref PRIMARY,fk_traces_1_idx fk_traces_1_idx 5   const   26333   Using where; Using temporary; Using filesort  
1   SIMPLE  results ref trace   trace   5   pinger.traces.id    7   Using where

表格:

CREATE TABLE IF NOT EXISTS `results` (
  `hop` int(11) DEFAULT NULL,
  `trace` int(11) DEFAULT NULL,
  `rtt` int(11) NOT NULL,
  `seq` int(11) NOT NULL,
  KEY `trace` (`trace`),
  KEY `fk_hops_id` (`hop`),
  KEY `seq` (`seq`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;  
    CREATE TABLE IF NOT EXISTS `traces` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `hopcount` smallint(6) NOT NULL,
  `target` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_traces_1_idx` (`target`,`id`,`time`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=52308;

在MySQL中, GROUP BY表示ORDER BY

要删除此隐式排序,请添加ORDER BY NULL

SELECTMySQL文档中提到了这一点

如果使用GROUP BY,则输出行将根据GROUP BY列进行排序,就好像您对同一列使用ORDER BY一样。 为避免GROUP BY产生的排序开销,请添加ORDER BY NULL:

而且在ORDER BY优化中

暂无
暂无

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

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