我有大约14000行逗号分隔值,我试图使用PHP PDO插入到sqlite表中,如下所示:
<?php
// create a PDO object
$dbh = new PDO('sqlite:mydb.sdb');
$lines = file('/csv/file.txt'); // import lines as array
foreach ($lines as $line) {
$line_array = (','$line); // create an array of comma-separated values in each line
$values = '';
foreach ($line_array as $l) {
$values .= "'$l', ";
}
substr($values,-2,0); // get rid of the last comma and whitespace
$query = "insert into sqlite_table values ($values)"; // plug the value into a query statement
$dbh->query($query); // run the query
}
?>
Run Code Online (Sandbox Code Playgroud)
这个查询需要很长时间,并且在没有中断的情况下运行它,我将不得不使用PHP-CLI.有更好(更快)的方法吗?
meo*_*ouw 22
通过将插入包装在单个事务中,您将看到良好的性能提升.如果不这样做,SQLite会将每个插入视为自己的事务.
<?php
// create a PDO object
$dbh = new PDO('sqlite:mydb.sdb');
// Start transaction
$dbh->beginTransaction();
$lines = file('/csv/file.txt'); // import lines as array
foreach ($lines as $line) {
$line_array = (','$line); // create an array of comma-separated values in each line
$values = '';
foreach ($line_array as $l) {
$values .= "'$l', ";
}
substr($values,-2,0); // get rid of the last comma and whitespace
$query = "insert into sqlite_table values ($values)"; // plug the value into a query statement
$dbh->query($query); // run the query
}
// commit transaction
$dbh->commit();
?>
Run Code Online (Sandbox Code Playgroud)