我正在从 4 个表中提取数据,这些表是:
job_master
job_numberclientcol3col4
123 45 blah blah
456 12 blah blah
伴奏_common(f31 和 f32 将包含 3 个集合,因为此表中有 3 个)
job_numberset_nodate_audit column 4column 5column 3
123 1 2013-04-23 blah blah blah blah blah blah
456 2 2013-04-13 blah blah blah blah blah blah
123 3 2013-04-10 blah blah blah blah blah blah
f31
job_numberparameterratingcommentsset_no
123 1 2 blah blah ble1
123 2 2 blah blah ble1
123 3 1 blah blah ble1
456 1 1 blah blah ble2
456 2 1 blah blah ble2
456 3 2 blah blah ble2
123 1 2 blah blah ble3
123 2 2 blah blah ble3
123 3 1 blah blah ble3
f32
job_numberparameterratingcommentsset_no
123 1 3 blah blah ble1
123 2 1 blah blah ble1
123 3 1 blah blah ble1
456 1 2 blah blah ble2
456 2 3 blah blah ble2
456 3 1 blah blah ble2
123 1 2 blah blah ble3
123 2 2 blah blah ble3
123 3 1 blah blah ble3
我正在使用的查询是:
SELECT * FROM
(SELECT job_number, set_no, SUM(IF(rating=1,1,0)) as f31yes,
SUM(if(rating=2,1,0)) as f31no FROM f31 GROUP BY job_number, set_no) x
JOIN
(SELECT job_number, set_no, SUM(IF(rating=1,1,0)) as f32yes,
SUM(IF(rating=2,1,0)) as f32no FROM f32 GROUP BY job_number, set_no) y
ON (x.job_number = y.job_number and x.set_no = y.set_no)
JOIN
(SELECT date_audit, job_number, set_no FROM accompaniment_common) z
ON (x.job_number = z.job_number and x.set_no = z.set_no)
JOIN
(SELECT job_number, client FROM job_master) jm
ON (z.job_number = jm.job_number)
WHERE z.date_audit >= '2013-04-01' AND z.date_audit < '2013-05-01'
AND jm.client = 45
HAVING ((f31yes+f32yes)/(f31yes+f32yes+f31no+f32no)) >= 0.9
当我在 phpMyAdmin 中运行此查询时,会在2-3 秒内获得结果。这似乎是快速检索,但是当我将其放入 PHP 文件并执行它时,大约需要5-6 分钟才能完成。
当然,我为不同的客户循环运行它,例如:
$clients = array(45,10,12,13,14,25,60,144);
$total = array();
foreach($clients as $client)
{
$q = mysql_query(// the above query goes here);
$numrows = mysql_num_rows($q);
$total[$client] = $numrows;
}
echo '<pre>';
print_r($array);
echo '</pre>';
如果我按照 phpMyAdmin 所用的时间进行,整个过程应该在 30 秒内完成,但事实并非如此。这令人困惑,因为我认为我正在以适当的方式使用查询。有什么建议么?
PS:是的,我知道它mysql_*
已被弃用,但我们从另一家公司接手了这个项目,没有足够的时间切换到PDO
或mysqli