[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) <odo@odoo.com>
This commit is contained in:
Victor Piryns (pivi)
2024-04-10 19:14:18 +00:00
parent 12e5296c1d
commit c0282c01ae
4 changed files with 5 additions and 5 deletions
+1 -1
View File
@@ -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)
@@ -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):
+1 -1
View File
@@ -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
@@ -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.'))