Android的SQLite数据库:缓慢插入 [英] Android SQLite database: slow insertion
问题描述
我需要解析一个相当大的XML文件,我使用 XML#解析(字符串的ContentHandler)$ C $正在做(大约几百KB几百千字节之间变化) C>。我目前正在测试这种具有152KB的文件。
I need to parse a fairly large XML file (varying between about a hundred kilobytes and several hundred kilobytes), which I'm doing using Xml#parse(String, ContentHandler)
. I'm currently testing this with a 152KB file.
在分析时,我也用电话与以下类似插入一个SQLite数据库中的数据: getWritableDatabase()插入(TABLE_NAME,_id,值)
。所有这一切都为152KB的测试文件(这可以归结为插入大约200行)在一起大约需要80秒。
During parsing, I also insert the data in an SQLite database using calls similar to the following: getWritableDatabase().insert(TABLE_NAME, "_id", values)
. All of this together takes about 80 seconds for the 152KB test file (which comes down to inserting roughly 200 rows).
当我注释掉所有的INSERT语句(但留下的一切,如创建 ContentValues
等)相同文件占用只有23秒。
When I comment out all insert statements (but leave in everything else, such as creating ContentValues
etc.) the same file takes only 23 seconds.
这是正常的数据库操作有这么大的开销?我可以做什么的?
Is it normal for the database operations to have such a big overhead? Can I do anything about that?
推荐答案
您应该做批量插入。
伪code:
db.beginTransaction();
for (entry : listOfEntries) {
db.insert(entry);
}
db.setTransactionSuccessful();
db.endTransaction();
这增加了插入的速度在我的应用程序极了。
That increased the speed of inserts in my apps extremely.
更新:
@Yuku提供了一个非常有趣的博客文章:<一个href="http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/">Android使用inserthelper更快地插入到SQLite数据库
Update:
@Yuku provided a very interesting blog post: Android using inserthelper for faster insertions into sqlite database
这篇关于Android的SQLite数据库:缓慢插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!