Add covering indexes for main queries.

With no indexes at all, a join between X and Y tables would require a full
table scan of Y for each row in X. With an index on the relevant field in
Y, it would require an index lookup on the join field in Y for each row in
X, which contains a pointer to the row of interest in Y. This row is then
looked up and the relevant value extracted. By using a covering index (one
which includes all fields required to satisfy the query, with the first field
being the one which is looked up in the join), then once the index has been
searched, there is no need to then go to table Y because all the relevant
data is already in the index.

This offers a marginal performance improvement.
This commit is contained in:
Peter Serwylo 2016-07-25 18:25:50 +10:00
parent 3e3af3bbf3
commit 903048ffe4

View File

@ -222,10 +222,12 @@ class DBHelper extends SQLiteOpenHelper {
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
createAppApk(db); db.execSQL(CREATE_TABLE_APP);
db.execSQL(CREATE_TABLE_APK);
db.execSQL(CREATE_TABLE_INSTALLED_APP); db.execSQL(CREATE_TABLE_INSTALLED_APP);
db.execSQL(CREATE_TABLE_REPO); db.execSQL(CREATE_TABLE_REPO);
db.execSQL(CREATE_TABLE_APP_PREFS); db.execSQL(CREATE_TABLE_APP_PREFS);
ensureIndexes(db);
insertRepo( insertRepo(
db, db,
@ -683,10 +685,6 @@ class DBHelper extends SQLiteOpenHelper {
db.execSQL("drop table " + AppTable.NAME); db.execSQL("drop table " + AppTable.NAME);
db.execSQL("drop table " + ApkTable.NAME); db.execSQL("drop table " + ApkTable.NAME);
clearRepoEtags(db); clearRepoEtags(db);
createAppApk(db);
}
private static void createAppApk(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_APP); db.execSQL(CREATE_TABLE_APP);
db.execSQL(CREATE_TABLE_APK); db.execSQL(CREATE_TABLE_APK);
ensureIndexes(db); ensureIndexes(db);
@ -702,6 +700,21 @@ class DBHelper extends SQLiteOpenHelper {
db.execSQL("CREATE INDEX IF NOT EXISTS apk_vercode on " + ApkTable.NAME + " (" + ApkTable.Cols.VERSION_CODE + ");"); db.execSQL("CREATE INDEX IF NOT EXISTS apk_vercode on " + ApkTable.NAME + " (" + ApkTable.Cols.VERSION_CODE + ");");
db.execSQL("CREATE INDEX IF NOT EXISTS apk_appId on " + ApkTable.NAME + " (" + ApkTable.Cols.APP_ID + ");"); db.execSQL("CREATE INDEX IF NOT EXISTS apk_appId on " + ApkTable.NAME + " (" + ApkTable.Cols.APP_ID + ");");
db.execSQL("CREATE INDEX IF NOT EXISTS repoId ON " + ApkTable.NAME + " (" + ApkTable.Cols.REPO_ID + ");"); db.execSQL("CREATE INDEX IF NOT EXISTS repoId ON " + ApkTable.NAME + " (" + ApkTable.Cols.REPO_ID + ");");
Utils.debugLog(TAG, "Ensuring indexes exist for " + AppPrefsTable.NAME);
db.execSQL("CREATE INDEX IF NOT EXISTS appPrefs_appId on " + AppPrefsTable.NAME + " (" + AppPrefsTable.Cols.APP_ID + ");");
db.execSQL("CREATE INDEX IF NOT EXISTS appPrefs_appId_ignoreAll_ignoreThis on " + AppPrefsTable.NAME + " (" +
AppPrefsTable.Cols.APP_ID + ", " +
AppPrefsTable.Cols.IGNORE_ALL_UPDATES + ", " +
AppPrefsTable.Cols.IGNORE_THIS_UPDATE + ");");
Utils.debugLog(TAG, "Ensuring indexes exist for " + InstalledAppTable.NAME);
db.execSQL("CREATE INDEX IF NOT EXISTS installedApp_appId_vercode on " + InstalledAppTable.NAME + " (" +
InstalledAppTable.Cols.PACKAGE_NAME + ", " + InstalledAppTable.Cols.VERSION_CODE + ");");
Utils.debugLog(TAG, "Ensuring indexes exist for " + RepoTable.NAME);
db.execSQL("CREATE INDEX IF NOT EXISTS repo_id_isSwap on " + RepoTable.NAME + " (" +
RepoTable.Cols._ID + ", " + RepoTable.Cols.IS_SWAP + ");");
} }
/** /**