如何在没有事务的情况下提高PDO SQLite插入


How to boost PDO SQLite inserts without transactions?

有以下模拟INSERT查询,这似乎是非常慢!

$sqlite = new 'PDO();
for($i=0;$i<100;$i++) {
  $sqlite->prepare('INSERT INTO test (`id`, `data1`, `data2`) VALUES (:id, :data1, :data2);');
  $sqlite->execute(array(
    ':id' => NULL,
    ':data1' => 'some string',
    ':data2' = 'some string'
    // And more
  ));
}

执行100个insert in 23.261 secs (4.3 q/s)

设置pragma synchronous = off;

$sqlite->exec("pragma synchronous = off;");

在0.037 secs (2704.7 q/s)内执行100个insert

  • pragma synchronous = off可能有哪些副作用?
  • 据我所知,瓶颈是fsync,对吧?
  • 有没有其他的方法来加速SQLite,而不使用事务查询?

文档记录了副作用:

使用synchronous OFF (0), SQLite在将数据传递给操作系统后立即继续不同步。如果运行SQLite的应用程序崩溃,数据将是安全的,但是如果操作系统崩溃或计算机在数据被写入磁盘表面之前断电,则数据库可能会损坏。另一方面,使用同步OFF可以将提交速度提高几个数量级。

使用单个事务是快速插入许多行的唯一安全方法。