쿼리를 최적화 할 수있는 방법은 'resulta'테이블에 약 500k 레코드가있는 약 2 초가 걸리지 만, touble가 sevaral 수십억으로 증가 할 것으로 예상합니다.filesort를 사용하는 MySQL의 평균 평균 사용량이 가장 느림
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
테이블 : MySQL의에서
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;
에 당신은 아마이 더욱 최적화 할 수있을 것입니다. http://dev.mysql.com/doc/refman/5.0/en/innodb-index-types.html 및 http://www.xaprb.com/blog/2006/07/04/how- to-exploit-mysql-index-optimizations/도움이 되었으면 좋겠다. –