我有一个存储过程,运行速度非常慢(6秒),使用以下JOIN:
JOIN tariffs t ON
(LEFT(`cdrs`.`cnumber`,7) = t.numberrange
OR LEFT(`cdrs`.`cnumber`,8) = t.numberrange)
如果没有上述JOIN,查询将以1.5秒的速度运行。 有什么方法可以提高性能?完整存储过程如下:
CREATE DEFINER=`xxx`@`%` PROCEDURE `GetHourStats`(IN _ID INT, IN _YEAR INT, IN _MONTH INT, IN _DAY INT)
BEGIN
set @_START = UNIX_TIMESTAMP(date(_YEAR * 10000 + _MONTH * 100 + _DAY * 1));
set @_END = UNIX_TIMESTAMP(date_add(date(_YEAR * 10000 + _MONTH * 100 + _DAY * 1), interval 1 day));
SELECT h.idhour, h.`hour` as 'hour', innumber, count(*) as `count`, sum(talktime) as `duration` FROM (
SELECT
`cdrs`.`dcustomer` AS `dcustomer`,
(CASE
WHEN (LEFT(`cdrs`.`cnumber`, 2) = "01" OR LEFT(`cdrs`.`cnumber`, 2) = "02") THEN '01-02'
WHEN (LEFT(`cdrs`.`cnumber`, 2) = "03") THEN '03'
WHEN (LEFT(`cdrs`.`cnumber`, 2) = "05") THEN '05'
WHEN (LEFT(`cdrs`.`cnumber`, 2) = "06") THEN '06'
WHEN (LEFT(`cdrs`.`cnumber`, 2) = "07") THEN '07'
WHEN (LEFT(`cdrs`.`cnumber`, 3) = "080") THEN '080'
WHEN (LEFT(`cdrs`.`cnumber`, 3) = "084") THEN '084'
WHEN (LEFT(`cdrs`.`cnumber`, 3) = "087") THEN '087'
WHEN (LEFT(`cdrs`.`cnumber`, 2) = "09") THEN '09'
END) AS 'innumber',
FROM_UNIXTIME(`cdrs`.`start`) AS `start`,
(`cdrs`.`end` - `cdrs`.`start`) AS `duration`,
`cdrs`.`cnumber` AS `calling`,
`cdrs`.`talktime` AS `talktime`
FROM `cdrs`
JOIN tariffs t ON (LEFT(`cdrs`.`cnumber`,7) = t.numberrange OR LEFT(`cdrs`.`cnumber`,8) = t.numberrange)
WHERE `cdrs`.`start` >= @_START and `cdrs`.`start` < @_END
AND `cdrs`.`stype` = _LATIN1'external'
AND `cdrs`.`talktime` >= 5
AND `cdrs`.`status` = 'answer'
AND CHAR_LENGTH(`cdrs`.`cnumber`) = 11
GROUP BY callid
) cdr
JOIN customers c ON c.id = cdr.dcustomer
LEFT JOIN hub.hours h ON HOUR(cdr.`start`) = h.idhour
WHERE (c.parent = _ID or cdr.dcustomer = _ID or c.parent IN
(SELECT id FROM customers WHERE parent = _ID))
GROUP BY h.idhour, cdr.innumber
ORDER BY h.idhour;
END
问:如何让上述存储过程运行得更快?
答案 0 :(得分:1)
JOIN tariffs t ON
(LEFT(`cdrs`.`cnumber`,7) = t.numberrange
OR LEFT(`cdrs`.`cnumber`,8) = t.numberrange)
这可能会给你相同的集合:
JOIN tariffs t
ON cdrs.cnumber LIKE CONCAT(t.numberrange, '%')
如果numberrange不是7或8个字符长,它会失败,但也许&#39;不能&#39;发生。也许在某个地方添加这个可以解决这个问题:
AND LENGTH(t.numberrange) BETWEEN 7 AND 8
INDEX(numberrange) -- no longer useful
INDEX(cnumber) -- may be useful here.