Ormlite Android的批量插入 [英] Ormlite Android bulk inserts

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

问题描述

任何人都可以解释为什么我的镶片在Ormlite要花这么长时间?在桌面上的一个sqlite的交易做1700刀片需要不到一秒钟。但是,使用Ormlite为Android时,它采取大约70秒,我可以看到每个插入的调试信息。

当我尝试换行插入到它会在完全相同的速度一个事务。据我所知,有两个开销为Android和Ormlite,但是,我不希望它是很大。我的code是如下:

  this.db =新DatabaseHelper(getApplicationContext());
    道= db.getAddressDao();
最终的BufferedReader读者=新的BufferedReader(新的InputStreamReader(getResources()openRawResource(R.raw.poi)));
    尝试{
        dao.callBatchTasks(新赎回<无效>(){
            公共无效()调用抛出异常{
                串线;
                而((行= reader.readLine())!= NULL){
                    串[]栏= line.split(,);
                    地址地址=新地址();
                    //设置地址
                    dao.create(地址);
                }
            返回null;
         }
        });
    }赶上(的SQLException E){
        e.printStackTrace();
    }赶上(例外五){
        e.printStackTrace();
    }


解决方案

不幸的是,这可能是预期。我得到类似的性能,当我做这个数字插入我的模拟器下也是如此。批处理任务,并关闭自动提交似乎并没有帮助。

如果你正在寻找一个大量数据加载到数据库中,你可能会考虑重播数据库转储来代替。在这里看到:


  

的Andr​​oid OrmLite pre-填充数据库


can anyone explain why my inserts are taking so long in Ormlite? Doing 1,700 inserts in one sqlite transaction on the desktop takes less than a second. However, when using Ormlite for Android, it's taking about 70 seconds, and I can see each insert in the debugging messages.

When I try and wrap the inserts into one transaction it goes at exactly the same speed. I understand that there is overhead both for Android and for Ormlite, however, I wouldn't expect it to be that great. My code is below:

    this.db = new DatabaseHelper(getApplicationContext());
    dao = db.getAddressDao();
final BufferedReader reader = new BufferedReader(new InputStreamReader(getResources().openRawResource(R.raw.poi)));
    try {
        dao.callBatchTasks(new Callable<Void>() {
            public Void call() throws Exception {
                String line;
                while ((line = reader.readLine()) != null) {
                    String[] columns = line.split(",");
                    Address address = new Address();
                    // setup Address
                    dao.create(address);
                } 
            return null;
         }
        });
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

解决方案

Unfortunately, this may be "expected". I get similar performance when I do that number of inserts under my emulator as well. The batch-tasks and turning off auto-commit don't seem to help.

If you are looking to load a large amount of data into a database, you might consider replaying a database dump instead. See here:

Android OrmLite pre-populate database

这篇关于Ormlite Android的批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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