From f22cb07c7680912160952efe5ba117c4c63a4aca Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Sat, 17 Aug 2019 20:25:18 +0200 Subject: [PATCH] [FIX] account_financial_report: Avoid slow vacuum due to ondelete=cascade `report_journal_ledger` is auto-vacuumed as any transient model, but has some ondelete="cascade" constraints that auto-remove subtables when a record is removed, doing this operation very slow when selecting these sub-records. Letting default ondelete="set null" would result in same performance bottleneck, as the select on sub-table is performed the same for setting "null" value on them. As a solution, and for avoiding a costly index operation, we delete by SQL sub-tables rows in advance. A bit of extra logic has been added for avoiding to remove that records if it's not the turn of vacuum the parent table. --- account_financial_report/readme/CONTRIBUTORS.rst | 4 ++++ account_financial_report/report/journal_ledger.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/account_financial_report/readme/CONTRIBUTORS.rst b/account_financial_report/readme/CONTRIBUTORS.rst index 7ea48d64..eecf5bb8 100644 --- a/account_financial_report/readme/CONTRIBUTORS.rst +++ b/account_financial_report/readme/CONTRIBUTORS.rst @@ -16,6 +16,10 @@ * Mihai Fekete * Miquel Raïch * Isaac Gallart +* `Tecnativa `__: + + * Pedro M. Baeza + * Sergio Teruel Much of the work in this module was done at a sprint in Sorrento, Italy in April 2016. diff --git a/account_financial_report/report/journal_ledger.py b/account_financial_report/report/journal_ledger.py index 352b4c49..b72ce5bf 100644 --- a/account_financial_report/report/journal_ledger.py +++ b/account_financial_report/report/journal_ledger.py @@ -612,6 +612,19 @@ class ReportJournalLedger(models.TransientModel): def get_html(self, given_context=None): return self._get_html() + @api.model + def _transient_vacuum(self, force=False): + """Remove journal ledger subtables first for avoiding a costly + ondelete operation. + """ + # Next 3 lines adapted from super method for mimicking behavior + cls = type(self) + if not force and (cls._transient_check_count < 21): + return True # no vacuum cleaning this time + self.env.cr.execute("DELETE FROM report_journal_ledger_move_line") + self.env.cr.execute("DELETE FROM report_journal_ledger_move") + return super(ReportJournalLedger, self)._transient_vacuum(force=force) + class ReportJournalLedgerJournal(models.TransientModel):