From c0282c01ae996dea2913b3522f3741584b2b5760 Mon Sep 17 00:00:00 2001 From: "Victor Piryns (pivi)" Date: Thu, 4 Apr 2024 11:08:57 +0200 Subject: [PATCH] [PERF] analytic: add missing indexes Description: Add indexes affecting queries related to `account.analytic.line`. Based on heuristics like domains in `search`/`read_group` that are not really selective, or being an inverse of One2many. Some of these searches are on code-paths that are frequent, like creation of an SO. The goal is to reduce the number of Seq.Scan or non-selective Index Scan. Reference: task-3724844 closes odoo/odoo#160472 Signed-off-by: Olivier Dony (odo) --- addons/hr_timesheet/models/hr_timesheet.py | 2 +- addons/project_timesheet_holidays/models/account_analytic.py | 4 ++-- addons/sale/models/analytic.py | 2 +- addons/sale_timesheet/wizard/project_create_sale_order.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/addons/hr_timesheet/models/hr_timesheet.py b/addons/hr_timesheet/models/hr_timesheet.py index bcf5ef728c0..202787b68c5 100644 --- a/addons/hr_timesheet/models/hr_timesheet.py +++ b/addons/hr_timesheet/models/hr_timesheet.py @@ -60,7 +60,7 @@ class AccountAnalyticLine(models.Model): compute='_compute_project_id', store=True, readonly=False) user_id = fields.Many2one(compute='_compute_user_id', store=True, readonly=False) employee_id = fields.Many2one('hr.employee', "Employee", domain=_domain_employee_id, context={'active_test': False}, - help="Define an 'hourly cost' on the employee to track the cost of their time.") + index=True, help="Define an 'hourly cost' on the employee to track the cost of their time.") job_title = fields.Char(related='employee_id.job_title') department_id = fields.Many2one('hr.department', "Department", compute='_compute_department_id', store=True, compute_sudo=True) manager_id = fields.Many2one('hr.employee', "Manager", related='employee_id.parent_id', store=True) diff --git a/addons/project_timesheet_holidays/models/account_analytic.py b/addons/project_timesheet_holidays/models/account_analytic.py index 48d7b3f4d2c..edfc4c8125d 100644 --- a/addons/project_timesheet_holidays/models/account_analytic.py +++ b/addons/project_timesheet_holidays/models/account_analytic.py @@ -9,8 +9,8 @@ from odoo.osv import expression class AccountAnalyticLine(models.Model): _inherit = 'account.analytic.line' - holiday_id = fields.Many2one("hr.leave", string='Time Off Request', copy=False) - global_leave_id = fields.Many2one("resource.calendar.leaves", string="Global Time Off", ondelete='cascade') + holiday_id = fields.Many2one("hr.leave", string='Time Off Request', copy=False, index='btree_not_null') + global_leave_id = fields.Many2one("resource.calendar.leaves", string="Global Time Off", index='btree_not_null', ondelete='cascade') task_id = fields.Many2one(domain="[('allow_timesheets', '=', True), ('project_id', '=?', project_id), ('is_timeoff_task', '=', False)]") def _get_redirect_action(self): diff --git a/addons/sale/models/analytic.py b/addons/sale/models/analytic.py index 5888e183ef9..8afcac8b00d 100644 --- a/addons/sale/models/analytic.py +++ b/addons/sale/models/analytic.py @@ -9,7 +9,7 @@ class AccountAnalyticLine(models.Model): # [XBO] TODO: remove me in master allowed_so_line_ids = fields.Many2many('sale.order.line', compute='_compute_allowed_so_line_ids') - so_line = fields.Many2one('sale.order.line', string='Sales Order Item', domain=[('qty_delivered_method', '=', 'analytic')]) + so_line = fields.Many2one('sale.order.line', string='Sales Order Item', domain=[('qty_delivered_method', '=', 'analytic')], index='btree_not_null') def _default_sale_line_domain(self): """ This is only used for delivered quantity of SO line based on analytic line, and timesheet diff --git a/addons/sale_timesheet/wizard/project_create_sale_order.py b/addons/sale_timesheet/wizard/project_create_sale_order.py index 9f9e6fb7bf1..d5ea6c924da 100644 --- a/addons/sale_timesheet/wizard/project_create_sale_order.py +++ b/addons/sale_timesheet/wizard/project_create_sale_order.py @@ -91,7 +91,7 @@ class ProjectCreateSalesOrder(models.TransientModel): raise UserError(_('The Sales Order cannot be created because you did not enter some employees that entered timesheets on this project. Please list all the relevant employees before creating the Sales Order.\nMissing employee(s): %s', ', '.join(missing_meployees.mapped('name')))) # check here if timesheet already linked to SO line - timesheet_with_so_line = self.env['account.analytic.line'].search_count([('task_id', 'in', self.project_id.tasks.ids), ('so_line', '!=', False)]) + timesheet_with_so_line = self.env['account.analytic.line'].search_count([('task_id', 'in', self.project_id.tasks.ids), ('so_line', '!=', False)], limit=1) if timesheet_with_so_line: raise UserError(_('The sales order cannot be created because some timesheets of this project are already linked to another sales order.'))