From c855bee515a6de03120f408d0745b605f0d5ad3a Mon Sep 17 00:00:00 2001 From: "Habib (ayh)" Date: Tue, 12 Sep 2023 11:49:13 +0200 Subject: [PATCH] [IMP] account: enable index scan on AML The corresponding enterprise PR enables sorting account move lines on the bank recon by matching amount. Unfortunately there is a significant performance impact when enabling this ORDER BY (~300 ms -> ~2600 ms tested on a large db) In order to optimise the above, we slightly modify the domains used so that the existing index `account_move_line__unreconciled_index` is used. This is not yet tested on a large DB (due to the index change), however observations on a populated database indicate an improvement: without index: 80ms with index: 30ms closes odoo/odoo#135155 Related: odoo/enterprise#45816 Related: odoo/upgrade#5146 Signed-off-by: Laurent Smet (las) --- addons/account/models/account_bank_statement_line.py | 8 +++++++- addons/account/models/account_move_line.py | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account_bank_statement_line.py b/addons/account/models/account_bank_statement_line.py index c8115d5d316..6f63dbbbf63 100644 --- a/addons/account/models/account_bank_statement_line.py +++ b/addons/account/models/account_bank_statement_line.py @@ -455,6 +455,11 @@ class AccountBankStatementLine(models.Model): return bank_account.filtered(lambda x: x.company_id in (False, self.company_id)) def _get_default_amls_matching_domain(self): + self.ensure_one() + all_reconcilable_account_ids = self.env['account.account'].search([ + ("company_id", "child_of", self.company_id.root_id.id), + ('reconcile', '=', True), + ]).ids return [ # Base domain. ('display_type', 'not in', ('line_section', 'line_note')), @@ -462,7 +467,8 @@ class AccountBankStatementLine(models.Model): ('company_id', 'child_of', self.company_id.root_id.id), # Reconciliation domain. ('reconciled', '=', False), - ('account_id.reconcile', '=', True), + # Domain to use the account_move_line__unreconciled_index + ('account_id', 'in', all_reconcilable_account_ids), # Special domain for payments. '|', ('account_id.account_type', 'not in', ('asset_receivable', 'liability_payable')), diff --git a/addons/account/models/account_move_line.py b/addons/account/models/account_move_line.py index 36cc5abda3b..426097cdf9a 100644 --- a/addons/account/models/account_move_line.py +++ b/addons/account/models/account_move_line.py @@ -1355,6 +1355,9 @@ class AccountMoveLine(models.Model): """ create_index(self._cr, 'account_move_line_partner_id_ref_idx', 'account_move_line', ["partner_id", "ref"]) create_index(self._cr, 'account_move_line_date_name_id_idx', 'account_move_line', ["date desc", "move_name desc", "id"]) + # Match exactly how the ORM converts domains to ensure the query planner uses it + create_index(self._cr, 'account_move_line__unreconciled_index', 'account_move_line', ['account_id', 'partner_id'], + where="(reconciled IS NULL OR reconciled = false OR reconciled IS NOT true) AND parent_state = 'posted'") super().init() def default_get(self, fields_list):