0

我正在从 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_*已被弃用,但我们从另一家公司接手了这个项目,没有足够的时间切换到PDOmysqli

4

2 回答 2

1

不要运行多个查询(每个客户端一个),而是尝试为所有必需的客户端运行一个查询 - 例如:

SELECT jm.job_number, 
       jm.client,
       ac.date_audit, 
       ac.set_no,
       count(distinct case f31.rating when 1 then f31.parameter end) f31yes,
       count(distinct case f31.rating when 2 then f31.parameter end) f31no,
       count(distinct case f32.rating when 1 then f32.parameter end) f32yes,
       count(distinct case f32.rating when 2 then f32.parameter end) f32no
FROM job_master jm
JOIN accompaniment_common ac ON jm.job_number = ac.job_number
JOIN f31 ON ac.job_number = f31.job_number and ac.set_no = f31.set_no
JOIN f32 ON ac.job_number = f32.job_number and ac.set_no = f32.set_no
WHERE ac.date_audit >= '2013-04-01' AND ac.date_audit < '2013-05-01' 
  AND jm.client in (45,10,12,13,14,25,60,144)
GROUP BY jm.job_number, ac.set_no
HAVING (f31yes+f32yes) / (f31yes+f32yes+f31no+f32no) >= 0.9
于 2013-06-22T07:30:28.043 回答
0

为 JOIN 中使用的列添加索引。

ALTER TABLE f31 ADD INDEX (job_number);
ALTER TABLE f31 ADD INDEX (set_no);
ALTER TABLE f32 ADD INDEX (job_number);
ALTER TABLE f32 ADD INDEX (set_no);
ALTER TABLE accompaniment_common ADD INDEX (job_number);
ALTER TABLE accompaniment_common ADD INDEX (set_no);
ALTER TABLE job_master ADD INDEX (job_number);

有关数据库索引的更多信息,请阅读此处

于 2013-06-22T07:46:02.773 回答