Android sqlite database setlocale is too slow while opening db. How to solve?

核能气质少年 提交于 2020-01-23 02:05:52

问题


I use the following method for reading/writing db:

  1. Database is located at /data/data/{packagename}/databases/Database.db Since the database is greater than 3Mb we found a specific solution to have it copied there and to have it populated with appropriate data.

  2. Following is the class implementing the task to get the opened database. This class is a singleton.

    public class DatabaseHelper extends SQLiteOpenHelper
    
  3. to open the database we use the following method:

    SQLiteDatabase db = DatabaseHelper.getInsance().getReadableDatabase();
    
  4. Then rawquery is used for querying the db:

    Cursor cursor = db.rawQuery(query, null);
    
  5. Then best fitting to our purposes we fetch the database data into memory in different resultset instances:

    while (cursor.moveToNext()) {
        ResultSet rs = new ResultSet();
        rs.setThis(cursor.getInt(0));
        rs.setThat(cursor.getString(1));
        // and so on.. this is just an example
        ResultList.add(rs);
    }
    
  6. Finally:

    cursor.close();
    db.close();
    
  7. Let mention, if necessary, transaction is used also, but using transaction didn't lead to speed-up.

For every single query the pattern above is (quite) followed. But unfortunately this solution seems very slow. So some method profiling is made and it came to clear, that sqlite setlocale is always run at getReadableDatabase() (which is created! don't forget) and that method takes the most of the time. Meanly 40% alone..

Please advice how to solve this problem! Or maybe please offer an other pattern to satisfy our needs!

Thanks in advance


回答1:


Szia!

Funniest thing is, native_setLocale (which is causing the slow DB open) apparently doesn't even work: http://code.google.com/p/android/issues/detail?id=2625




回答2:


6) Finally:

cursor.close();
db.close();

It's not possible to keep the database open between queries?




回答3:


As with the question posed here (SQLCipher for Android getReadableDatabase() Overherad) the performance issue you are seeing is likely due to SQLCipher key derivation. Performance for opening a database is deliberately slow due to key derivation. You should cache the database connection so that it can be used multiple times without having to open and key the database repeatedly. If this is possible, opening the database once during startup is the preferred course of action. Subsequent access on the same database handle will not trigger key derivation, so performance will be much faster.



来源:https://stackoverflow.com/questions/5230769/android-sqlite-database-setlocale-is-too-slow-while-opening-db-how-to-solve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!