我有一个包含约 60 万条记录的统计表,我在该表上执行以下(原始 sql)查询以获取图表的统计数据:
SELECT
(UNIX_TIMESTAMP(s.date)*1000+3600000) as time,
ROUND((s.loadtime / s.loadtimeMeasurements), 3) as loadtime
FROM mw_statistics s
WHERE s.type = 0
AND s.date >= '2013-02-01 07:52:06'
AND s.date <= '2013-02-01 11:52:06'
AND s.product_id IN (1,8,9,10,11)
GROUP BY s.date
此查询大约需要 1 秒才能完成。我希望它只需要几百毫秒。知道如何改进这个查询吗?我正在使用带有 mysql 数据库和 innodb 引擎的 Symfony2/Doctrine。
问候,贾斯珀
这是表的结构转储:
CREATE TABLE IF NOT EXISTS `mw_statistics` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`contentErrors` smallint(6) DEFAULT NULL,
`contentMeasurements` smallint(6) DEFAULT NULL,
`thirdpartyErrors` smallint(6) DEFAULT NULL,
`thirdpartyMeasurements` smallint(6) DEFAULT NULL,
`applicationErrors` smallint(6) DEFAULT NULL,
`applicationMeasurements` smallint(6) DEFAULT NULL,
`loadtime` double NOT NULL,
`loadtimeMeasurements` smallint(6) NOT NULL,
`unavailable` smallint(6) DEFAULT NULL,
`unavailableMeasurements` smallint(6) DEFAULT NULL,
`type` smallint(6) NOT NULL,
`step` smallint(6) DEFAULT NULL,
`date` datetime NOT NULL,
`status` smallint(6) DEFAULT NULL,
`url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`product_id` int(11) DEFAULT NULL,
`script_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_FC665E6F4584665A` (`product_id`),
KEY `IDX_FC665E6FA1C01850` (`script_id`),
KEY `date` (`date`)
) ENGINE=InnoDB DEFAULT
CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2105417 ;
请注意,组合是唯一的: (type=0, product_id, date) 或 (type=1, script_id, step, date)