douhan1992 2015-08-08 22:28 采纳率: 100%
浏览 109

sqlite + php - 使用循环进行事务的权利和最快方式

I'm a beginner in php/sql (6 months), and I noticed - transactions are faster than pure "insert into".

When I operate on huge amounts of data (with range: 10-500k inserts), I noticed my script is slow.

The goal: I wanna do the fastest way to save data into sqlite *.db file.

My script looks like that:

$array = array(
    'ronaldo' => 'gay' , 
    'mario basler' => 'cool guy'
);

$db = new Sqlite3('file.db')
$db->query('BEGIN;');

foreach($array as $kee => $val){
    $db->query("insert into table('name' , 'personality') values('$k' , '$v')");
}

$db->query("COMMIT");

Is that way is wrong?

  • 写回答

1条回答 默认 最新

  • doumin4553 2015-08-08 22:35
    关注

    What you do is absolutely correct. It will speed up your interaction with the database. Any command that changes the database will automatically start a transaction if one is not already in effect.

    So, if you do many inserts without starting a transaction explicitly, for each operation a transaction will be created. You create 1 transaction and do all the operations in bulk.

    How to insert 40000 records fast into an sqlite database in an iPad

    https://www.sqlite.org/lang_transaction.html

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器