From 8bf004831fe7e66aa82cb0022e33b9241eb2b6b5 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Dufays Date: Mon, 12 Dec 2022 10:16:06 +0000 Subject: [PATCH] [IMP] crm: add index on phone and mobile fields This index allows to improve performances of learch searching based on phone and mobile fields. We currently use those fields are phone_sanitized is based on both fields, which could lead to information loss and/not not accurate results, notably when used to find duplicates. Task-3007714 closes odoo/odoo#105873 Signed-off-by: Thibault Delavallee (tde) --- addons/crm/models/crm_lead.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index c85b7c91224..dad5d39eaec 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -22,7 +22,7 @@ from odoo.tools.misc import get_lang from . import crm_stage _logger = logging.getLogger(__name__) - +_schema = logging.getLogger('odoo.schema') CRM_LEAD_FIELDS_TO_MERGE = [ @@ -707,12 +707,22 @@ class Lead(models.Model): # ------------------------------------------------------------ def _auto_init(self): - res = super(Lead, self)._auto_init() + super()._auto_init() tools.create_index(self._cr, 'crm_lead_user_id_team_id_type_index', self._table, ['user_id', 'team_id', 'type']) tools.create_index(self._cr, 'crm_lead_create_date_team_id_idx', self._table, ['create_date', 'team_id']) - return res + phone_pattern = r'[\s\\./\(\)\-]' + for field_name in ('phone', 'mobile'): + index_name = f'crm_lead_{field_name}_partial_tgm' + if tools.index_exists(self._cr, index_name): + continue + regex_expression = "regexp_replace((phone::text), %s::text, ''::text, 'g'::text)" + self._cr.execute( + f'CREATE INDEX "{index_name}" ON "{self._table}" ({regex_expression}) WHERE {field_name} IS NOT NULL', + (phone_pattern,) + ) + _schema.debug("Table %r: created index %r (%s)", self._table, index_name, regex_expression) @api.model_create_multi def create(self, vals_list):