使用 volkszaehler.org 我需要从一百万多行表中检索数据,以下是 ORM 创建的内容:
CREATE TABLE `data` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`channel_id` int(11) DEFAULT NULL,
`timestamp` bigint(20) NOT NULL,
`value` double NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ts_uniq` (`channel_id`,`timestamp`),
KEY `IDX_ADF3F36372F5A1AA` (`channel_id`)
)
现在,选择分组数据很慢,尤其是在 Raspberry Pi 等低性能平台上运行时:
SELECT MAX(timestamp) AS timestamp, SUM(value) AS value, COUNT(timestamp) AS count
FROM data WHERE channel_id = 4 AND timestamp >= 1356994800000 AND timestamp <= 1375009341000
GROUP BY YEAR(FROM_UNIXTIME(timestamp/1000)), DAYOFYEAR(FROM_UNIXTIME(timestamp/1000));
解释:
SIMPLE data ref ts_uniq,IDX_ADF3F36372F5A1AA ts_uniq 5 const 2066 Using where; Using temporary; Using filesort
查询需要经过 50k 条记录,在 Core i5 上需要 1.5 秒,在 RasPi 上已经需要 6 秒。
除了减少数据量之外,还有什么可以提高性能的吗?