diff --git a/inbox/mailsync/backends/gmail.py b/inbox/mailsync/backends/gmail.py index 0ee3bbf42..8fbf3a10b 100644 --- a/inbox/mailsync/backends/gmail.py +++ b/inbox/mailsync/backends/gmail.py @@ -325,7 +325,14 @@ def resync_uids_impl(self): .options( load_only("msg_uid"), joinedload("message").load_only("g_msgid") ) - .filter_by(account_id=self.account_id, folder_id=self.folder_id) + .filter( + ImapUid.account_id == self.account_id, + ImapUid.folder_id == self.folder_id, + ) + .with_hint( + ImapUid, + "FORCE INDEX (ix_imapuid_account_id_folder_id_msg_uid_desc)", + ) ) chunk_size = 1000 diff --git a/inbox/mailsync/backends/imap/common.py b/inbox/mailsync/backends/imap/common.py index b4de8b569..0966613f6 100644 --- a/inbox/mailsync/backends/imap/common.py +++ b/inbox/mailsync/backends/imap/common.py @@ -156,10 +156,16 @@ def update_metadata(account_id, folder_id, folder_role, new_flags, session): account = Account.get(account_id, session) change_count = 0 - for item in session.query(ImapUid).filter( - ImapUid.account_id == account_id, - ImapUid.msg_uid.in_(new_flags), - ImapUid.folder_id == folder_id, + for item in ( + session.query(ImapUid) + .filter( + ImapUid.account_id == account_id, + ImapUid.folder_id == folder_id, + ImapUid.msg_uid.in_(new_flags), + ) + .with_hint( + ImapUid, "FORCE INDEX (ix_imapuid_account_id_folder_id_msg_uid_desc)" + ) ): flags = new_flags[item.msg_uid].flags labels = getattr(new_flags[item.msg_uid], "labels", None) @@ -204,6 +210,10 @@ def remove_deleted_uids(account_id, folder_id, uids): ImapUid.folder_id == folder_id, ImapUid.msg_uid == uid, ) + .with_hint( + ImapUid, + "FORCE INDEX (ix_imapuid_account_id_folder_id_msg_uid_desc)", + ) .first() ) if imapuid is None: diff --git a/inbox/mailsync/backends/imap/generic.py b/inbox/mailsync/backends/imap/generic.py index 7dd2c22fa..604da4992 100644 --- a/inbox/mailsync/backends/imap/generic.py +++ b/inbox/mailsync/backends/imap/generic.py @@ -517,8 +517,13 @@ def resync_uids_impl(self): with session_scope(self.namespace_id) as db_session: invalid_uids = { uid - for uid, in db_session.query(ImapUid.msg_uid).filter_by( - account_id=self.account_id, folder_id=self.folder_id + for uid, in db_session.query(ImapUid.msg_uid) + .filter( + ImapUid.account_id == self.account_id, + ImapUid.folder_id == self.folder_id, + ) + .with_hint( + ImapUid, "FORCE INDEX(ix_imapuid_account_id_folder_id_msg_uid_desc)" ) } with self.syncmanager_lock: @@ -545,20 +550,24 @@ def create_message( # Check if we somehow already saved the imapuid (shouldn't happen, but # possible due to race condition). If so, don't commit changes. - existing_imapuid = ( + imapuid_exists = db_session.query( db_session.query(ImapUid) .filter( ImapUid.account_id == account.id, ImapUid.folder_id == folder.id, ImapUid.msg_uid == raw_message.uid, ) - .first() - ) - if existing_imapuid is not None: + .with_hint( + ImapUid, "FORCE INDEX(ix_imapuid_account_id_folder_id_msg_uid_desc)" + ) + .exists() + ).scalar() + if imapuid_exists: log.warning( "Expected to create imapuid, but existing row found", - remote_msg_uid=raw_message.uid, - existing_imapuid=existing_imapuid.id, + account_id=account.id, + folder_id=folder.id, + msg_uid=raw_message.uid, ) return None