[FIX] mail.thread: optimize lookup of res.users by email

Searching on res.partners with domain [('user_ids' , '!=', False)]
will currently be translated into a huge "ID IN <...>"  query,
with the IDs of all existing users.
On a database with a lot of users, this can be measured
in seconds (e.g. 3-4 seconds with 500k+ users),
leading to significant delays in email delivery.

Using a direct lookup in res.users is more direct and faster.
This commit is contained in:
Olivier Dony
2015-12-10 18:43:17 +01:00
parent 4432639fb5
commit 3d32e99665
+3 -2
View File
@@ -1475,6 +1475,7 @@ class MailThread(models.AbstractModel):
followers = record.message_partner_ids
Partner = self.env['res.partner'].sudo()
Users = self.env['res.users'].sudo()
partner_ids = []
for contact in emails:
@@ -1495,10 +1496,10 @@ class MailThread(models.AbstractModel):
email_brackets = "<%s>" % email_address
if not partner_id:
# exact, case-insensitive match
partners = Partner.search([('email', '=ilike', email_address), ('user_ids', '!=', False)], limit=1)
partners = Users.search([('email', '=ilike', email_address)], limit=1).mapped('partner_id')
if not partners:
# if no match with addr-spec, attempt substring match within name-addr pair
partners = Partner.search([('email', 'ilike', email_brackets), ('user_ids', '!=', False)], limit=1)
partners = Users.search([('email', 'ilike', email_brackets)], limit=1).mapped('partner_id')
if partners:
partner_id = partners[0].id
# third try: check in partners