Large number of SQLite inserts using PHP

前端 未结 4 1233
无人共我
无人共我 2021-01-13 01:54

I have about 14000 rows of comma separated values that I am trying to insert into a sqlite table using PHP PDO, like so:



        
4条回答
  •  礼貌的吻别
    2021-01-13 02:21

    If you're looking for a bit more speed, use prepare/fetch, so the SQL engine doesn't have to parse out the text string each time.

    $name = $age = '';
    $insert_stmt = $db->prepare("insert into table (name, age) values (:name, :age)");
    $insert_stmt->bindValue(':name', $name);
    $insert_stmt->bindValue(':age', $age);
    
    // do your loop here, like fgetcsv
    while (get the data) {
    list($name, $age) = split(',', $string);
    $insert_stmt->execute();
    }
    

    It's counter-intuitive that you do the binding outside the loop, but this is one reason why this method is so fast, you're basically saying "Execute this pre-compiled query using data from these variables". So it doesn't even need to move the data around internally. And you want to avoid re-parsing the query, which is the problem if you use something like "insert into table (name) values ('$name')", every query sends the entire text string to the database to be re-parsed.

    One more thing to speed it up -- wrap the whole loop in a transaction, then commit the transaction when the loop is finished.

提交回复
热议问题