优化这段SQL:SELECT CONCAT( HOUR ( mail_send_time ) DIV 1 * 1, ':00-', HOUR ( mail_send_time ) DIV 1 * 1, ':59' ) AS timeInterval, IFNULL( COUNT( * ), 0 ) AS total, IFNULL( SUM( contact_account_type = IF(cv.code_value='01',cv.code_description,'') ), 0 ) AS wxTotal, IFNULL( SUM( contact_account_type = IF(cv.code_value='02',cv.code_description,'') ), 0 ) AS qqTotal, IFNULL( SUM( local_action = IF(cv.code_value='05',cv.code_description,'') AND contact_account_type = IF(cv.code_value='01',cv.code_description,'')), 0 ) AS wxSend, IFNULL( SUM( local_action = IF(cv.code_value='06',cv.code_description,'') AND contact_account_type = IF(cv.code_value='01',cv.code_description,'')), 0 ) AS wxReceive , IFNULL( SUM( local_action = IF(cv.code_value='05',cv.code_description,'') AND contact_account_type = IF(cv.code_value='02',cv.code_description,'')), 0 ) AS qqSend, IFNULL( SUM( local_action = IF(cv.code_value='06',cv.code_description,'') AND contact_account_type = IF(cv.code_value='02',cv.code_description,'')), 0 ) AS qqReceive FROM dwd_t_friend_talk, dwd_t_code_value cv WHERE person_no = '653129200105072624' AND contact_account_type IN ( IF(cv.code_value='01',cv.code_description,''), IF(cv.code_value='02',cv.code_description,'')) AND friend_account <> '' AND contact_account_type <> '' AND mail_send_time IS NOT NULL AND mail_send_time <> '' GROUP BY HOUR ( mail_send_time ) DIV 1
时间: 2024-04-07 10:29:57 浏览: 86
ORA-00904: "WM_CONCAT": 标识符无效
5星 · 资源好评率100%
可以考虑以下几种优化方式:
1. 使用JOIN语句代替WHERE语句中的子查询,可以提高查询效率。
2. 对mail_send_time字段创建索引,可以加快查询速度。
3. 可以尝试将COUNT和SUM聚合函数合并成一个SELECT语句,以减少查询次数。
4. 可以考虑将IFNULL函数替换为COALESCE函数,因为COALESCE函数在MySQL中的性能更好。
优化后的SQL如下:
SELECT CONCAT(HOUR(mail_send_time) DIV 1 * 1, ':00-', HOUR(mail_send_time) DIV 1 * 1, ':59') AS timeInterval,
COUNT(*) AS total,
SUM(COALESCE(contact_account_type = IF(cv.code_value='01',cv.code_description,''), 0)) AS wxTotal,
SUM(COALESCE(contact_account_type = IF(cv.code_value='02',cv.code_description,''), 0)) AS qqTotal,
SUM(COALESCE(local_action = IF(cv.code_value='05',cv.code_description,'') AND contact_account_type = IF(cv.code_value='01',cv.code_description,''), 0)) AS wxSend,
SUM(COALESCE(local_action = IF(cv.code_value='06',cv.code_description,'') AND contact_account_type = IF(cv.code_value='01',cv.code_description,''), 0)) AS wxReceive,
SUM(COALESCE(local_action = IF(cv.code_value='05',cv.code_description,'') AND contact_account_type = IF(cv.code_value='02',cv.code_description,''), 0)) AS qqSend,
SUM(COALESCE(local_action = IF(cv.code_value='06',cv.code_description,'') AND contact_account_type = IF(cv.code_value='02',cv.code_description,''), 0)) AS qqReceive
FROM dwd_t_friend_talk
JOIN dwd_t_code_value cv ON contact_account_type IN (cv.code_description)
WHERE person_no = '653129200105072624'
AND friend_account <> ''
AND contact_account_type <> ''
AND mail_send_time IS NOT NULL
AND mail_send_time <> ''
GROUP BY HOUR(mail_send_time) DIV 1;
阅读全文