3

就“技能名称”的子查询选择而言,以下查询相当慢。当我针对 SQL 执行运行配置文件时,我从 ACDCallinformation 表中针对技能名称的子查询的每行查询太多。

优化此 SQL 查询的最佳方法是什么,或者是否有 MySQL 工具来帮助检查 SQL 查询的成本和优化脚本?

SELECT 
CASE 
    WHEN(
            SELECT 
                COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`)
            FROM acdcallinformation ag
            WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL AND DATEofcall = DATE(NOW()) AND ag.skillid = acdcallinformation.skillid
        ) IS NULL 
    THEN 
            0 
    ELSE
        (
            SELECT COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`)
                FROM acdcallinformation ag
            WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL AND DATEofcall= DATE(NOW()) AND ag.skillid = acdcallinformation.skillid) 
        END AS 'Lost Calls', 
        CASE WHEN COUNT(acdcallinformation.idleonqueue) IS NULL THEN 0 ELSE COUNT(acdcallinformation.idleonqueue) END AS 'Total Calls', 
        CASE WHEN COUNT(acdcallinformation.`ANSWERTIME`) IS NULL THEN 0 ELSE COUNT(acdcallinformation.`ANSWERTIME`) END AS 'Answered',
    (
        SELECT 
            skillinfo.skillname
        FROM skillinfo
        WHERE skillinfo.pkey = acdcallinformation.skillid
    ) AS Skill, 
    SEC_TO_TIME(AVG(TIME_TO_SEC(answertime)- TIME_TO_SEC(firstringonqueue))) AS 'Average Answer Time', 
    SEC_TO_TIME(AVG(TIME_TO_SEC(IDLEONQUEUE) - TIME_TO_SEC(answertime))) AS 'Average Talk Time'
FROM `acdcallinformation` acdcallinformation
WHERE DATEOFCALL = DATE(NOW())
GROUP BY skill;    

不确定显示数据的最佳方式:

ACDCALLINFORMATION - 当前行数 3028

INSTIME              PKEY   DATEOFCALL  CONNECTTIME FIRSTRING SKILLID
2012-07-19 14:50:16  19985  2012-07-19  14:50:16    14:50:16  5

SKILLINFO - 平均行数为 5-10

INSTIME              PKEY   SKILLNAME
2012-07-01 13:12:01  1      Calls Outgoing
2012-07-01 13:12:01  2      Call Centre
2012-07-01 13:12:01  3      Accounts
2012-07-01 13:12:01  4      Reception

这是预期的输出:

"Lost Calls"    "Total Calls"   "Answered"  "Skill"         "Average Answer Time" "Average Talk Time"

"1"         "2"          "1"            "Accounts"  "00:00:04"  "00:00:01"
"0"         "5"          "5"            "Service"   "00:00:07"  "00:01:20"
4

4 回答 4

3

试试这个,正在使用内部连接来提高性能并避免不必要的子查询

SELECT 
    COALESCE(ag.skillcount, 0) AS 'Lost Calls', 
    CASE WHEN COUNT(acdcallinformation.idleonqueue) IS NULL THEN 0 ELSE COUNT(acdcallinformation.idleonqueue) END AS 'Total Calls', 
    CASE WHEN COUNT(acdcallinformation.`ANSWERTIME`) IS NULL THEN 0 ELSE COUNT(acdcallinformation.`ANSWERTIME`) END AS 'Answered',
    si.skillname AS Skill, 
    SEC_TO_TIME(AVG(TIME_TO_SEC(answertime)- TIME_TO_SEC(firstringonqueue))) AS 'Average Answer Time', 
    SEC_TO_TIME(AVG(TIME_TO_SEC(IDLEONQUEUE) - TIME_TO_SEC(answertime))) AS 'Average Talk Time'
FROM `acdcallinformation` acdcallinformation
LEFT JOIN (
    SELECT skillid,  COUNT(`PKEY`) - COUNT(`ANSWERTIME`) skillcount
    FROM acdcallinformation 
    WHERE (`COMPLETED`) = 1 AND answertime IS NULL AND DATEofcall = DATE(NOW())
) ag ON  AND ag.skillid = acdcallinformation.skillid
LEFT JOIN skillinfo si ON si.pkey = acdcallinformation.skillid
WHERE DATEOFCALL = DATE(NOW())
GROUP BY si.skillname;   
于 2013-02-07T16:41:48.920 回答
2

看起来您正在尝试确保将NULLs 转换为0s。因此:

SELECT 
  IFNULL(
    (SELECT COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`) FROM acdcallinformation ag
       WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL 
        AND DATEofcall = DATE(NOW()) AND ag.skillid = acdcallinformation.skillid
    ), 0)  AS 'Lost Calls', 
    IFNULL(COUNT(acdcallinformation.idleonqueue), 0) AS 'Total Calls', 
    IFNULL(COUNT(acdcallinformation.`ANSWERTIME`),0) AS 'Answered',
    (
        SELECT 
            skillinfo.skillname
        FROM skillinfo
        WHERE skillinfo.pkey = acdcallinformation.skillid
    ) AS Skill, 
    SEC_TO_TIME(AVG(TIME_TO_SEC(answertime)- TIME_TO_SEC(firstringonqueue))) AS 'Average Answer Time', 
    SEC_TO_TIME(AVG(TIME_TO_SEC(IDLEONQUEUE) - TIME_TO_SEC(answertime))) AS 'Average Talk Time'
FROM `acdcallinformation` acdcallinformation
WHERE DATEOFCALL = DATE(NOW())
GROUP BY skill;

NULL虽然,使用使用这些数据的语言将这些 s转换为零可能更容易......只是一个想法。

此外,我对文档的阅读COUNT让我认为它永远不会返回NULL,因此:

SELECT 
  (SELECT COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`) FROM acdcallinformation ag
       WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL 
        AND DATEofcall = DATE(NOW()) AND ag.skillid = acdcallinformation.skillid
  )  AS 'Lost Calls', 
  COUNT(acdcallinformation.idleonqueue) AS 'Total Calls', 
  COUNT(acdcallinformation.`ANSWERTIME`) AS 'Answered',
    (
        SELECT 
            skillinfo.skillname
        FROM skillinfo
        WHERE skillinfo.pkey = acdcallinformation.skillid
    ) AS Skill, 
    SEC_TO_TIME(AVG(TIME_TO_SEC(answertime)- TIME_TO_SEC(firstringonqueue))) AS 'Average Answer Time', 
    SEC_TO_TIME(AVG(TIME_TO_SEC(IDLEONQUEUE) - TIME_TO_SEC(answertime))) AS 'Average Talk Time'
