Add slow query logging to updateSuggestedFrom* methods.

Produces the following output:

D  Explain:
D    SCAN TABLE fdroid_app
D    EXECUTE CORRELATED SCALAR SUBQUERY 0
D    SEARCH TABLE fdroid_apk USING INDEX apk_vercode
D    EXECUTE CORRELATED SCALAR SUBQUERY 1
D    SEARCH TABLE fdroid_app AS innerAppName USING INTEGER PRIMARY KEY (rowid=?)
D    EXECUTE CORRELATED SCALAR SUBQUERY 2
D    SEARCH TABLE fdroid_package AS pkg USING INTEGER PRIMARY KEY (rowid=?)
D    SEARCH TABLE fdroid_installedApp AS installed USING INDEX sqlite_autoindex_fdroid_installedApp_1 (appId=?)

There are two possibilities here, one is the number of correlated sub
queries (three seems a bit excessive). Alterantively, it could be the
fact that one of the inner queries is using a string index (appId=?)
instead of an integer primary key.
This commit is contained in:
Peter Serwylo 2017-06-09 09:37:46 +10:00
parent d0444dafca
commit b729f4dc84
2 changed files with 20 additions and 2 deletions

View File

@ -985,7 +985,7 @@ public class AppProvider extends FDroidProvider {
" ( " + app + "." + Cols.IS_COMPATIBLE + " = 0 OR " + apk + "." + Cols.IS_COMPATIBLE + " = 1 ) ) " +
" WHERE " + Cols.UPSTREAM_VERSION_CODE + " > 0 ";
db().execSQL(updateSql);
LoggingQuery.execSQL(db(), updateSql);
}
/**
@ -1015,7 +1015,7 @@ public class AppProvider extends FDroidProvider {
" ( " + app + "." + Cols.IS_COMPATIBLE + " = 0 OR " + apk + "." + ApkTable.Cols.IS_COMPATIBLE + " = 1 ) ) " +
" WHERE COALESCE(" + Cols.UPSTREAM_VERSION_CODE + ", 0) = 0 OR " + Cols.SUGGESTED_VERSION_CODE + " IS NULL ";
db().execSQL(updateSql);
LoggingQuery.execSQL(db(), updateSql);
}
/**

View File

@ -66,6 +66,20 @@ final class LoggingQuery {
return db.rawQuery(query, queryArgs);
}
private void execSQLInternal() {
if (BuildConfig.DEBUG) {
long startTime = System.currentTimeMillis();
db.execSQL(query);
long queryDuration = System.currentTimeMillis() - startTime;
if (queryDuration >= SLOW_QUERY_DURATION) {
logSlowQuery(queryDuration);
}
} else {
db.execSQL(query);
}
}
/**
* Log the query and its duration to the console. In addition, execute an "EXPLAIN QUERY PLAN"
* for the query in question so that the query can be diagnosed (https://sqlite.org/eqp.html)
@ -116,4 +130,8 @@ final class LoggingQuery {
public static Cursor query(SQLiteDatabase db, String query, String[] queryBuilderArgs) {
return new LoggingQuery(db, query, queryBuilderArgs).rawQuery();
}
public static void execSQL(SQLiteDatabase db, String sql) {
new LoggingQuery(db, sql, null).execSQLInternal();
}
}