Fix possible performance issues with reactions query.

master
Greyson Parrelli 2019-12-05 19:10:37 -05:00
parent e94d4b64cf
commit 19c83de510
5 changed files with 13 additions and 5 deletions

View File

@ -99,8 +99,8 @@ public abstract class MessagingDatabase extends Database implements MmsSmsColumn
public void setReactionsSeen(long threadId) {
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
String whereClause = THREAD_ID + " = ?";
String[] whereArgs = new String[]{String.valueOf(threadId)};
String whereClause = THREAD_ID + " = ? AND " + REACTIONS_UNREAD + " = ?";
String[] whereArgs = new String[]{String.valueOf(threadId), "1"};
values.put(REACTIONS_UNREAD, 0);
values.put(REACTIONS_LAST_SEEN, System.currentTimeMillis());

View File

@ -175,6 +175,7 @@ public class MmsDatabase extends MessagingDatabase {
"CREATE INDEX IF NOT EXISTS mms_message_box_index ON " + TABLE_NAME + " (" + MESSAGE_BOX + ");",
"CREATE INDEX IF NOT EXISTS mms_date_sent_index ON " + TABLE_NAME + " (" + DATE_SENT + ");",
"CREATE INDEX IF NOT EXISTS mms_thread_date_index ON " + TABLE_NAME + " (" + THREAD_ID + ", " + DATE_RECEIVED + ");",
"CREATE INDEX IF NOT EXISTS mms_reactions_unread_index ON " + TABLE_NAME + " (" + REACTIONS_UNREAD + ");"
};
private static final String[] MMS_PROJECTION = new String[] {

View File

@ -138,7 +138,7 @@ public class MmsSmsDatabase extends Database {
public Cursor getUnread() {
String order = MmsSmsColumns.NORMALIZED_DATE_RECEIVED + " ASC";
String selection = MmsSmsColumns.NOTIFIED + " = 0 AND (" + MmsSmsColumns.READ + " = 0 OR " + MmsSmsColumns.REACTIONS_UNREAD + ")";
String selection = MmsSmsColumns.NOTIFIED + " = 0 AND (" + MmsSmsColumns.READ + " = 0 OR " + MmsSmsColumns.REACTIONS_UNREAD + " = 1)";
return queryTables(PROJECTION, selection, order, null);
}

View File

@ -110,7 +110,8 @@ public class SmsDatabase extends MessagingDatabase {
"CREATE INDEX IF NOT EXISTS sms_read_and_notified_and_thread_id_index ON " + TABLE_NAME + "(" + READ + "," + NOTIFIED + "," + THREAD_ID + ");",
"CREATE INDEX IF NOT EXISTS sms_type_index ON " + TABLE_NAME + " (" + TYPE + ");",
"CREATE INDEX IF NOT EXISTS sms_date_sent_index ON " + TABLE_NAME + " (" + DATE_SENT + ");",
"CREATE INDEX IF NOT EXISTS sms_thread_date_index ON " + TABLE_NAME + " (" + THREAD_ID + ", " + DATE_RECEIVED + ");"
"CREATE INDEX IF NOT EXISTS sms_thread_date_index ON " + TABLE_NAME + " (" + THREAD_ID + ", " + DATE_RECEIVED + ");",
"CREATE INDEX IF NOT EXISTS sms_reactions_unread_index ON " + TABLE_NAME + " (" + REACTIONS_UNREAD + ");"
};
private static final String[] MESSAGE_PROJECTION = new String[] {

View File

@ -97,8 +97,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
private static final int USERNAMES = 36;
private static final int REACTIONS = 37;
private static final int STORAGE_SERVICE = 38;
private static final int REACTIONS_UNREAD_INDEX = 39;
private static final int DATABASE_VERSION = 38;
private static final int DATABASE_VERSION = 39;
private static final String DATABASE_NAME = "signal.db";
private final Context context;
@ -673,6 +674,11 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
// }
}
if (oldVersion < REACTIONS_UNREAD_INDEX) {
db.execSQL("CREATE INDEX IF NOT EXISTS sms_reactions_unread_index ON sms (reactions_unread);");
db.execSQL("CREATE INDEX IF NOT EXISTS mms_reactions_unread_index ON mms (reactions_unread);");
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();