一次性向MySQL資料庫中插入6000條資料 測試代碼

來源:互聯網
上載者:User

之前遇到個問題,需要將一個檔案夾中的所有檔案名稱取出並 存入到資料庫中。

採用php指令碼測試,讀取一個檔案名稱,就立即儲存到資料庫中,結果花了我230多秒的時間,速度實在太慢!

原因分析:insert 語句被執行了6000次,導致速度特別慢!

改進方法:1、用php指令碼 將 所有的檔案名稱都存入到一個文字檔中filename.txt(一個檔案名稱佔一行),實測指令碼執行時間 1.5262秒。

2、用phpfgets()函數取出filename.txt中的每一行資料,儲存到buffer數組中,將buffer數組寫入到一條insert語句中,

執行一次insert語句,實測指令碼執行時間 0.0629秒。

代碼1如下:

<?php
$st=microtime_float();//自訂函數,在代碼2中可見。
      $cout=0;$dir="e:/pydot";$talbe="pydot2";
              if(is_dir($dir)){
                    if($dh=opendir($dir)) {
                            while(($file=readdir($dh)) !== false){
                                  if($file!="."&&$file!=".."){
                                  $file=mb_convert_encoding($file,'UTF-8','EUC-CN');
                                                                                                 
                                  //$sql="insertinto ".$talbe."(name) values('".$file."');";
                                 
                                  //$m->execute($sql);
                                  $filename="test/filename.txt";
                                  if(!$handle = fopen($filename, 'a')) {
                                    echo "不能開啟檔案$filename";
                                    exit;
                                  }
                            //將$file寫入到我們開啟的檔案中。
                                  fputs($handle,$file."\n");
                                  $cout++;
                                  //echo$cout,":",$file."<br>";
                                  echo$file."<br>";
                                  }
                            }
                    closedir($dh);
                    }
              }
      $et=microtime_float();
      $t=$et-$st;
      echo"insert into talbe ",$talbe,"",$cout,"times;\n<br>";
      echo"Total time $t seconds.\n";
?>

代碼2如下:

<?php   
      $server='localhost';
      $user='root';
      $passwd='root';
      $port='3306';
      $dbname='dbname';
      $talbe="tablename";
      $c=0;$cc=0;
      $st=microtime_float();
      $link=mysql_connect($server,$user,$passwd);
      if(!$link) {
              die('Couldnot connect: ' . mysql_error());
      }
      else echo 'Connected successfully\n<br>';
      mysql_select_db("catx",$link);
      $handle= @fopen("sq.txt", "r");
     
      while((fgets($handle, 256)) !== false) {
              $buffer[$c]= fgets($handle, 256);
              $c++;
              if(0==$c%5978){
                    $c=0;$start_c="('";$end_c="');";
                    $va="";
                    $kh="'),('";//括弧
                        for($i=0;$i<5978;$i++){
                        $va=$va+$buffer[$i].$kh;
                          }
                    $sql="insertinto tablename(name) values".$start_c.$va.$end_c;
                    mysql_query($sql);
              }
              $cc++;
      }     
      $et=microtime_float();
      $t=$et-$st;
      echo"Totaltime $t seconds.\n<br>";
      echo"insert into talbe ",$talbe,"",$cc,"times;\n<br>";
     
      functionmicrotime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.