1

我有一个完整的查询,但加载需要 1.5 秒。我对 MySQL 很陌生,所以我知道我的查询不是最好的,但有没有办法优化它?我将有 5-6 个这样的查询,加载它们需要 10 多秒。

Select * From
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `JAN` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-01-01') As `JAN`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `FEB` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-02-01') As `FEB`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `MAR` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-03-01') As `MAR`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `APR` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-04-01') As `APR`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `MAY` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-05-01') As `MAY`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `JUN` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-06-01') As `JUN`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `JUL` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-07-01') As `JUL`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `AUG` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-08-01') As `AUG`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `SEP` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-09-01') As `SEP`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `OCT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-10-01') As `OCT`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `NOV` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-11-01') As `NOV`,
    (SELECT ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) as `DEC` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-12-01') As `DEC`,
    (SELECT ROUND(SUM(TotalAHT) / CallHandled) as Total_Metric
        FROM
        (SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-01-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-02-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-03-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-04-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-05-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-06-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-07-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-08-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-09-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-10-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-11-01' UNION ALL
        SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as `TotalAHT` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id' AND `Month_Date` = '2013-12-01') As TotalAHT, 
        (SELECT SUM(`Calls_Handled_Ct`) as `CallHandled` FROM `enterprise_rep_agent_stats` WHERE `Employee_ID` = '$id') As CallHandled) As `Total_Metric`

编辑

通过下面的答案,我可以获得我想要的数字。有没有办法让每列的结果而不是一列中的所有内容?

预期结果:

JAN     FEB     MAR     APR     MAY     JUN     JUL     AUG     
148     168     175     160     165     145     162     143
4

2 回答 2

2

您似乎正在寻找月份分组统计信息,而不是使用分组您每月进行查询。我会建议类似:

SELECT 
    ROUND(
           (
            SUM(`I_Talk_Time_Sec`) + 
            SUM(`Hold_Time_Sec`) + 
            SUM(`I_Work_Time_Sec`) + 
            SUM(I_AUX_Out_Time_Sec)
           ) / SUM(`Calls_Handled_Ct`)
         ) AS time_spent, 
     YEAR(Month_Date) stats_year, 
     MONTH(Month_Date) AS stats_month 
FROM `enterprise_rep_agent_stats`
WHERE `Employee_ID` = '$id'
GROUP BY stats_year, stats_month;
于 2013-09-09T06:58:15.057 回答
0

如果我设法正确地阅读它(我的眼睛在某些时候开始受伤),那么这样的事情应该可以工作:

SELECT (ROUND((SUM(`I_Talk_Time_Sec`) + SUM(`Hold_Time_Sec`) + SUM(`I_Work_Time_Sec`) + SUM(I_AUX_Out_Time_Sec)) / SUM(`Calls_Handled_Ct`)) * SUM(`Calls_Handled_Ct`)) as sum, 
DATE_FORMAT(NOW(),'%Y-%m') as month
FROM enterprise_rep_agent_stats 
GROUP BY month
WITH ROLLUP
于 2013-09-09T06:56:54.913 回答