dua27031 2017-06-06 13:41
浏览 76
已采纳

有没有更快的方法使用PHP将数据从文件导入MySQL?

Okay, so I get around 100k-1M lines of text that I always import to a database. The code that i use is as follows:

$lines = new SplFileObject('/home/file.txt');
while(!$lines->eof()) {
    $lines->next();       //Skipping first line
    $row = explode(',',$lines);
    for($i = 0; $i<4; $i++){
        if(!isset($row[$i])){
            $row[$i] = null;
        }
    }
    $y = (float) $row[1];
    $z = (float) $row[2];
    $load_query = "INSERT IGNORE INTO new (datetime_gmt,field2,field3)
    VALUES ('".$row[0]."','".$y."','".$z."');";

    if(!$mysqli->query($load_query)){
      die("CANNOT EXECUTE".$mysqli->error."
");
    }
}
$lines = null;

However, it takes waaayyy too long. Is there any faster way to do it, or am I stuck with this method?

PS. I don't want to use MySQL's "INSERT DATA INFILE".

  • 写回答

1条回答 默认 最新

  • dongyi2159 2017-06-06 14:51
    关注

    As written, you're running an insert statement for every line. It'll be much faster if you compile a single multi-insert statement in the format of INSERT INTO table (foo, bar) VALUES (1, 2), (3, 4), (5, 6); that is executed once at the end. Something along the lines of this, though it could be cleaned up more.

    $lines = new SplFileObject('/home/file.txt');
    $load_query = "INSERT IGNORE INTO new (datetime_gmt,field2,field3)
        VALUES ";
    while(!$lines->eof()) {
        $lines->next();       //Skipping first line
        $row = explode(',',$lines);
        for($i = 0; $i<4; $i++){
            if(!isset($row[$i])){
                $row[$i] = null;
            }
        }
        $y = (float) $row[1];
        $z = (float) $row[2];
        $load_query .= "('".$row[0]."','".$y."','".$z."'),";
    }
    
    if(!$mysqli->query(rtrim($load_query, ','))) {
        die("CANNOT EXECUTE".$mysqli->error."
    ");
    }
    $lines = null;
    

    Also keep make sure the data is trusted. If the file can come from an outside user, appending directly to the query string creates an SQL injection vector.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据