From 8545e05fc0aaad1c942317f6b4ac120da415b3cc Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 16 Nov 2018 16:35:22 +0100 Subject: [PATCH] [FIX] mass_mailing: fix slow contact number query The query was computing the count for all mailing lists, not just the one being requested. This prevents optimizing the SELECT with indexes. In addition, using a subselect for the blacklist forced a full table scan on the contacts - switching to a LEFT JOIN avoids that. With a blacklist of a few hundred thousand entries, and a table of mailing list contacts in the same order of magnitude, this brings the SELECT from 3+ seconds down to 3 milliseconds - with the same result. Bonus: no need to perform the regexp extraction of emails to test for NULL values -> substring(x) is NULL iff x is NULL. --- addons/mass_mailing/models/mass_mailing.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/addons/mass_mailing/models/mass_mailing.py b/addons/mass_mailing/models/mass_mailing.py index 72f6b71b5e9..643c03db195 100644 --- a/addons/mass_mailing/models/mass_mailing.py +++ b/addons/mass_mailing/models/mass_mailing.py @@ -114,14 +114,16 @@ class MassMailingList(models.Model): from mail_mass_mailing_contact_list_rel r left join mail_mass_mailing_contact c on (r.contact_id=c.id) + left join mail_blacklist bl on (LOWER(substring(c.email, %s)) = bl.email and bl.active) where + list_id in %s COALESCE(r.opt_out,FALSE) = FALSE - AND substring(c.email, '%s') IS NOT NULL - AND LOWER(substring(c.email, '%s')) NOT IN (select email from mail_blacklist where active = TRUE) + AND c.email IS NOT NULL + AND bl.id IS NULL group by list_id - ''' % (EMAIL_PATTERN, EMAIL_PATTERN)) - data = dict(self.env.cr.fetchall()) + ''') + data = dict(self.env.cr.fetchall(), [EMAIL_PATTERN, tuple(self.ids)]) for mailing_list in self: mailing_list.contact_nbr = data.get(mailing_list.id, 0)