Files
odoo_source/addons/account_debit_note/models/account_move.py
T
Habib (ayh) 39eaf87b1b [IMP] account,*: performance on Foreign Keys
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) <wan@odoo.com>
Signed-off-by: Habib Ayob (ayh) <ayh@odoo.com>
2023-12-09 13:29:18 +00:00

35 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
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, 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')
@api.depends('debit_note_ids')
def _compute_debit_count(self):
debit_data = self.env['account.move']._read_group([('debit_origin_id', 'in', self.ids)],
['debit_origin_id'], ['__count'])
data_map = {debit_origin.id: count for debit_origin, count in debit_data}
for inv in self:
inv.debit_note_count = data_map.get(inv.id, 0.0)
def action_view_debit_notes(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': _('Debit Notes'),
'res_model': 'account.move',
'view_mode': 'tree,form',
'domain': [('debit_origin_id', '=', self.id)],
}
def action_debit_note(self):
action = self.env.ref('account_debit_note.action_view_account_move_debit').read()[0]
return action