FROM `acdcallinformation` acdcallinformation
WHERE DATEOFCALL = DATE(NOW())
GROUP BY skill;

最后,我认为您可以使用JOIN

SELECT 
      IFNULL(
        (SELECT COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`) FROM acdcallinformation ag
           WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL 
            AND DATEofcall = DATE(NOW()) AND ag.skillid = acdcallinformation.skillid
        ), 0)  AS 'Lost Calls', 
        IFNULL(COUNT(acdcallinformation.idleonqueue), 0) AS 'Total Calls', 
        IFNULL(COUNT(acdcallinformation.`ANSWERTIME`),0) AS 'Answered',
        skillinfo.skillname AS Skill, 
        SEC_TO_TIME(AVG(TIME_TO_SEC(answertime)- TIME_TO_SEC(firstringonqueue))) AS 'Average Answer Time', 
        SEC_TO_TIME(AVG(TIME_TO_SEC(IDLEONQUEUE) - TIME_TO_SEC(answertime))) AS 'Average Talk Time'
    FROM `acdcallinformation` acdcallinformation 
       INNER JOIN skillinfo ON skillinfo.pkey = acdcallinformation.skillid 
    WHERE DATEOFCALL = DATE(NOW())
    GROUP BY skill;
于 2013-02-07T16:35:29.740 回答
2

试试这个查询。整个查询只是一个猜测,但如果您提供一些数据,那将是一个打击。我也使用 id 作为主键,您需要用自己的键替换它。避免使用子查询,而是使用连接它们更快。这是查询。

SELECT 
    IF(l.LDifference IS NULL,0,r.RDifference) AS 'Lost Calls', 
    IF(COUNT(acdcallinformation.idleonqueue) IS NULL , 0 , COUNT(acdcallinformation.idleonqueue))AS 'Total Calls', 
    IF(COUNT(acdcallinformation.`ANSWERTIME`) IS NULL,0,COUNT(acdcallinformation.`ANSWERTIME`))AS 'Answered',
    (SELECT skillinfo.skillname FROM skillinfo  WHERE skillinfo.pkey = acdcallinformation.skillid) AS Skill, 
    SEC_TO_TIME(AVG(TIME_TO_SEC(a.answertime)- TIME_TO_SEC(a.firstringonqueue))) AS 'Average Answer Time', 
    SEC_TO_TIME(AVG(TIME_TO_SEC(a.IDLEONQUEUE) - TIME_TO_SEC(a.answertime))) AS 'Average Talk Time'
FROM acdcallinformation as a
INNER JOIN( 
    SELECT  
        (COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`)) as `LDifference`
    FROM acdcallinformation ag
    WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL AND DATEofcall = DATE(NOW()) AND ag.skillid = acdcallinformation.skillid  
) as l ON l.id = a.id
INNER JOIN( 
    SELECT (COUNT(ag.`PKEY`) - COUNT(ag.`ANSWERTIME`)) as `RDifference`
        FROM acdcallinformation ag
    WHERE (ag.`COMPLETED`) = 1 AND answertime IS NULL AND DATEofcall= DATE(NOW()) AND ag.skillid = acdcallinformation.skillid
) as r ON r.id = a.id   
WHERE a.DATEOFCALL = DATE(NOW())
GROUP BY skill;
于 2013-02-07T16:36:21.920 回答
2

试试这个。使用 INNER JOIN、IF() 并尽量避免不必要的子查询。

SELECT IFNULL(ag.skillcount, 0) AS 'Lost Calls', COUNT(info.idleonqueue) AS 'Total Calls', 
         COUNT(info.ANSWERTIME) AS 'Answered', si.skillname AS Skill, 
         SEC_TO_TIME(AVG(TIME_TO_SEC(answertime)- TIME_TO_SEC(firstringonqueue))) AS 'Average Answer Time', 
         SEC_TO_TIME(AVG(TIME_TO_SEC(IDLEONQUEUE) - TIME_TO_SEC(answertime))) AS 'Average Talk Time'
FROM acdcallinformation AS info
INNER JOIN (
            SELECT skillid, COUNT(PKEY)-COUNT(ANSWERTIME) skillcount
            FROM acdcallinformation 
            WHERE COMPLETED = 1 AND DATEofcall = DATE(NOW()) AND answertime IS NULL 
           ) ag ON ag.skillid = info.skillid
INNER JOIN skillinfo si ON si.pkey = info.skillid
WHERE DATEOFCALL = DATE(NOW())
GROUP BY si.skillname;
于 2013-02-07T17:58:29.353 回答