如何在Android应用程序中加快数据库(SQLite)中的大数据插入 [英] How to speed up large data insertion in Database (SQLite) in Android application

查看:52
本文介绍了如何在Android应用程序中加快数据库(SQLite)中的大数据插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须在数据库中插入数据,而数据非常大,位于ArrayList 中.数据插入循环如下:

I have a situation where I have to insert data in the database, and the data is very large which is in an ArrayList. The data is inserted in a loop like this:

for( ..... )
ContentValues cv = new ContentValues();

// cv.put statements

sqLiteDB.insert ......

现在在测试用例中大约需要 1 多分钟,如果有真实数据,预计会超过 10 分钟,而目标是将时间保持在 30 秒以下.数据不能减少.我的问题是:

Now it takes around more than 1 minute in the test case and is expected to exceed 10 minutes with real data, while the target is to keep the time below 30 seconds. Data cannot be reduced. My questions are:

  1. 如何提高速度
  2. 有没有办法避免重复调用 sqLiteDB.insert 函数并一次性插入所有行?

推荐答案

在您的案例中可能提高的速度之一是使用如下代码:

One of the possible speed improvement in your case is to have a code like this:

ContentValues cv = new ContentValues();
for(....) {
    //cv put
    sqLiteDB.insert
    cv.clear();
}

此外,这里 是一篇如何提高插入速度的博客文章.

Moreover, here is a blog post how to improve insertion speed.

编辑我还检查了 SO,here 是一个类似的问题.据称使用交易提高了速度.

EDIT I also checked SO and here is a similar question. It's claimed that the use of transactions improves the speed.

这篇关于如何在Android应用程序中加快数据库(SQLite)中的大数据插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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