gpt4 book ai didi

mysql - 如何优化具有计算的 MySQL 查询

转载 作者:行者123 更新时间:2023-11-29 06:45:49 25 4
gpt4 key购买 nike

我有一个完全可用的查询,但需要 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`

编辑

有了下面的答案,我就能得到我想要的数字。有没有办法使每列的结果而不是 1 列中的所有内容?

预期结果:

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

最佳答案

您似乎在寻找按月分组的统计信息,而不是使用分组,而是每月进行一次查询。我建议像这样:

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;

关于mysql - 如何优化具有计算的 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18692793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com