From 71469758155dc1496b428b364a6a46cfcd13faeb Mon Sep 17 00:00:00 2001 From: Cyrus Robins Date: Mon, 5 Jun 2023 22:31:00 +0000 Subject: [PATCH] [FIX] stock_valuation_layer: add an index to stock_valuation_layer_id field to improve performance The index is being added because when processing a large PoS session, the method consume_specific_qty is called a large number of times if the product categories are set to automatic valuations. This method has the line: tmp_value += qty_taken_on_candidate * ((candidate.value + sum(candidate.stock_valuation_layer_ids.mapped('value'))) / candidate.quantity) This line, when running upwards of 20,000 times, can cause large performance issues, due to the stock_valuation_layer_ids being a related field on stock_valuation_layer_id. This causes a doubling of the number of sql queries which causes slowdowns in performance. Adding this index drastically improves the performance of the queries generated by this line of code. opw-3295560 closes odoo/odoo#126207 X-original-commit: 6f491e34d9355d1e45599fd0c881d7d721ea7d73 Signed-off-by: William Henrotin (whe) --- addons/stock_account/models/stock_valuation_layer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/stock_account/models/stock_valuation_layer.py b/addons/stock_account/models/stock_valuation_layer.py index aeb94bcaf5b..63b3b410fec 100644 --- a/addons/stock_account/models/stock_valuation_layer.py +++ b/addons/stock_account/models/stock_valuation_layer.py @@ -26,7 +26,7 @@ class StockValuationLayer(models.Model): remaining_qty = fields.Float(readonly=True, digits='Product Unit of Measure') remaining_value = fields.Monetary('Remaining Value', readonly=True) description = fields.Char('Description', readonly=True) - stock_valuation_layer_id = fields.Many2one('stock.valuation.layer', 'Linked To', readonly=True, check_company=True) + stock_valuation_layer_id = fields.Many2one('stock.valuation.layer', 'Linked To', readonly=True, check_company=True, index=True) stock_valuation_layer_ids = fields.One2many('stock.valuation.layer', 'stock_valuation_layer_id') stock_move_id = fields.Many2one('stock.move', 'Stock Move', readonly=True, check_company=True, index=True) account_move_id = fields.Many2one('account.move', 'Journal Entry', readonly=True, check_company=True, index="btree_not_null")