查询SQLite数据库的方法永远完成

时间:2012-09-11 15:57:02

标签: java performance sqlite

我有一个SQLite数据库,其中一个表有〜90000行..这是一个花费太长时间才完成的方法..

void intilizeAyaHighlighShapes() {
        ayaShapes = new ArrayList<ArrayList<AyaShape>>();
        ArrayList<AyaShape> ayaRowsHighlight = new ArrayList<AyaShape>(); // contains the highligh shapes of a specific aya for all its rows

        int tmpAya = 1, tmpLineMin = 0, tmpLineMax = 0;
        int maxLeft = 0, maxRight = 0, maxTop = 0, maxBottom = 0;

        SQLiteDatabase db = SQLiteDatabase.openDatabase(Environment.getExternalStorageDirectory() + "/ayahinfo_800.db", null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = null;

        while (tmpAya < Database.AyatNumberInSura[suraIndex]) {
            cursor = db.rawQuery("SELECT MIN(line_number) FROM glyphs WHERE(sura_number=" + Integer.toString(suraIndex + 1) + " AND ayah_number=" + Integer.toString(tmpAya) + ")",
                    null);
            cursor.moveToFirst();
            tmpLineMin = cursor.getInt(0);

            cursor = db.rawQuery("SELECT MAX(line_number) FROM glyphs WHERE(sura_number=" + Integer.toString(suraIndex + 1) + " AND ayah_number=" + Integer.toString(tmpAya) + ")",
                    null);
            cursor.moveToFirst();
            tmpLineMax = cursor.getInt(0);


            for (int i = tmpLineMin; i <= tmpLineMax; i++) {
                cursor = db.rawQuery("SELECT MAX(max_x) FROM glyphs WHERE (sura_number=" + Integer.toString(suraIndex + 1) + " AND ayah_number=" + Integer.toString(tmpAya)
                        + " AND line_number=" + Integer.toString(i) + ")", null);
                cursor.moveToFirst();
                maxRight = cursor.getInt(0);

                cursor = db.rawQuery("SELECT MIN(min_x) FROM glyphs WHERE (sura_number=" + Integer.toString(suraIndex + 1) + " AND ayah_number=" + Integer.toString(tmpAya)
                        + " AND line_number=" + Integer.toString(i) + ")", null);
                cursor.moveToFirst();
                maxLeft = cursor.getInt(0);

                cursor = db.rawQuery("SELECT MAX(max_y) FROM glyphs WHERE (sura_number=" + Integer.toString(suraIndex + 1) + " AND ayah_number=" + Integer.toString(tmpAya)
                        + " AND line_number=" + Integer.toString(i) + ")", null);
                cursor.moveToFirst();
                maxBottom = cursor.getInt(0);

                cursor = db.rawQuery("SELECT MIN(min_y) FROM glyphs WHERE (sura_number=" + Integer.toString(suraIndex + 1) + " AND ayah_number=" + Integer.toString(tmpAya)
                        + " AND line_number=" + Integer.toString(i) + ")", null);
                cursor.moveToFirst();
                maxTop = cursor.getInt(0);



                ayaRowsHighlight.add(new AyaShape(maxLeft, maxRight, maxTop, maxBottom));
            }

            ayaShapes.add(ayaRowsHighlight);
            ayaRowsHighlight.clear();
            tmpAya++;

        }





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

    }

这段代码的哪些部分可能会花费这么长时间? 我应该在这里写下这段代码的目的吗?

1 个答案:

答案 0 :(得分:0)

您可以简单地将多个查询与相同的WHERE条件组合在一起:

SELECT MIN(line_number), MAX(line_number)
 FROM glyphs
 WHERE sura_number = ? AND ayah_number = ?

SELECT MAX(max_x), MIN(min_x), MAX(max_y), MIN(min_y)
 FROM glyphs
 WHERE sura_number = ? AND ayah_number = ? AND line_number = ?

但是,在处理各个字形边界之前,您的算法实际上并不要求tmpLineMintmpLineMax已知。因此,您可以通过使用单个查询然后逐步执行结果行(每个行号一个)来进一步加快速度:

SELECT line_number, MAX(max_x), MIN(min_x), MAX(max_y), MIN(min_y)
 FROM glyphs
 WHERE sura_number = ? AND ayah_number = ?
 GROUP BY line_number
 ORDER BY line_number