ORMLite的createOrUpdate似乎很慢-正常速度是多少? [英] ORMLite's createOrUpdate seems slow - what is normal speed?

查看:212
本文介绍了ORMLite的createOrUpdate似乎很慢-正常速度是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中调用ORMLite RuntimeExceptionDaocreateOrUpdate(...)方法非常慢.

Calling the ORMLite RuntimeExceptionDao's createOrUpdate(...) method in my app is very slow.

我有一个非常简单的对象(Item),具有2个整数(一个是generatedId),一个String和一个double.我使用以下代码测试(大约)更新数据库中的对象所需的时间(大约100次).日志语句记录:

I have a very simple object (Item) with a 2 ints (one is the generatedId), a String and a double. I test the time it takes (roughly) to update the object in the database (a 100 times) with the code below. The log statement logs:

更新1行100次的时间:3069

time to update 1 row 100 times: 3069

为什么在只有1行的表中,要更新一次对象100次需要3秒.这是正常的ORMLite速度吗?如果没有,可能是什么问题?

Why does it take 3 seconds to update an object 100 times, in a table with only 1 row. Is this the normal ORMLite speed? If not, what might be the problem?

RuntimeExceptionDao<Item, Integer> dao =
    DatabaseManager.getInstance().getHelper().getReadingStateDao();
Item item = new Item();
long start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
    item.setViewMode(i);
    dao.createOrUpdate(item);
}
long update = System.currentTimeMillis();
Log.v(TAG, "time to update 1 row 100 times: " + (update - start));

如果我创建100个新行,则速度甚至会更慢.

If I create 100 new rows then the speed is even slower.

注意:我已经在使用ormlite_config.txt.它记录"Loaded configuration for class ...Item",所以这不是问题.

Note: I am already using ormlite_config.txt. It logs "Loaded configuration for class ...Item" so this is not the problem.

谢谢.

推荐答案

不幸的是,这可能是预期"的速度.确保您使用的是ORMLite 4.39或更高版本. createOrUpdate(...)正在使用一种更昂贵的方法来预先测试数据库中对象的存在.但是我怀疑这将是最小的速度改进.

This may be the "expected" speed unfortunately. Make sure you are using ORMLite version 4.39 or higher. createOrUpdate(...) was using a more expensive method to test for existing of the object in the database beforehand. But I suspect this is going to be a minimal speed improvement.

如果我创建100个新行,则速度甚至会更慢.

If I create 100 new rows then the speed is even slower.

默认情况下,Sqlite处于自动提交模式.尝试做的一件事是使用 ORMLite Dao.callBatchTasks(...)方法包装您的插入内容(或您的createOrUpdate).

By default Sqlite is in auto-commit mode. One thing to try is to wrap your inserts (or your createOrUpdates) using the the ORMLite Dao.callBatchTasks(...) method.

通过

In by BulkInsertsTest android unit test, the following doInserts(...) method inserts 1000 items. When I just call it:

doInserts(dao);

在我的模拟器中需要7.3秒.如果我使用callBatchTasks(...)方法进行调用,该方法将事务封装在Android Sqlite中的调用周围:

It takes 7.3 seconds in my emulator. If I call using the callBatchTasks(...) method which wraps a transactions around the call in Android Sqlite:

dao.callBatchTasks(new Callable<Void>() {
    public Void call() throws Exception {
        doInserts(dao);
        return null;
    }
});

需要1.6秒.使用dao.setSavePoint(...)方法可以达到相同的性能.这会启动一个事务,但不如callBachTasks(...)方法那么好,因为您必须确保关闭自己的事务:

It takes 1.6 seconds. The same performance can be had by using the dao.setSavePoint(...) method. This starts a transaction but is not as good as the callBachTasks(...) method because you have to make sure you close your own transaction:

DatabaseConnection conn = dao.startThreadConnection();
Savepoint savePoint = null;
try {
    savePoint = conn.setSavePoint(null);
    doInserts(dao);
} finally {
    // commit at the end
    conn.commit(savePoint);
    dao.endThreadConnection(conn);
}

这也需要大约1.7秒.

This also takes ~1.7 seconds.

这篇关于ORMLite的createOrUpdate似乎很慢-正常速度是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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