Php脚本将数据输入数据库的速度很慢

时间:2017-02-03 09:35:33

标签: php mysql

<?php 

// Check connection
if ($conn->connect_error)
 {
  die("Connection failed: " . $conn->connect_error);
 }     

echo "\n\nCONNECTED TO DATABASE:\n";           
$whereclause = 1;

// initialize array
$arrVTV  = array ( "FTR" => array ( "ACC"  => array ( "Letter" => array ( 0 => 0 ) ) ) );
$arrm  = array ( "FTR" => array ( "Letter" => array ( 0 => 0  ) ) );
$yeasrmonths = array("1991001","1991002","1991003","1991004","1991005","1991006","1991007","1991008","1991009","1991010","1991011","1991012","1991101","1991102","1991103","1991104","1991105","1991106","1991107","1991108","1991109","1991110","1991111","1991112","1991201","1991202","1991203","1991204","1991205","1991206","1991207","1991208","1991209","1991210","1991211","1991212","1991301","1991302","1991303","1991304","1991305","1991306","1991307","1991308","1991309","1991310","1991311","1991312","1991401","1991402","1991403","1991404","1991405","1991406","1991407","1991408","1991409","1991410","1991411","1991412","1991501","1991502","1991503","1991504","1991505","1991506","1991507","1991508","1991509","1991510","1991511","1991512","1991601","1991602","1991603","1991604","1991605","1991606","1991607","1991608","1991609","1991610","1991611","1991612","1991701","1991702","1991703","1991704","1991705","1991706","1991707","1991708","1991709","1991710","1991711","1991712","1991801","1991802","1991803","1991804","1991805","1991806","1991807","1991808","1991809","1991810","1991811","1991812","1991901","1991902","1991903","1991904","1991905","1991906","1991907","1991908","1991909","1991910","1991911","1991912","1992001","1992002","1992003","1992004","1992005","1992006","1992007","1992008","1992009","1992010","1992011","1992012");
$j = 0;

