[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) <tde@openerp.com>
This commit is contained in:
Pierre-Yves Dufays
2022-12-12 13:18:46 +01:00
committed by Thibault Delavallée
parent 33d23cc246
commit 8bf004831f
+13 -3
View File
@@ -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):