From d3f9cfbdfa0bc8b4354e2a0ae3b927f057642c73 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Mon, 27 Jun 2016 22:41:48 +1000 Subject: [PATCH] Remove need for `temorary b-tree for order by` in most cases by introducing two indexes. The fact that sqlite chose to do a "FULL TABLE SCAN" right off the bat when showing a list of apps results in it having to do extra work at the end of the query to sort. If the scan can be utilise an index, then the sorting is already done and a b-tree need not be constructed. Thus, this introduces indexes for both the `name` column (used to sort most lists of apps) and the `added` column (used to figure out the `Whats New` apps). --- .../java/org/fdroid/fdroid/data/DBHelper.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java index c688ee01a..677c207e0 100644 --- a/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java +++ b/app/src/main/java/org/fdroid/fdroid/data/DBHelper.java @@ -321,18 +321,21 @@ class DBHelper extends SQLiteOpenHelper { private void migrateAppPrimaryKeyToRowId(SQLiteDatabase db, int oldVersion) { if (oldVersion < 58) { - final String alter = "ALTER TABLE " + ApkTable.NAME + " ADD COLUMN appId NUMERIC"; - Log.i(TAG, "Adding appId foreign key to fdroid_apk."); + db.execSQL("CREATE INDEX IF NOT EXISTS name ON " + AppTable.NAME + " (" + AppTable.Cols.NAME + ")"); + db.execSQL("CREATE INDEX IF NOT EXISTS added ON " + AppTable.NAME + " (" + AppTable.Cols.ADDED + ")"); + + final String alter = "ALTER TABLE " + ApkTable.NAME + " ADD COLUMN " + ApkTable.Cols.APP_ID + " NUMERIC"; + Log.i(TAG, "Adding appId foreign key to " + ApkTable.NAME); Utils.debugLog(TAG, alter); db.execSQL(alter); final String update = "UPDATE " + ApkTable.NAME + " SET appId = ( " + - "SELECT app.rowid " + + "SELECT app." + AppTable.Cols.ROW_ID + " " + "FROM " + ApkTable.NAME + " AS app " + - "WHERE " + ApkTable.NAME + ".id = app.id " + + "WHERE " + ApkTable.NAME + "." + ApkTable.Cols.PACKAGE_NAME + " = app." + AppTable.Cols.PACKAGE_NAME + " " + ")"; - Log.i(TAG, "Updating foreign key from fdroid_apk to fdroid_app to use numeric foreign key."); + Log.i(TAG, "Updating foreign key from " + ApkTable.NAME + " to " + AppTable.NAME + " to use numeric foreign key."); Utils.debugLog(TAG, update); db.execSQL(update); } @@ -590,6 +593,8 @@ class DBHelper extends SQLiteOpenHelper { private static void createAppApk(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_APP); db.execSQL("create index app_id on " + AppTable.NAME + " (" + AppTable.Cols.PACKAGE_NAME + ");"); + db.execSQL("create index name on " + AppTable.NAME + " (" + AppTable.Cols.NAME + ");"); // Used for sorting most lists + db.execSQL("create index added on " + AppTable.NAME + " (" + AppTable.Cols.ADDED + ");"); // Used for sorting "newly added" db.execSQL(CREATE_TABLE_APK); db.execSQL("create index apk_vercode on " + ApkTable.NAME + " (" + ApkTable.Cols.VERSION_CODE + ");"); db.execSQL("create index apk_appId on " + ApkTable.NAME + " (" + ApkTable.Cols.APP_ID + ");");