From e8a7c8d6dd5a53d6e02e6b3f124d690aed1f81aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Mart=C3=ADnez?= Date: Mon, 31 Mar 2025 09:42:16 +0200 Subject: [PATCH] [PERF] account_reconcile_model_oca: add CTE to invoice amls candidates query Backport of odoo/enterprise#64754. This commit adds a CTE at the beginning of the invoice matching amls candidate to speedup the whole query. More info and speedup in PR above Related to https://github.com/odoo/odoo/commit/f396bc2f1f423e0c13a553a252953b99913f1b78 --- .../models/account_reconcile_model.py | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/account_reconcile_model_oca/models/account_reconcile_model.py b/account_reconcile_model_oca/models/account_reconcile_model.py index baa4bf24..53d4b178 100644 --- a/account_reconcile_model_oca/models/account_reconcile_model.py +++ b/account_reconcile_model_oca/models/account_reconcile_model.py @@ -396,11 +396,29 @@ class AccountReconcileModel(models.Model): sub_queries = [] all_params = [] + aml_cte = "" ( numerical_tokens, exact_tokens, _text_tokens, ) = self._get_invoice_matching_st_line_tokens(st_line) + if numerical_tokens or exact_tokens: + aml_cte = rf""" + WITH aml_cte AS ( + SELECT + account_move_line.id as account_move_line_id, + account_move_line.date as account_move_line_date, + account_move_line.date_maturity as account_move_line_date_maturity, + account_move_line.name as account_move_line_name, + account_move_line__move_id.name as account_move_line__move_id_name, + account_move_line__move_id.ref as account_move_line__move_id_ref + FROM {tables} + JOIN account_move account_move_line__move_id + ON account_move_line__move_id.id = account_move_line.move_id + WHERE {where_clause} + ) + """ # noqa: E501 + all_params += where_params if numerical_tokens: for table_alias, field in ( ("account_move_line", "name"), @@ -410,27 +428,24 @@ class AccountReconcileModel(models.Model): sub_queries.append( rf""" SELECT - account_move_line.id, - account_move_line.date, - account_move_line.date_maturity, + account_move_line_id as id, + account_move_line_date as date, + account_move_line_date_maturity as date_maturity, UNNEST( REGEXP_SPLIT_TO_ARRAY( SUBSTRING( REGEXP_REPLACE( - {table_alias}.{field}, '[^0-9\s]', '', 'g' + {table_alias}_{field}, '[^0-9\s]', '', 'g' ), '\S(?:.*\S)*' ), '\s+' ) ) AS token - FROM {tables} - JOIN account_move account_move_line__move_id - ON account_move_line__move_id.id = account_move_line.move_id - WHERE {where_clause} AND {table_alias}.{field} IS NOT NULL + FROM aml_cte + WHERE {table_alias}_{field} IS NOT NULL """ ) - all_params += where_params if exact_tokens: for table_alias, field in ( @@ -441,22 +456,20 @@ class AccountReconcileModel(models.Model): sub_queries.append( rf""" SELECT - account_move_line.id, - account_move_line.date, - account_move_line.date_maturity, - {table_alias}.{field} AS token - FROM {tables} - JOIN account_move account_move_line__move_id - ON account_move_line__move_id.id = account_move_line.move_id - WHERE {where_clause} AND COALESCE({table_alias}.{field}, '') != '' + account_move_line_id as id, + account_move_line_date as date, + account_move_line_date_maturity as date_maturity, + {table_alias}_{field} AS token + FROM aml_cte + WHERE COALESCE({table_alias}_{field}, '') != '' """ ) - all_params += where_params if sub_queries: order_by = get_order_by_clause(alias="sub") self._cr.execute( - """ + aml_cte + + """ SELECT sub.id, COUNT(*) AS nb_match