[IMP] hr_work_entry_contract: Add index for performance improvement
From 7s to 2ms for this query with 2.6M records
explain (analyze, verbose, buffers)
SELECT "hr_work_entry"."id" FROM "hr_work_entry" WHERE
(
("hr_work_entry"."active" = true) AND
(
(
("hr_work_entry"."state" in ('validated', 'draft')) AND
("hr_work_entry"."contract_id" in (548259))
)
AND
(
(
(
(("hr_work_entry"."date_start" >= '2023-05-01 00:00:00') AND (
"hr_work_entry"."date_start" < '2023-05-31 23:59:59.999999')
) AND
("hr_work_entry"."date_stop" > '2023-05-31 23:59:59.999999')
)
OR
((("hr_work_entry"."date_start" < '2023-05-01 00:00:00') AND
("hr_work_entry"."date_stop" <= '2023-05-31 23:59:59.999999')) AND
("hr_work_entry"."date_stop" > '2023-05-01 00:00:00'))
) OR
(("hr_work_entry"."date_start" < '2023-05-01 00:00:00') AND
("hr_work_entry"."date_stop" > '2023-05-01 23:59:59.999999'))
)
)
)
ORDER BY COALESCE("hr_work_entry"."conflict", false) DESC,
"hr_work_entry"."state" ,"hr_work_entry"."date_start"
;
closes odoo/odoo#124108
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
@@ -18,6 +18,14 @@ class HrWorkEntry(models.Model):
|
||||
employee_id = fields.Many2one(domain=[('contract_ids.state', 'in', ('open', 'pending'))])
|
||||
work_entry_source = fields.Selection(related='contract_id.work_entry_source')
|
||||
|
||||
def init(self):
|
||||
# FROM 7s by query to 2ms (with 2.6 millions entries)
|
||||
self.env.cr.execute("""
|
||||
CREATE INDEX IF NOT EXISTS hr_work_entry_contract_date_start_stop_idx
|
||||
ON hr_work_entry(contract_id, date_start, date_stop)
|
||||
WHERE state in ('draft', 'validated');
|
||||
""")
|
||||
|
||||
def _init_column(self, column_name):
|
||||
if column_name != 'contract_id':
|
||||
super()._init_column(column_name)
|
||||
|
||||
Reference in New Issue
Block a user