sqlite3 批量insert的性能优
1、传统方式导入,一万条数据的导入(46s)
$sqlite_db = new PDO('sqlite:sqlitedata.db', 'Gongz', 'gongz', []);
$start = time();
$n = 0;
while ($n < 100000) {
$n ++;
$sql = "INSERT INTO `result_data` (`buguid`, `buname`, `签约金额_万`, `认购金额_万`, `认购套数_万`, `date`, `number1`, `number2`, `rp_buname_95938`, `rp_type_95938`, `rp_parentguid_95938`, `rp_levelcode_95938`) VALUES ('871709c2-083c-e511-80c0-00155d0a6f23', '名称', '31', '547', '776', '2015-12-07 00:00:00', '123.7890', '456.5678', '名称', '4', 'e64c7c15-7ac9-406c-9969-d41aeebf3a09', 'a.aa.001.01')";
$sqlite_db->exec($sql);
}
echo time() - $start;
2、传统方式导入,关闭写如磁盘的检查,一万条数据的导入(10s)
$sqlite_db = new PDO('sqlite:sqlitedata.db', 'gongz', 'gongz', []);
$start = time();
$n = 0;
$sqlite_db->exec('PRAGMA synchronous = OFF;');
while ($n < 10000) {
$n ++;
$sql = "INSERT INTO `result_data` (`buguid`, `buname`, `签约金额_万`, `认购金额_万`, `认购套数_万`, `date`, `number1`, `number2`, `rp_buname_95938`, `rp_type_95938`, `rp_parentguid_95938`, `rp_levelcode_95938`) VALUES ('871709c2-083c-e511-80c0-00155d0a6f23', '名称', '31', '547', '776', '2015-12-07 00:00:00', '123.7890', '456.5678', '名称', '4', 'e64c7c15-7ac9-406c-9969-d41aeebf3a09', 'a.aa.001.01')";
$sqlite_db->exec($sql);
}
echo time() - $start;
3、开启事务,一万条数据的导入(0s)
$sqlite_db = new PDO('sqlite:sqlitedata.db', 'gongz', 'gongz', []);
$start = time();
$n = 0;
$sqlite_db->beginTransaction();
while ($n < 10000) {
$n ++;
$sql = "INSERT INTO `result_data` (`buguid`, `buname`, `签约金额_万`, `认购金额_万`, `认购套数_万`, `date`, `number1`, `number2`, `rp_buname_95938`, `rp_type_95938`, `rp_parentguid_95938`, `rp_levelcode_95938`) VALUES ('871709c2-083c-e511-80c0-00155d0a6f23', '名称', '31', '547', '776', '2015-12-07 00:00:00', '123.7890', '456.5678', '名称', '4', 'e64c7c15-7ac9-406c-9969-d41aeebf3a09', 'a.aa.001.01')";
$sqlite_db->exec($sql);
}
$sqlite_db->commit();
echo time() - $start;
另外关于sqlite主键id的小技巧:
在创建表的时候,可以不用指定主键,sqlite默认会有一个隐藏的rowid字段存在,只需要在select的时候在字段中将rowid as成一个字段名称就可以使用了
相关文章