Android SQLite 数据库:插入缓慢 [英] Android SQLite database: slow insertion

查看:47
本文介绍了Android SQLite 数据库:插入缓慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析一个相当大的 XML 文件(大约在一百千字节到几百千字节之间变化),我正在使用 Xml#parse(String, ContentHandler) 进行解析.我目前正在用 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().insert(TABLE_NAME, "_id", values).对于 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).

当我注释掉所有插入语句(但保留其他所有内容,例如创建 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?

推荐答案

你应该批量插入.

伪代码:

db.beginTransaction();
for (entry : listOfEntries) {
    db.insert(entry);
}
db.setTransactionSuccessful();
db.endTransaction();

这极大地提高了我应用中的插入速度.

That increased the speed of inserts in my apps extremely.

更新:
@Yuku 提供了一篇非常有趣的博文:Android 使用 inserthelper 更快地插入 sqlite 数据库

这篇关于Android SQLite 数据库:插入缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