这是SQL:
SELECT
f_collect_time
FROM
t_ov_meterorigvalue_a98ea7cee0b1111e48e0800163e002d36 a98ea7cee0b1111e48e0800163e002d36
WHERE
f_collect_time < '2017-05-05 00:00:00'
AND f_meter_id = 320679
ORDER BY
f_collect_time DESC,
f_meter_id
LIMIT 1
我的查询时间是38S +
表的行是19093806
the index of my table is below:
the fields of my table is below
显示创建表结果:
CREATE TABLE `t_ov_meterorigvalue_a98ea7cee0b1111e48e0800163e002d36` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`calc_time` datetime DEFAULT NULL,
`f_coding` varchar(30) DEFAULT NULL,
`f_collect_time` datetime DEFAULT NULL,
`f_data` double DEFAULT NULL,
`f_data_collection_info_id` int(11) DEFAULT NULL,
`f_orig_value` varchar(50) DEFAULT NULL,
`f_orig_valueid` varchar(32) DEFAULT NULL,
`rate` double DEFAULT NULL,
`receive_time` datetime DEFAULT NULL,
`f_state` varchar(1) DEFAULT '0',
`f_build_id` int(11) DEFAULT NULL,
`f_meter_id` int(11) DEFAULT NULL,
`f_meter_change_id` int(11) DEFAULT NULL,
`f_meter_param_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `buildid_collecttime` (`f_build_id`,`f_collect_time`),
KEY `meterid_collecttime_asc` (`f_collect_time`,`f_meter_id`,`f_meter_param_id`),
KEY `meterid_collecttime_desc` (`f_collect_time`,`f_meter_id`,`f_meter_param_id`),
KEY `meterid_meterparamid` (`f_meter_id`,`f_meter_param_id`),
KEY `f_meter_id` (`f_collect_time`,`f_meter_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=19152255 DEFAULT CHARSET=utf8
请帮帮我,我怎样才能优化这个可怕的查询
答案 0 :(得分:0)
请提供SHOW CREATE TABLE
。如果没有,这是一个猜测。添加此索引:
INDEX(meter_id, collect_time) -- in this order
它会做3件事:
WHERE
)。请注意,=
部分必须是第一个。DESC
以使您拥有ORDER BY f_collect_time DESC, f_meter_id DESC
将使索引也处理ORDER BY
而无需进行排序。但实际上,为什么在meter_id
?? ORDER BY