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

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

问题描述

我需要解析一个相当大的XML文件(大约一百千字节和几百千字节之间变化),我使用 Xml#parse(String,ContentHandler)

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)。所有这一切一起需要大约80秒的152KB测试文件(这大约插入了200行)。

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?

推荐答案

您应该进行批处理插入。

You should do batch inserts.

伪代码:

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天全站免登陆