设为首页 收藏本站
查看: 631|回复: 0

[经验分享] SPEEDING UP SQLITE INSERT OPERATIONS

[复制链接]

尚未签到

发表于 2016-11-29 11:16:25 | 显示全部楼层 |阅读模式
I’ve been working on an Android application recently which uses an SQLite database to store and search about 6000 rows of data, and the data is updated from a webservice periodically. It would seem like a fair assumption to think the retrieval of data from the webservice would be the slowest part of the operation, however – this turned out not to be the case.

On the emulator, the actual retrieval and parsing of the data from a CSV-format took about 20 seconds, while inserting the data to the database took 71 seconds.

Since this was an operation that only ran about once a week, I felt a minute and a half was an acceptable amount of time since it would run in the background anyway. When I launched the application to my Android device and ran the initial synchronization, the 6000 inserts took a shocking 478 seconds – almost 8 minutes!

I found this strange – most operations usually run a little slower on the emulator than on an actual device, and this was on the Samsung Galaxy S – one of the faster Android devices. This being my first real use of SQLite, I figure the next step was to try and optimize my inserts – compiled statements was next in line:


String sql = "INSERT INTO table (number, nick) VALUES (?, ?)";
SQLiteStatement stmt = db.compileStatement(sql);
for (int i = 0; i < values.size(); i++) {
    stmt.bindString(1, values.get(i).number);
    stmt.bindString(2, values.get(i).nick);
    stmt.execute();
    stmt.clearBindings();
}

This brought the total time of the insertion on the emulator down from 71 to 56 seconds. Testing the same operation on the device resulted in a total insert time of 487 seconds, 9 seconds slower than before. I ran the test again and ended up with a similar result – I was clearly not on the right track here.

Digging further, I found a documentation page for SQLite telling me that “PRAGMA synchronous = OFF” would tell SQLite to continue without syncing as soon as it has handed data off to the operating system. This resulted in an insertion time of 50 seconds, compared to the 71 seconds we started with. Testing this on the device resulted in encouraging results; 361 seconds – almost two minutes faster than what we started with.

Feeling I was on to the right track and suspecting the filesystem was slowing down the inserts, I found various references around the net mentioning problems with the performance of the I/O on the Samsung Galaxy S – all related to the RFS file system. What this means for our SQLite database is that it will perform an fsync at every insert to make sure it’s been written to disk…

Reading up on SQLite, I discovered that with transactions I could keep the data in memory and commit the changes to filesystem with one operation instead of 6000:

String sql = "INSERT INTO table (number, nick) VALUES (?, ?)";
db.beginTransaction();

SQLiteStatement stmt = db.compileStatement(sql);
for (int i = 0; i < values.size(); i++) {
    stmt.bindString(1, values.get(i).number);
    stmt.bindString(2, values.get(i).nick);
    stmt.execute();
    stmt.clearBindings();
}

db.setTransactionSuccessful();
db.endTransaction();

Combining transactions with compiled statements yielded a tremendous performance boost: From the original 71 seconds on the emulator down to an incredible 5 seconds!
The performance on the Galaxy was even more shocking: from the original 478 seconds down to 1.5 seconds!

I had to rerun the benchmarks and verify that all the data were actually being inserted – but luckily I was able to reproduce the results.

Transactions are useful for multiple reasons:

When performing inserts to multiple tables and you want to make sure that either everything or nothing gets inserted
When doing a batch insert of multiple rows (like the example above shows)
Conclusions:

Unless you’re doing a single insert or it is critical that rows gets written to disk right away, use transactions.
Make sure to test your application on an actual device, preferably more than one.
The performance numbers above apply to the Samsung Galaxy S – a different brand or model might give a smaller performance gain

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-307183-1-1.html 上篇帖子: Android的SQLite使用实例 下篇帖子: Sqlite数据库sqlite3命令小记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表