2016-10-18 2 views
1

mysql을 사용하는 작업 업데이트 쿼리가 있지만 현재 페이지를로드하는 속도가 매우 느립니다. 내 쿼리를 더 빨리 업데이트 할 수있는 방법이 있습니까?PHP/MYSQL- 루핑 내부 업데이트. 느린로드 페이지

여기에 내 코드

<?php 

    $sql = "select ite_desc,ecr_desc, pric_cash, t.itemcode as itemcode ,sum(t.qty) as qty 
     from ( 
    select ite_desc,ecr_desc, pric_cash, itemcode,qty from barcode as bc inner JOIN allinvty3 as ait on bc.itemcode = ait.in_code 
    union all 
    select ite_desc,ecr_desc, pric_cash, itemcode,qty from branchtobranch_tb as bb inner JOIN allinvty3 as ait on bb.itemcode = ait.in_code 
    union all 
    select ite_desc,ecr_desc, pric_cash, itemcode,qty from adjustment_tb as adt inner JOIN allinvty3 as ait1 on adt.itemcode = ait1.in_code where adt.status='APPROVED' 
    union all 
    select ite_desc,ecr_desc, pric_cash, itemcode,qty from stockreturn_tb as sb inner JOIN allinvty3 as ait on sb.itemcode = ait.in_code 
    union all 
    select ite_desc,ecr_desc, pric_cash, itemcode,qty from notinclude_tb as nt inner JOIN allinvty3 as ait on nt.itemcode = ait.in_code where nt.status='COMPLETE' 
    union all 
    select ite_desc,ecr_desc, pric_cash, itemcode,qty from purchase_tb as pt inner JOIN allinvty3 as ait on pt.itemcode = ait.in_code 
    union all 
    select ite_desc,ecr_desc, pric_cash, itemcode,(qty * -1) from soldout_pd as slp inner JOIN allinvty3 as ait2 on slp.itemcode = ait2.in_code) as t 
    group by itemcode order by ecr_desc ASC "; 
    $result = $conn->query($sql); 
    if ($result->num_rows > 0) { 

    echo " 

    </tr>"; 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
    $total =$row['qty']; 
    $itemcode=$row['itemcode']; 
    $sql1="UPDATE allinvty3 set sa_onhand = '".$total."' where in_code ='".$itemcode."'" ; 
    $conn->query($sql1); 
    }echo " </table>";} 
    ?> 
의 단일 쿼리로이 작업을 수행 할 수 있습니다
+0

경고 ** ** mysql을 사용할 때 [매개 변수가있는 쿼리] (http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)와 ['bind_param' ] (http://php.net/manual/en/mysqli-stmt.bind-param.php)를 사용하여 쿼리에 사용자 데이터를 추가하십시오. ** 심각한 [SQL injection bug] (http://bobby-tables.com/)를 만들었 기 때문에 문자열 보간이나 연결을 사용하여 **이를 수행하지 마십시오. **'$ _POST' 또는'$ _GET' 데이터를 쿼리에 직접 입력하지 마십시오. 실수로 누군가를 악용하려고 시도하면 매우 위험 할 수 있습니다. – tadman

답변

2

: 데이터베이스셔서,이 적어도 update 루프를 제거합니다

update allinvty3 a join 
     (select t.itemcode, sum(t.qty) as qty 
     from ((select ite_desc,ecr_desc, pric_cash, itemcode, qty 
       from barcode bc inner join 
        allinvty3 ait 
        on bc.itemcode = ait.in_code 
      ) union all 
       (select ite_desc, ecr_desc, pric_cash, itemcode, qty 
       from branchtobranch_tb bb inner join 
        allinvty3 ait 
        on bb.itemcode = ait.in_code 
      ) union all 
       (select ite_desc, ecr_desc, pric_cash, itemcode, qty 
       from adjustment_tb adt inner join 
        allinvty3 ait1 
        on adt.itemcode = ait1.in_code 
       where adt.status = 'APPROVED' 
      ) union all 
       (select ite_desc, ecr_desc, pric_cash, itemcode, qty 
       from stockreturn_tb sb inner join 
        allinvty3 ait 
        on sb.itemcode = ait.in_code 
      ) union all 
       (select ite_desc, ecr_desc, pric_cash, itemcode, qty 
       from notinclude_tb nt inner join 
        allinvty3 ait 
        on nt.itemcode = ait.in_code 
       where nt.status='COMPLETE' 
      ) union all 
       (select ite_desc, ecr_desc, pric_cash, itemcode, qty 
       from purchase_tb pt inner join 
        allinvty3 ait 
        on pt.itemcode = ait.in_code 
      ) union all 
       (select ite_desc, ecr_desc, pric_cash, itemcode, (qty * -1) 
       from soldout_pd slp inner join 
        allinvty3 ait2 
        on slp.itemcode = ait2.in_code 
      ) 
      ) t 
     group by itemcode 
     ) i 
     on a.in_code = i.itemcode 
    set a.sa_onhand = i.qty; 

오히려 일을 응용 프로그램보다. 성능에 문제가 있다면 union all 일 것입니다. 이 경우 각 하위 쿼리를 조사해야합니다.

+0

완벽하게 작동합니다! :) – codeSeven