Fra*_*ank 9 performance android ormlite
在我的应用程序中调用ORMLite RuntimeExceptionDao的createOrUpdate(...)方法非常慢.
我有一个非常简单的object(Item),有2个int(一个是generatedId),a String和a double.我用下面的代码测试(大致)更新数据库中的对象所需的时间(100次).日志语句记录:
时间更新1行100次:3069
为什么在只有1行的表中更新对象需要3秒才能更新100次.这是正常的ORMLite速度吗?如果没有,可能是什么问题?
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));
Run Code Online (Sandbox Code Playgroud)
如果我创建100个新行,那么速度会更慢.
注意:我已经在使用了ormlite_config.txt.它记录"Loaded configuration for class ...Item"所以这不是问题.
谢谢.
Gra*_*ray 24
不幸的是,这可能是"预期的"速度.确保使用的是ORMLite 4.39或更高版本. createOrUpdate(...)正在使用更昂贵的方法预先测试数据库中现有的对象.但我怀疑这将是一个最小的速度提升.
如果我创建100个新行,那么速度会更慢.
默认情况下,Sqlite处于自动提交模式.要尝试的一件事是createOrUpdate使用ORMLite Dao.callBatchTasks(...)方法包装您的插入(或您的s).
在BulkInsertsTest android单元测试中,以下doInserts(...)方法插入1000个项目.当我打电话给它时:
doInserts(dao);
Run Code Online (Sandbox Code Playgroud)
我的模拟器需要7.3秒.如果我使用callBatchTasks(...)在Android Sqlite中围绕调用包装事务的方法调用:
dao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
doInserts(dao);
return null;
}
});
Run Code Online (Sandbox Code Playgroud)
需要1.6秒.使用该dao.setSavePoint(...)方法可以获得相同的性能.这会启动一个事务,但不如该callBachTasks(...)方法好,因为您必须确保关闭自己的事务:
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);
}
Run Code Online (Sandbox Code Playgroud)
这也需要约1.7秒.