From 39eaf87b1b9d4720d850cd70ddcd390f63522bbd Mon Sep 17 00:00:00 2001 From: "Habib (ayh)" Date: Mon, 27 Nov 2023 17:09:48 +0100 Subject: [PATCH] [IMP] account,*: performance on Foreign Keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On deletion of records, foreign keys are updated. For larger tables (like account.move, account.bank.statement.line) this could take some time. adding a "not null btree index" on sparsely populated many2one fields significantly improves performance. In the analysis of deleting a bank statement line. Before: 7.4s to delete the bank statement line (missing index on suspense_statement_line_id consuming 99% of the execution time) 3.8s to delete the corresponding account move After: With the indexes, a 700x performance improvement was found. This opportunity is taken to add an index on all many2one fields to account.move & bank.statement.line. Issue report by @tsb-odoo closes odoo/odoo#145643 X-original-commit: f10b87991cc72f5e16b587b6c9ede7e338ec79ac Related: odoo/enterprise#52466 Signed-off-by: William André (wan) Signed-off-by: Habib Ayob (ayh) --- addons/account/models/account_bank_statement_line.py | 1 + addons/account/models/account_move.py | 2 ++ addons/account/models/account_partial_reconcile.py | 2 +- addons/account/models/account_payment.py | 1 + addons/account_debit_note/models/account_move.py | 2 +- addons/account_edi/models/account_edi_document.py | 2 +- addons/l10n_id_efaktur/models/account_move.py | 2 +- addons/sale_timesheet/models/account.py | 2 +- addons/stock_landed_costs/models/stock_landed_cost.py | 1 + 9 files changed, 10 insertions(+), 5 deletions(-) diff --git a/addons/account/models/account_bank_statement_line.py b/addons/account/models/account_bank_statement_line.py index 27636de023b..e93a59eacdb 100644 --- a/addons/account/models/account_bank_statement_line.py +++ b/addons/account/models/account_bank_statement_line.py @@ -44,6 +44,7 @@ class AccountBankStatementLine(models.Model): comodel_name='account.move', auto_join=True, string='Journal Entry', required=True, readonly=True, ondelete='cascade', + index='btree_not_null', check_company=True) statement_id = fields.Many2one( comodel_name='account.bank.statement', diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index e6957559ff3..91d978d25fc 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -174,6 +174,7 @@ class AccountMove(models.Model): string="Statement Line", copy=False, check_company=True, + index='btree_not_null', ) statement_id = fields.Many2one( related="statement_line_id.statement_id" @@ -226,6 +227,7 @@ class AccountMove(models.Model): comodel_name='account.move', string='First recurring entry', readonly=True, copy=False, + index='btree_not_null', ) hide_post_button = fields.Boolean(compute='_compute_hide_post_button', readonly=True) to_check = fields.Boolean( diff --git a/addons/account/models/account_partial_reconcile.py b/addons/account/models/account_partial_reconcile.py index 3e17fed99a5..f89d815fa0c 100644 --- a/addons/account/models/account_partial_reconcile.py +++ b/addons/account/models/account_partial_reconcile.py @@ -19,7 +19,7 @@ class AccountPartialReconcile(models.Model): full_reconcile_id = fields.Many2one( comodel_name='account.full.reconcile', string="Full Reconcile", copy=False) - exchange_move_id = fields.Many2one(comodel_name='account.move') + exchange_move_id = fields.Many2one(comodel_name='account.move', index='btree_not_null') # ==== Currency fields ==== company_currency_id = fields.Many2one( diff --git a/addons/account/models/account_payment.py b/addons/account/models/account_payment.py index 8e7642abea8..315fff7b64b 100644 --- a/addons/account/models/account_payment.py +++ b/addons/account/models/account_payment.py @@ -16,6 +16,7 @@ class AccountPayment(models.Model): move_id = fields.Many2one( comodel_name='account.move', string='Journal Entry', required=True, readonly=True, ondelete='cascade', + index='btree_not_null', check_company=True) is_reconciled = fields.Boolean(string="Is Reconciled", store=True, diff --git a/addons/account_debit_note/models/account_move.py b/addons/account_debit_note/models/account_move.py index 432a7c8f631..3333b9f025f 100644 --- a/addons/account_debit_note/models/account_move.py +++ b/addons/account_debit_note/models/account_move.py @@ -6,7 +6,7 @@ from odoo import models, fields, api, _ class AccountMove(models.Model): _inherit = "account.move" - debit_origin_id = fields.Many2one('account.move', 'Original Invoice Debited', readonly=True, copy=False) + debit_origin_id = fields.Many2one('account.move', 'Original Invoice Debited', readonly=True, copy=False, index='btree_not_null') debit_note_ids = fields.One2many('account.move', 'debit_origin_id', 'Debit Notes', help="The debit notes created for this invoice") debit_note_count = fields.Integer('Number of Debit Notes', compute='_compute_debit_count') diff --git a/addons/account_edi/models/account_edi_document.py b/addons/account_edi/models/account_edi_document.py index 2c5259e5a29..12e999a50d7 100644 --- a/addons/account_edi/models/account_edi_document.py +++ b/addons/account_edi/models/account_edi_document.py @@ -18,7 +18,7 @@ class AccountEdiDocument(models.Model): _description = 'Electronic Document for an account.move' # == Stored fields == - move_id = fields.Many2one('account.move', required=True, ondelete='cascade') + move_id = fields.Many2one('account.move', required=True, ondelete='cascade', index='btree_not_null') edi_format_id = fields.Many2one('account.edi.format', required=True) attachment_id = fields.Many2one( comodel_name='ir.attachment', diff --git a/addons/l10n_id_efaktur/models/account_move.py b/addons/l10n_id_efaktur/models/account_move.py index 37527a831e6..bd9d483084e 100644 --- a/addons/l10n_id_efaktur/models/account_move.py +++ b/addons/l10n_id_efaktur/models/account_move.py @@ -22,7 +22,7 @@ class AccountMove(models.Model): _inherit = "account.move" l10n_id_tax_number = fields.Char(string="Tax Number", copy=False) - l10n_id_replace_invoice_id = fields.Many2one('account.move', string="Replace Invoice", domain="['|', '&', '&', ('state', '=', 'posted'), ('partner_id', '=', partner_id), ('reversal_move_id', '!=', False), ('state', '=', 'cancel')]", copy=False) + l10n_id_replace_invoice_id = fields.Many2one('account.move', string="Replace Invoice", domain="['|', '&', '&', ('state', '=', 'posted'), ('partner_id', '=', partner_id), ('reversal_move_id', '!=', False), ('state', '=', 'cancel')]", copy=False, index='btree_not_null') l10n_id_attachment_id = fields.Many2one('ir.attachment', readonly=True, copy=False) l10n_id_csv_created = fields.Boolean('CSV Created', compute='_compute_csv_created', copy=False) l10n_id_kode_transaksi = fields.Selection([ diff --git a/addons/sale_timesheet/models/account.py b/addons/sale_timesheet/models/account.py index 41613c40200..ca77926915f 100644 --- a/addons/sale_timesheet/models/account.py +++ b/addons/sale_timesheet/models/account.py @@ -24,7 +24,7 @@ class AccountAnalyticLine(models.Model): timesheet_invoice_type = fields.Selection(TIMESHEET_INVOICE_TYPES, string="Billable Type", compute='_compute_timesheet_invoice_type', compute_sudo=True, store=True, readonly=True) commercial_partner_id = fields.Many2one('res.partner', compute="_compute_commercial_partner") - timesheet_invoice_id = fields.Many2one('account.move', string="Invoice", readonly=True, copy=False, help="Invoice created from the timesheet") + timesheet_invoice_id = fields.Many2one('account.move', string="Invoice", readonly=True, copy=False, help="Invoice created from the timesheet", index='btree_not_null') so_line = fields.Many2one(compute="_compute_so_line", store=True, readonly=False, help="Sales order item to which the time spent will be added in order to be invoiced to your customer. Remove the sales order item for the timesheet entry to be non-billable.") # we needed to store it only in order to be able to groupby in the portal diff --git a/addons/stock_landed_costs/models/stock_landed_cost.py b/addons/stock_landed_costs/models/stock_landed_cost.py index 7e01433dc2e..e1ae041b3cc 100644 --- a/addons/stock_landed_costs/models/stock_landed_cost.py +++ b/addons/stock_landed_costs/models/stock_landed_cost.py @@ -62,6 +62,7 @@ class StockLandedCost(models.Model): copy=False, readonly=True, tracking=True) account_move_id = fields.Many2one( 'account.move', 'Journal Entry', + index='btree_not_null', copy=False, readonly=True) account_journal_id = fields.Many2one( 'account.journal', 'Account Journal',