SQLite查询在Android上确实很慢

mil*_*ous 4 java sqlite android

我有一个非常大的数据库,我需要查询以获取一些数据并在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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

并加入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());

        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Fem*_*emi 11

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

编辑:您可能还想考虑在该列上创建索引,因为它可能会加快您的检索时间.


Mat*_*hen 6

没有测试,我希望你的list.contains大部分时间都会吃掉.您应该能够使用SELECT DISTINCT删除数据库库中的重复项.

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