有优化的方式吗?

时间:2014-06-30 17:04:36

标签: php

网页需要这些循环来获取我尝试填充的数据。代码工作得很好,但是第一次加载确实需要时间。

下次加载闪光灯。

我也试过制作程序而不是在代码中编写查询,但每次都以相同(较慢)的速度运行(而不是在上面第二次以更快的速度运行)。

  $qry4 = "SELECT   cl.name as client_name, COUNT(cl.name) as number 
  FROM candidateJoborderHistory ch 

  LEFT JOIN joborders j ON ch.joborder_id = j.job_id 
  LEFT JOIN clients cl ON j.client_id = cl.client_id 
  LEFT JOIN candidatestatus cs ON ch.status_id = cs.status_id 

  WHERE ch.status_id >=2 AND ch.status_id <=4 AND YEAR( date_interview ) = $year AND   
  QUARTER (date_interview ) = $quarter

  GROUP BY cl.name 
  ORDER BY cl.name ASC"; 
  $stmt = $conn->prepare($qry4);
  $stmt->execute();
  $row_a = $stmt->fetchAll(PDO::FETCH_ASSOC);

  // Loop to get data...
  foreach ($row_a as $row_x)
    {
        $count_client = 0;
        $table_row = '<tr><td class="rborder">'.$row_x['client_name'].'</td>';
        foreach ($row as $rec) //running 17 times from a loop aboveto fetch table headers/columns
        {
          $id=$rec['id'];
          $name = $row_x['client_name'];

          $qry5 = "SELECT cl.name as client_name, COUNT(cl.name) as number
          FROM candidateJoborderHistory ch

          LEFT JOIN candidates_info c ON ch.candidate_id = c.candidate_id
          LEFT JOIN joborders j ON ch.joborder_id = j.job_id
          LEFT JOIN clients cl ON j.client_id = cl.client_id

          LEFT JOIN interviewtypes i ON ch.interview_id = i.interview_id
          LEFT JOIN candidatestatus cs ON ch.status_id = cs.status_id

          WHERE c.recruiter_id = $id
          AND ch.status_id >=2 AND ch.status_id <=4 
          AND cl.name = '$name'

          AND YEAR( date_interview ) = $year
          AND QUARTER(date_interview ) = $quarter

          GROUP BY cl.name";

        $res = $conn->prepare($qry5);

        $res->execute();
        $row_b = $res->fetchAll(PDO::FETCH_ASSOC);

        // Loop to get data...
          $rows_found = $res->rowCount();
          if ($rows_found != 0)

          foreach ($row_b as $row_y)
          { 

        $table_row .= '<td>';
        $table_row .= $row_y['number'];
        $table_row .= '</td>';
        $count_client += $row_y['number'];

        }
         else 
           { 
            $table_row .= '<td>';
            $table_row .= '';
            $table_row .= '</td>';
           }


        }
      $table_row .= '<td><h3>'.$count_client.'</h3></td>';
      $table_row .='</tr>';
      echo $table_row . "\n";

        }

只是想知道是否有一种优化的方法来做到这一点。我可以避免循环吗?或者其他能改善提取和速度的东西。

0 个答案:

没有答案