我有一张这样的表:
以下是表格定义:
CREATE TABLE `report_all_hour_1` (
`date` date NOT NULL DEFAULT '0000-00-00',
`id_station` int(11) NOT NULL DEFAULT '0',
`time_begin` time NOT NULL DEFAULT '00:00:00',
`time_end` time DEFAULT NULL,
`area_type` tinyint(4) NOT NULL DEFAULT '1',
`id_coltype` int(11) NOT NULL DEFAULT '0',
`data_value` double DEFAULT NULL,
PRIMARY KEY (`date`,`id_station`,`time_begin`,`area_type`,`id_coltype`),
KEY `NewIndex1` (`date`,`time_begin`,`id_coltype`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
area_type
最多可包含10个不同的值
id_coltype
最多可以包含2000个不同的值。
表的大小约为1000万行。
我需要一个查询来获取具有2个时间范围的所有id_coltype
中的所有area_type
的值(因此我可以将此范围与之前的范围进行比较)。
查询:
SELECT
CONCAT_WS('-',
COALESCE(IFNULL(SUM(IF(`date` BETWEEN '2016-03-01' AND '2016-04-30' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(88),data_value,0)),0),''),
COALESCE(IFNULL(SUM(IF(`date` BETWEEN '2016-03-01' AND '2016-04-30' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(89),data_value,0)),0),''),
COALESCE(IFNULL(SUM(IF(`date` BETWEEN '2016-03-01' AND '2016-04-30' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(90),data_value,0)),0),''),
COALESCE(IFNULL(SUM(IF(`date` BETWEEN '2015-12-31' AND '2016-02-29' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(88),data_value,0)),0),''),
COALESCE(IFNULL(SUM(IF(`date` BETWEEN '2015-12-31' AND '2016-02-29' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(89),data_value,0)),0),''),
COALESCE(IFNULL(SUM(IF(`date` BETWEEN '2015-12-31' AND '2016-02-29' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(90),data_value,0)),0),'')
) AS '86',
id_station,
area_type,
IFNULL(id_station,'sum_result') AS 'key'
FROM report_all_hour_1
WHERE id_station IN(493,494,495,496,517,960,961,962,963,964,965,518,1054,1499)
AND `date` BETWEEN '2015-12-31'
AND '2016-04-30'
AND time_begin >= '09:00:00'
AND time_begin < '22:00:00'
GROUP BY area_type, id_station WITH ROLLUP
查询2:
SELECT CONCAT_WS('-',COALESCE(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102),data_value, 0)),''), COALESCE(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102),data_value, 0)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,`date`,NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,WEEK(`date`),NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,MONTH(`date`),NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,`date`,NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,WEEK(`date`),NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,MONTH(`date`),NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,`id_station`,NULL)),''), COALESCE(COUNT(DISTINCT IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(102) AND data_value > 0,`id_station`,NULL)),'') ) AS '159',
CONCAT(IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(928),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(260),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(376),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(492),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(618),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(734),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(259),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(375),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(491),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(617),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(733),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(928),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(260),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(376),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(492),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(618),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(734),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(259),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(375),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(491),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(617),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(733),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),'-', IFNULL(SUM(IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0) ) AS '112',
CONCAT_WS('-', COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value, 0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(34),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(35),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(36),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(37),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(38),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(39),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(40),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(41),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(42),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(43),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(46),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(44),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(47),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(48),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(45),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(49),data_value, 0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(169),data_value, 0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(169),data_value, 0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(262),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(378),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(494),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(620),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(736),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(262),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(378),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(494),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(620),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(736),data_value,0)),0),'') ) AS '97',
CONCAT_WS('-', COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(17),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(18),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(19),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(20),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(21),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(22),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(23),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(24),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(25),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(26),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(27),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(28),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(29),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(30),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(31),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(32),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value, 0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(17),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(18),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(19),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(20),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(21),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(22),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(23),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(24),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(25),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(26),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(27),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(28),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(29),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(30),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(31),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(32),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value, 0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),''), COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(33),data_value,0)),0),'') ) AS '93',
CONCAT( COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00',(IF(id_coltype=63,data_value*0,0)+ IF(id_coltype=64,data_value*1,0)+IF(id_coltype=65,data_value*2,0)+IF(id_coltype=66,data_value*3,0)+IF(id_coltype=67,data_value*4,0)+IF(id_coltype=68,data_value*5,0)+IF(id_coltype=69,data_value*6,0)+IF(id_coltype=70,data_value*7,0)+IF(id_coltype=71,data_value*8,0)+IF(id_coltype=72,data_value*9,0)),0)),0),'') ,'-', COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2015-02-04' AND '2017-02-25' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(63,64,65,66,67,68,69,70,71,72),data_value,0)),0),'') ,'-', COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00',(IF(id_coltype=63,data_value*0,0)+ IF(id_coltype=64,data_value*1,0)+IF(id_coltype=65,data_value*2,0)+IF(id_coltype=66,data_value*3,0)+IF(id_coltype=67,data_value*4,0)+IF(id_coltype=68,data_value*5,0)+IF(id_coltype=69,data_value*6,0)+IF(id_coltype=70,data_value*7,0)+IF(id_coltype=71,data_value*8,0)+IF(id_coltype=72,data_value*9,0)),0)),0),'') ,'-', COALESCE(IFNULL(SUM( IF(`date` BETWEEN '2013-01-12' AND '2015-02-03' AND time_begin >= '09:00:00' AND time_begin < '22:00:00' AND id_coltype IN(63,64,65,66,67,68,69,70,71,72),data_value,0)),0),'') ) AS '7'
FROM report_all_hour_1
WHERE id_station IN(493,494,495,496,517,960,961,962,963,964,965,518,1054,1499)
AND `date` BETWEEN '2013-01-12'
AND '2017-02-25'
AND time_begin >= '09:00:00'
AND time_begin < '22:00:00'
GROUP BY area_type, id_station WITH ROLLUP
问题是性能缓慢,需要4分钟才能得到我需要的东西。反正有没有改进这个查询?
答案 0 :(得分:1)
WHERE
中基本上有3个过滤器。哪个更有选择性?选择性如何?也就是说,每个表中选择的表的百分比是多少:
id_station IN (...)
date BETWEEN ...
time_begin ...
目前,您只使用date
进行过滤,因为这是两个索引的开头。
如果其他两个字段(id_station
或time_begin
)中的任何一个具有足够的选择性,请在该列上添加索引:
INDEX(id_station, date)
INDEX(time_begin)
(向其中添加更多列是没有用的。)
如果将常量从运行更改为运行,则添加这两个索引;任何一个人可能会在某些时候工作。
您是否总是选择一个完全按月界限的日期范围? (显然不是,因为你从12-31开始?)如果你这样做,那么我们可以谈论汇总表。