我做了一个查询,它给了我需要的结果,但速度非常慢。
问题:非常慢。
问题:我有什么方法可以加快查询速度!!!
SELECT ci.ctt_region_id,
ci.region_name,
cs.slot_set_date,
cs.ctt_photo_slot_id,
/* Mon*/
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-12'
AND slot_set_time = '11:00:00'
) as mon_eleven_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-12'
AND slot_set_time = '14:00:00'
) as mon_two_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-12'
AND slot_set_time = '17:00:00'
) as mon_five_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-12'
AND slot_set_time = '11:00:00'
AND ctt_region_id = ci.ctt_region_id
) as mon_eleven_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-12'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as mon_two_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-12'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as mon_five_timecount,
/* Tue*/
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-13'
AND slot_set_time = '11:00:00'
) as tue_eleven_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-13'
AND slot_set_time = '14:00:00'
) as tue_two_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-13'
AND slot_set_time = '17:00:00'
) as tue_five_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-13'
AND slot_set_time = '11:00:00'
AND ctt_region_id = ci.ctt_region_id
) as tue_eleven_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-13'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as tue_two_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-13'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as tue_five_timecount,
/* Wed*/
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-14'
AND slot_set_time = '11:00:00'
) as wen_eleven_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-14'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as wen_two_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-14'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as wen_five_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-14'
AND slot_set_time = '11:00:00'
AND ctt_region_id = ci.ctt_region_id
) as wen_eleven_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-14'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as wen_two_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-14'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as wen_five_timecount,
/* Thu*/
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-15'
AND slot_set_time = '11:00:00'
) as thu_eleven_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-15'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as thu_two_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-15'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as thu_five_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-15'
AND slot_set_time = '11:00:00'
AND ctt_region_id = ci.ctt_region_id
) as thu_eleven_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-15'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as thu_two_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-15'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as thu_five_timecount,
/* Fri*/
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-16'
AND slot_set_time = '11:00:00'
) as fri_eleven_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-16'
AND slot_set_time = '14:00:00'
AND ctt_region_id = cs.ctt_region_id
) as fri_two_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_set_time SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-16'
AND slot_set_time = '17:00:00'
) as fri_five_time,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-16'
AND slot_set_time = '11:00:00'
AND ctt_region_id = ci.ctt_region_id
) as fri_eleven_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-16'
AND slot_set_time = '14:00:00'
AND ctt_region_id = ci.ctt_region_id
) as fri_two_timecount,
(SELECT GROUP_CONCAT(DISTINCT AA.slot_count SEPARATOR ',')
FROM ctt_photo_slot AA
WHERE slot_set_date = '2015-01-16'
AND slot_set_time = '17:00:00'
AND ctt_region_id = ci.ctt_region_id
) as fri_five_timecount
FROM ctt_photo_slot cs
JOIN ctt_region ci
ON cs.ctt_region_id = ci.ctt_region_id
GROUP BY region_name;
答案 0 :(得分:1)
使用条件聚合:
SELECT ci.ctt_region_id, ci.region_name, cs.slot_set_date, cs.ctt_photo_slot_id,
/* Mon*/
GROUP_CONCAT(DISTINCT CASE WHEN slot_set_date = '2015-01-12' AND slot_set_time = '11:00:00'
THEN AA.slot_set_time END SEPARATOR ',') as mon_eleven_time,
GROUP_CONCAT(DISTINCT CASE WHEN slot_set_date = '2015-01-12' AND slot_set_time = '14:00:00'
THEN AA.slot_set_time END SEPARATOR ',') as mon_two_time,
. . .
FROM ctt_photo_slot cs JOIN
ctt_region ci
ON cs.ctt_region_id = ci.ctt_region_id
GROUP BY region_name;
目前尚不清楚cs.slot_set_date
中cs.ctt_photo_slot_id
和select
正在做什么。 group by
为每个区域生成一行,因此如果有多个值,则选择任意值。