我有一个名为'活动'与50M +行表。如何优化此性能不佳的Mysql查询?
CREATE TABLE `activities` (
`activity_id` bigint(20) NOT NULL AUTO_INCREMENT,
`id_of_contract` bigint(20) DEFAULT NULL,
`duration_s` int(11) DEFAULT NULL,
`timestamp_end` bigint(20) DEFAULT NULL,
`timestamp_start` bigint(20) DEFAULT NULL,
`id_of_room` bigint(20) DEFAULT NULL,
PRIMARY KEY (`activity_id`),
KEY `analyse` (`id_of_room`,`timestamp_end`,`timestamp_start`,`duration_s`),
ENGINE=InnoDB DEFAULT CHARSET=utf8;
我有这样的要求:
select *
from activities
where id_of_room=3263
and timestamp_end>1471491882747
and timestamp_start<1479267882747
and duration_s>900
order by duration_s desc;
的解释回报这样的:
id select_type table partitions type possible_keys key key_len ref rows filtered Extra
1 SIMPLE activities NULL range analyse analyse 18 NULL 1 5.00 Using index condition; Using filesort
在1.5秒的查询返回。我怎样才能优化这个?
感谢您的帮助。
'ORDER BY'需要很长的时间...为什么你需要订购? – Eugene
这会返回多少条记录? – e4c5
@ e4c5它返回1k行 –