[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 f396bc2f1f
pull/819/head
Víctor Martínez 2025-03-31 09:42:16 +02:00
parent 6d53fcd7f8
commit f9078dc573
1 changed files with 32 additions and 19 deletions

View File

@ -399,11 +399,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 {from_clause}
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"),
@ -413,27 +431,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 {from_clause}
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 (
@ -444,22 +459,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 {from_clause}
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