[英]php query taking too much time to complete report
你好朋友我正在生成一個報告,首先我得到這樣的材料代碼
$sql_srh="select Material_code, Mat_type , Material_Desc , UOM from (
select Material_code, Mat_type , Material_Desc , UOM from opening_data where Mat_type in ('YACC' ,
'YFAB' , 'YLFB' , 'YLAC' , 'YSFAB' , 'YSVM' , 'YSNV' ) and datee < '".$frm."' and (location like 'J%' or location like 'L%' ) union all
select Material_code, Mat_type , Material_Desc , UOM from material_data where Mat_type in ('YACC' ,
'YFAB' , 'YLFB' , 'YLAC' , 'YSFAB' , 'YSVM' , 'YSNV') and (location like 'J%' or location like 'L%' )
) x group by Material_code";
然后我使用上面的結果(材料代碼)我試圖生成一個報告,我在下面添加了代碼來從兩個表生成報告。
$res = $conn->query($sql_srh);
while($res_row = $res->fetch_assoc()){
$res_open=$conn->query("Select Sum(qty) open_blnc From (
select qty from opening_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and datee < '".$frm."' and (location like 'J%' or location like 'L%' )
Union all
select qty from material_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and Posting_date < '".$frm."' and
Debit_Credit ='S' and Move_type in ('101' , '511' , '315' , '309' , '262' , '202' , '123' , '544' ,
'334' , '552' , '310' , '314') and (location like 'J%' or location like 'L%' )
Union all
select qty from material_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and Posting_date < '".$frm."' and
Debit_Credit ='H' and Move_type in ('102' , '512' , '316' , '310' , '261' , '201' , '122' , '543' ,
'333' , '551' , '309' , '313') and (location like 'J%' or location like 'L%' )
)x");
$row_open = $res_open->fetch_assoc();
$res_in=$conn->query("select sum(qty) as in_qty from (
select qty from material_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and Posting_date >= '".$frm."' and
Posting_date <= '".$tdt."' and Debit_Credit ='S' and Move_type in ('101' , '511' , '315' , '309') and
(location like 'J%' or location like 'L%' )
Union all
select qty from material_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and Posting_date >= '".$frm."' and
Posting_date <= '".$tdt."' and Debit_Credit ='H' and Move_type in ('102' , '512' , '316' , '310')
and (location like 'J%' or location like 'L%' ) )x");
$row_in = $res_in->fetch_assoc();
$sql_out="select sum(qty) as out_qty from (
select qty from material_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and Posting_date >= '".$frm."' and
Posting_date <= '".$tdt."' and Debit_Credit ='H' and Move_type in ('261' , '201' , '122' , '543' ,
'333' , '551' , '309' , '313') and (location like 'J%' or location like 'L%' )
Union All
select qty from material_data where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM'
, 'YSNV') and Material_code = '".$res_row['Material_code']."' and Posting_date >= '".$frm."' and
Posting_date <= '".$tdt."' and Debit_Credit ='S' and Move_type in ('262' , '202' , '123' , '544' ,
'334' , '552' , '310' , '314') and (location like 'J%' or location like 'L%' ))x";
$res_out = $conn->query($sql_out);
$row_out = $res_out->fetch_assoc();
$sql_adj ="Select Sum(adj) as adj_qty , Sum(opname) as opnam_qty from ( select adj , opname from
tbl_adj_opname where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' , 'YSFB' , 'YSVM' , 'YSNV') and
Material_code = '".$res_row['Material_code']."' and datee >= '".$frm."' and datee <= '".$tdt."' and
(location like 'J%' or location like 'L%' ) and Debit_Credit = 'S'
Union All
select adj , opname from tbl_adj_opname where Mat_type in ('YACC' , 'YFAB' , 'YLFB' , 'YLAC' ,
'YSFB' , 'YSVM' , 'YSNV') and Material_code = '".$res_row['Material_code']."' and datee >= '".$frm."'
and datee <= '".$tdt."' and (location like 'J%' or location like 'L%' ) and Debit_Credit = 'H'
)x";
$res_adj = $conn->query($sql_adj);
$row_adj = $res_adj->fetch_assoc();
這工作正常,我的問題是生成報告需要幾個小時,我在表中有 200000 行。 如果我評論上面的代碼(意味着未結余額,數量和數量)並且只運行第一個代碼(我正在獲取材料代碼),它會在幾秒鍾內完成,但是當我嘗試生成 Comelete 報告它需要幾個小時。
其中 Mat_type 和 Move_Type 是固定的。
結果將是: ?>
<td class="sn"><?php echo $i;?></td>
<td><?php echo $res_row['Material_code'];?></td>
<td><?php echo $res_row['Material_Desc'];?></td>
<td><?php echo $res_row['UOM'];?></td>
<td class="lft"><?php echo $open ;?></td>
<td class="lft"><?php echo abs($row_in['in_qty']);?></td>
<td class="lft"><?php echo $outqty= ($row_out['out_qty'])*(-1);?></td>
<td><?php echo $row_adj['adj_qty'];?></td>
<td class="lft"><?php $balance = (($open+(abs($row_in['in_qty'])))-
(abs($outqty)))+($row_adj['adj_qty']);
echo round($balance,3);
?></td>
<td class="lft"><?php /*echo $opnam = ($open)+($row_in['in_qty'])-
(abs($outqty));*/ $opnam = $row_adj['opnam_qty'];?></td>
<td class="lft"><?php $diff = ($balance)-($opnam); echo round($diff,3); ?>
</td>
<td></td>
</tr>
<?php } ?>
在這里找到帶有測試數據的表格,請幫助我。
使用內連接
前任:
SELECT *
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.