SQLite查询在Android上确实很慢

时间:2011-08-25 21:14:42

标签: java android sqlite

我有一个非常大的数据库,我需要查询以获取一些数据并在Android上的ListView中显示。数据库大约5MB,存储在SD卡上。它在2个表中有6万条记录。问题是查询数据库以从一个特定列获取所有记录需要非常长的时间 - 就像模拟器和我的手机上的几分钟一样。我已经尝试了一切 - 将这些数据存储在平面文件中,xml - sqlite是我最后的希望。这是我用来打开数据库并查询它的类:

    public class DataHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "TableName";
    private Context context;
    private SQLiteDatabase db;
    private SQLiteStatement insertStmt;

    public DataHelper(Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);
        // this.db = openHelper.getReadableDatabase();

        this.db = SQLiteDatabase.openDatabase(
                Environment.getExternalStorageDirectory()
                        + "/myDB.db", null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        // this.insertStmt = this.db.compileStatement(INSERT);
    }

    public void deleteAll() {
        this.db.delete(TABLE_NAME, null, null);
    }

    public List<String> selectBrands() {
        List<String> list = new ArrayList<String>();
        Cursor cursor = this.db.query(TABLE_NAME, new String[] { "ColumnName" },
                null, null, null, null, null); 
 }
        if (cursor.moveToFirst()) {
            do {
                if (!(list.contains(cursor.getString(0)))) {
                    list.add(cursor.getString(0));

                }

            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return list;
    }

    private static class OpenHelper extends SQLiteOpenHelper {
        OpenHelper(Context context) {
            super(context, null, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            onCreate(db);
        }
    }

并加入ListView部分:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        ctxContext = this.getApplicationContext();

        lView = (ListView) findViewById(R.id.ListView01);

        lView.setTextFilterEnabled(true);

        ParseSQLITETask task = new ParseSQLITETask();
        task.execute(null);

    } catch (Exception e) {
        LogErr(e.getMessage());
    }

}

private class ParseSQLITETask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... urls) {
        try {

            DataHelper dHelper = new DataHelper(getApplicationContext());
            list = (ArrayList<String>) dHelper.selectBrands();

        } catch (Exception e) {
            LogErr(e.getMessage());

        }
        return null;

    }

    @Override
    protected void onProgressUpdate(Void... progress) {

    }

    @Override
    protected void onPostExecute(Void result) {
        try {

            lView.setAdapter(new ArrayAdapter<String>(ctxContext,
                    R.layout.list_item, list));
        } catch (Exception e) {
            LogErr(e.getMessage());

        }
    }

}

4 个答案:

答案 0 :(得分:11)

您正在检索整个记录集并将其转换为ArrayList。不要这样做。将光标传回并使用适当的适配器(例如SimpleCursorAdapter)。

编辑:您可能还想在该列上考虑creating an index,因为它可能会加快您的检索时间。

答案 1 :(得分:6)

如果不进行测试,我会希望您的list.contains在很多时候吃掉。您应该能够使用SELECT DISTINCT删除数据库库中的重复项。

编辑:Femi是正确的,你可能不需要List的所有记录。光标和/或适配器就足够了。还要考虑是否可以以任何方式缩小结果(例如WHERE子句)。

答案 2 :(得分:0)

以下是我最终使用的代码,万一有人遇到类似的问题:

public class DataHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "TableName";
    private Context context;
    private SQLiteDatabase db;

    public static final String ColName = "ColumnName";
    public static final String KEY_ID = "_id";

    public DataHelper(Context context) {
        this.context = context;
        OpenHelper openHelper = new OpenHelper(this.context);

        this.db = SQLiteDatabase.openDatabase(
                Environment.getExternalStorageDirectory()
                        + "/myDB.db", null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);

    }

    public void deleteAll() {
        this.db.delete(TABLE_NAME, null, null);
    }

    public Cursor selectBrandsCursor() {

        String[] columns = new String[] { ColName, KEY_ID };
        Cursor cursor = this.db.rawQuery("SELECT " + ColName + ", " + KEY_ID
                + " FROM " + TABLE_NAME + " GROUP BY " + ColName + ";", null);

        return cursor;
    }

    private static class OpenHelper extends SQLiteOpenHelper {
        OpenHelper(Context context) {
            super(context, null, null, DATABASE_VERSION);

        }

        @Override
        public void onCreate(SQLiteDatabase db) {

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            onCreate(db);
        }
    }
}

工作得很好,很快,重复免费,感谢马修和费米的建议。

答案 3 :(得分:0)

它与该主题没有直接关系,但是我只是发现了为什么数据库查询非常慢。

在该应用程序的冷启动处,立即在100ms内加载了20.000个数据库条目。如果在运行的应用程序中重复了相同的查询,则需要9000毫秒(9秒)。

原因是新的Android Studio 数据库检查器,这大大降低了查询速度。