// lost table Column Count
   $lost = "SELECT COUNT(*) as cnt FROM information_schema.columns WHERE table_name = 'post_master'";
   $lostres = $conn->query($lost);
   $mac_cols = $lostres->fetch_assoc();
   $losttotal = $mac_cols["cnt"]-5;

   for($i = 1;$i<=$losttotal;$i++)
   {
    if($i == $losttotal){
    $flosts .= "lost".$i;}
    else{
    $flosts .= "lost".$i.",";}
   }

 // lost table to array creation
   $mquery = "SELECT country_zipcode,yearperiod,".$flosts." FROM `post_master`";
   $lostres = $conn->query("$mquery");
    if ($lostres->num_rows > 0) {
        while($lostrow = $lostres->fetch_array()) {
            $country_zipcode = $lostrow['country_zipcode'];  
            $yearperiod = $lostrow['yearperiod'];
            for($i = 1;$i<=$losttotal;$i++)
            {
            $mc_num =  'lost'.$i;
            $arrm[$country_zipcode][$yearperiod][$mc_num] = $lostrow['lost'.$i];
            }
        }
    }

  // for each rep_countrycode run
   $sql = "SELECT DISTINCT ACCode3,zcode FROM `rep_main`";
   $result = $conn->query($sql);
   if ($result->num_rows > 0) {
   // output data of each row
    while($row = $result->fetch_assoc()) {

        //output Table name
        $tblname = $row["ACCode3"]."_".$row["zcode"];
        // Old Table name
        $rep_country= "$tblname" ."_4_digits";

        // create table structure 
        $conn->query("DROP TABLE IF EXISTS $tblname");
        $sql_tname = "CREATE TABLE $tblname Like rep_rawtable";
        $ftable = $conn->query($sql_tname);


                // code for reporter_4_digits table to array
                $q  =  "SELECT SPOO3,SNOO3,ACC,FTR,VTV,Yearperiod FROM $rep_country WHERE $whereclause ORDER BY FTR,ACC,SPOO3";
                $VTVres = $conn->query("$q");
                if ($VTVres->num_rows > 0) {
                    while($VTVrow = $VTVres->fetch_array()) {
                        $FTR = $VTVrow['FTR'];   
                        $ACC = $VTVrow['ACC'];   
                        $VTV = $VTVrow['VTV'];
                        $pc =  $VTVrow['SPOO3'];
                        $y =  $VTVrow['Yearperiod'];
                        $arrVTV[$FTR][$ACC]["$pc"][$y] += $VTV;
                    }
                }
                mysqli_free_result($VTVres);


                // code for get the max year where forcasting begin
                $maxyear = "SELECT max(Yearperiod) AS yp FROM $reporter";
                $myear = $conn->query("$maxyear");
                $maxrow = $myear->fetch_assoc();



                // for inserting the values into the table 
                $dis_value  =  "SELECT DISTINCT SPOO3,SNOO3,ACC,FTR FROM $rep_country WHERE $whereclause ORDER BY SNOO3 ASC";
                $dis_value_result = $conn->query("$dis_value");

                if ($dis_value_result->num_rows > 0) 
                {
                        while($VTVrow = $dis_value_result->fetch_assoc()) 
                        {
                            $FTR = $VTVrow['FTR'];   
                            $ACC = $VTVrow['ACC'];   
                            $pp  =  $VTVrow['SPOO3'];
                            $SNOO3 = $VTVrow['SNOO3'];
                            $maincnt = 0;
                            $cntcellval = 0;
                            $j = 0;
                            $cntsum = 0;
                            $sumsqrt = 0;
                            $stackbefore = array();
                            $stack = array();
                            $arrsqrt = array();
                            $arrsntsum = array();
                            $stacklost = array();
                            $stklost  = array ( "lost" => array ( 0 => 0 ) );

                            $startdel = 0;
                            $ncode = $row["zcode"];

                            for($i = 0;$i<count($yeasrmonths);$i++)
                            {
                                // before timeseries code is in if condition
                                if($maxrow[yp]>=$yeasrmonths[$i]){
                                $maincnt = $maincnt + 1;
                                $yp = $yeasrmonths[$i];
                                $monthlyval = $arrVTV[$FTR][$ACC]["$pp"][$yp];
                                array_push($stackbefore, $monthlyval);
                                array_push($stack, $monthlyval);

                                $cntsum = $cntsum + $maincnt;
                                $sumsqrt = $sumsqrt + pow(($i+1),2);

                                    for($kk = 1;$kk<=$losttotal;$kk++)
                                    {
                                        $stklost["lost".$kk][$i] = $monthlyval;
                                        $arrsntsum["lost".$kk] = $arrsntsum["lost".$kk] + $arrm[$ncode][$yp]["lost".$kk];
                                        $arrsqrt["lost".$kk] = $arrsqrt["lost".$kk] + pow(($arrm[$ncode][$yp]["lost".$kk]),2);   
                                    }                                   
                                }
                                else // where timeseries begin and average final score
                                {

                                        // code for linear timeseries analysis
                                        $flag ="true";
                                        $j = $j + 1;
                                        $cntcellval = $maincnt + $j;
                                        $aa = forecasting($cntcellval,$maincnt,$cntsum,$sumsqrt,$FTR,$pp,$ACC,$stackbefore,$flag,$yp,$ncode,$mcrow,$arrm,$yeasrmonths,$monthlyval);   
                                        $stack[$i] += $aa;
                                        array_push($stackbefore, $aa);
                                        $flag ="false";

                                        // code for lost score
                                        for($kk = 1;$kk<=$losttotal;$kk++)
                                        {
                                            $ymonth = $yeasrmonths[$i];
                                            $aa = forecasting($cntcellval,$maincnt,$arrsntsum["lost".$kk],$arrsqrt["lost".$kk],$FTR,$pp,$ACC,$stackbefore,$flag,$ymonth,$ncode,$kk,$arrm,$yeasrmonths,$stklost);
                                            $stack[$i] += $aa;
                                            $stklost["lost".$kk][$i]  = $aa;
                                            $delfromstart = $yeasrmonths[$startdel];
                                            $arrsntsum["lost".$kk] = $arrsntsum["lost".$kk] + $arrm[$ncode][$ymonth]["lost".$kk] - $arrm[$ncode][$delfromstart]["lost".$kk];
                                            $arrsqrt["lost".$kk] = $arrsqrt["lost".$kk] + pow(($arrm[$ncode][$ymonth]["lost".$kk]),2) - pow(($arrm[$ncode][$delfromstart]["lost".$kk]),2);  
                                        }
                                        $startdel = $startdel + 1;
                                        $cntsum = $cntsum + $maincnt;
                                        $sumsqrt = $sumsqrt + pow($maincnt,2) + (($maincnt*2)*$j);
                                }

                                // this is used for final accounts markup
                                if($maxrow[yp]>=$yeasrmonths[$i]){}else{
                                $stack[$i] = $stack[$i]/($losttotal+1);
                                }

                            }
                            $COST4 = $row['zcode'];
                            $CONT4 = $row['ACCode3'];

                            // insert query
                            $sql = "INSERT INTO $tblname(`COST4`, `CONT4`, `FTR`, `ACC`, `SNOO3`, `SPOO3`,`1991001`, `1991002`,    `1991003`,    `1991004`,    `1991005`,    `1991006`,    `1991007`,    `1991008`,    `1991009`,    `1991010`,    `1991011`,    `1991012`,    `1991101`,    `1991102`,    `1991103`,    `1991104`,    `1991105`,    `1991106`,    `1991107`,    `1991108`,    `1991109`,    `1991110`,    `1991111`,    `1991112`,    `1991201`,    `1991202`,    `1991203`,    `1991204`,    `1991205`,    `1991206`,    `1991207`,    `1991208`,    `1991209`,    `1991210`,    `1991211`,    `1991212`,    `1991301`,    `1991302`,    `1991303`,    `1991304`,    `1991305`,    `1991306`,    `1991307`,    `1991308`,    `1991309`,    `1991310`,    `1991311`,    `1991312`,    `1991401`,    `1991402`,    `1991403`,    `1991404`,    `1991405`,    `1991406`,    `1991407`,    `1991408`,    `1991409`,    `1991410`,    `1991411`,    `1991412`,    `1991501`,    `1991502`,    `1991503`,    `1991504`,    `1991505`,    `1991506`,    `1991507`,    `1991508`,    `1991509`,    `1991510`,    `1991511`,    `1991512`,    `1991601`,    `1991602`,    `1991603`,    `1991604`,    `1991605`,    `1991606`,    `1991607`,    `1991608`,    `1991609`,    `1991610`,    `1991611`,    `1991612`,    `1991701`,    `1991702`,    `1991703`,    `1991704`,    `1991705`,    `1991706`,    `1991707`,    `1991708`,    `1991709`,    `1991710`,    `1991711`,    `1991712`,    `1991801`,    `1991802`,    `1991803`,`    `1991804`,    `1991805`,    `1991806`,    `1991807`,    `1991808`,    `1991809`,    `1991810`,    `1991811`,    `1991812`,    `1991901`,    `1991902`,    `1991903`,    `1991904`,    `1991905`,    `1991906`,    `1991907`,    `1991908`,    `1991909`,    `1991910`,    `1991911`,    `1991912`,    `1992001`,    `1992002`,    `1992003`,    `1992004`,    `1992005`,    `1992006`,    `1992007`,    `1992008`,    `1992009`,    `1992010`,    `1992011`,    `1992012`)VALUES ('$COST4', '$CONT4', '$FTR','$ACC','$SNOO3','$pp','$stack[0]','$stack[1]','$stack[2]','$stack[3]','$stack[4]','$stack[5]','$stack[6]','$stack[7]','$stack[8]','$stack[9]','$stack[10]','$stack[11]','$stack[12]','$stack[13]','$stack[14]','$stack[15]','$stack[16]','$stack[17]','$stack[18]','$stack[19]','$stack[20]','$stack[21]','$stack[22]','$stack[23]','$stack[24]','$stack[25]','$stack[26]','$stack[27]','$stack[28]','$stack[29]','$stack[30]','$stack[31]','$stack[32]','$stack[33]','$stack[34]','$stack[35]','$stack[36]','$stack[37]','$stack[38]','$stack[39]','$stack[40]','$stack[41]','$stack[42]','$stack[43]','$stack[44]','$stack[45]','$stack[46]','$stack[47]','$stack[48]','$stack[49]','$stack[50]','$stack[51]','$stack[52]','$stack[53]','$stack[54]','$stack[55]','$stack[56]','$stack[57]','$stack[58]','$stack[59]','$stack[60]','$stack[61]','$stack[62]','$stack[63]','$stack[64]','$stack[65]','$stack[66]','$stack[67]','$stack[68]','$stack[69]','$stack[70]','$stack[71]','$stack[72]','$stack[73]','$stack[74]','$stack[75]','$stack[76]','$stack[77]','$stack[78]','$stack[79]','$stack[80]','$stack[81]','$stack[82]','$stack[83]','$stack[84]','$stack[85]','$stack[86]','$stack[87]','$stack[88]','$stack[89]','$stack[90]','$stack[91]','$stack[92]','$stack[93]','$stack[94]','$stack[95]','$stack[96]','$stack[97]','$stack[98]','$stack[99]','$stack[100]','$stack[101]','$stack[102]','$stack[103]','$stack[104]','$stack[105]','$stack[106]','$stack[107]','$stack[108]','$stack[109]','$stack[110]','$stack[111]','$stack[112]','$stack[113]','$stack[114]','$stack[115]','$stack[116]','$stack[117]','$stack[118]','$stack[119]','$stack[120]','$stack[121]','$stack[122]','$stack[123]','$stack[124]','$stack[125]','$stack[126]','$stack[127]','$stack[128]','$stack[129]','$stack[130]','$stack[131]')";
                            $conn->query($sql);
                        }
                        mysqli_free_result($dis_value_result);
                }
        }

   }

在此我已在表格中插入多行。将记录插入表中需要4小时。表中插入的记录仅为45000行。

逐行插入记录。在这一点上,我缺乏,发送超过100条记录减少时间或减少时间的方法。

0 个答案:

没有答案