在大型数据库上运行搜索查询 - SQLite / Android

时间:2018-04-16 14:18:31

标签: android sqlite

我有一张超过10万件物品的桌子,而且我忙着离开去展示和搜索它们,但是我已经把光标窗口窗口充满了#34; ,我知道这是由于表格的大小,并且一直在阅读有关分解查询的内容。

 public List<Product> getAllItems(){
        List<Item> itemList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        String countQuery = "SELECT COUNT(*) FROM " + TABLE;
        Cursor cursor = db.rawQuery(countQuery,null);
        cursor.moveToFirst();
        int num = cursor.getInt(0);
        for(int x = 1; x < num; x = x + 10000){
            //Select All Query
            String selectQuery = "SELECT * FROM " + TABLE + " WHERE " + KEY_ID + " >= " + String.valueOf(x) + " AND " + KEY_ID + " <= " + String.valueOf(x + 10000);
            cursor = db.rawQuery(selectQuery, null);
            // Looping through all the rows and adding to the list
            if(cursor.moveToFirst()){
                do{
                    Item item= new Item();
                    //Add item to list
                    itemList.add(item);
                } while (cursor.moveToNext());
            }
        }
        cursor.close();
        db.close();
        return itemList;
    }

现在这已经略微加快了查询速度,但我是否正确地采用这种思路?

0 个答案:

没有答案