具有多个计数的MySQL性能

时间:2015-08-03 10:30:10

标签: php mysql

在一个查询中拥有多个MySQL Count个实例或为每个计数实例分别进行查询是否更快?如果是这样,我将如何组合这两个查询?

 $msd_call_qry = "SELECT count(event) AS MissedCalls  FROM queuelogdb.queue_log WHERE (agent = '$agent_name[0]' OR agent = '$agent_name[1]')"
                . " AND (event = 'ABANDON' OR event = 'RINGNOANSWER') AND queuename IN ('500','505') AND time BETWEEN '$lastDay' AND '$today'";

 $ttl_call_qry = "SELECT count(event) AS TotalCalls from queuelogdb.queue_log WHERE (agent = '$agent_name[0]' OR agent = '$agent_name[1]') "
                . "AND event = 'CONNECT' AND queuename IN ('500','505') AND time BETWEEN '$lastDay' AND '$today'";

1 个答案:

答案 0 :(得分:2)

你的桌子是一样的。 WHERE子句中的大多数条件都是相同的。所以,你应该能够使用条件聚合:

SELECT SUM(event = 'ABANDON' OR event = 'RINGNOANSWER') AS MissedCalls,
       SUM(event = 'CONNECT') AS TotalCalls
FROM queuelogdb.queue_log
WHERE agent IN ('$agent_name[0]', '$agent_name[1]') AND
      queuename IN ('500', '505') AND
      time BETWEEN '$lastDay' AND '$today'". ";