【问题标题】:column index not improving select statement (Android)列索引未改进选择语句(Android)
【发布时间】:2016-08-29 15:06:14
【问题描述】:

我有一个包含 250 万行的数据库表。我通过一列 (item_no) 或两列 (item_no & subitem_no) 查询此表。为了优化我的查询,我创建了两个索引。一个用于 item_no,一个用于 item_no 和 subitem_no 对。

示例:

CREATE INDEX Idx2 ON master(item_no);
CREATE INDEX Idx3 ON master(item_no, subitem_no);

现在当我在 SQLite 浏览器中运行这个查询时:

SELECT component, hours, unit, price
FROM master
WHERE barcode = "234567"
AND item_no = "1234"

或者这个:

SELECT component, hours, unit, price
FROM master
WHERE barcode = "234567"
AND item_no = "1234"
AND subitem_no = "34"

它执行得非常快。大约 76 毫秒 - 186 毫秒。这就是我想要的,没有索引的原始选择语句在 4000 - 6000 毫秒之间。所以这是我一直在寻找的巨大改进。所以现在我将数据库加载到我的 android 设备(Samsung Galaxy S6)上并为其提供相同的索引。查询速度没有任何提高......完全,select语句仍然需要4000 - 6000ms才能运行。

这就是我的做法。如果您发现任何错误,请告诉我,或者您可以解释为什么我没有看到预期的性能提升。

db.execSQL("CREATE INDEX Idx2 ON master(item_no, subitem_no);");
db.execSQL("CREATE INDEX Idx3 ON master(item_no);");

public ArrayList<Data> getData(String barcode, String itemNo) {
    ArrayList<Data> dataList = new ArrayList<>();

    try {
        Database db = Database.getInstance();
        db.open();  

        String where = BARCODE_COLUMN + " = ? AND " + ITEM_NO_COLUMN + " = ?";  
        String[] columns = {COMPONENT_COLUMN, HOURS_COLUMN, UNIT_COLUMN, PRICE_COLUMN};
        String[] args = {barcode, itemNo};
        String sort = COMPONENT_COLUMN + " ASC";
        Cursor cursor = db.getDb().query(true, MASTER_TABLE, columns, where, args, null, null, sort, null);
        cursor.moveToFirst();

        for (int i = 0; i < cursor.getCount(); i++) {
            Data data = new Data();
            data.setComponent(cursor.getString(cursor.getColumnIndex(COMPONENT_COLUMN)));
            data.setHours(cursor.getString(cursor.getColumnIndex(HOURS_COLUMN)));
            data.setUnit(cursor.getString(cursor.getColumnIndex(UNIT_COLUMN)));
            data.setPrice(cursor.getString(cursor.getColumnIndex(PRICE_COLUMN)));

            dataList.add(data);

            if (!cursor.isLast()) {
                cursor.moveToNext();
            }
        }

        cursor.close();
        db.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return dataList;
}

【问题讨论】:

  • barcode 上也添加索引!您应该索引所有 WHEREJOIN 字段。
  • 按照这个逻辑,我不妨在条码和 item_no 对中添加一个。谢谢,我试试
  • item_no 已被编入索引...不是吗?
  • 当然可以,但我读到如果您要进行多项选择,您也应该将它们与它们自己的索引配对。意思是因为查询是条形码 AND item_no 他们应该有一个“配对”索引,如此处所述sqlite.org/queryplanner.html
  • 另外@Rotwang 谢谢你,我刚刚创建了条形码索引以及带有 item_no 的配对,它的响应时间下降到 1 毫秒。虽然仍然只在 SQLite 浏览器中。我的应用程序仍然很慢

标签: android performance sqlite indexing


【解决方案1】:

一定是数据缓存问题。我重新启动了设备,卸载了我的应用程序,然后根据@Rotwang 的 cmets 更新了我的索引。现在,当我运行我的应用程序时,我得到了我想要的 3 毫秒查询时间。

这是我的修复:

db.execSQL("CREATE INDEX Idx2 ON master(barcode, item_no, subitem_no);");
db.execSQL("CREATE INDEX Idx3 ON master(barcode, item_no);");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多