From 49be9e4904e5b5b39627ad13681a883a094e768d Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Fri, 31 Jul 2020 15:35:10 +0000 Subject: [PATCH] [FIX] stock: Add index for free reservation Free reservation is called each time an extra quantity is used on a move (and for each immediate transfer). It means that the function is called many times on a database with an inventoy activity. Also _free_reservation method do a search on fields that are not indexed, and is slow on large database. This index is specific to _free_reservation. On a live database with an important stock, the queries dropped from 265ms to 1.5ms. closes odoo/odoo#64327 X-original-commit: 74ea87d0a354b7414afb76d98ed39ecde45a8fb1 Signed-off-by: Arnold Moyaux --- addons/stock/models/stock_move_line.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index 4ebbccecb2d..b44f89fea14 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -3,7 +3,7 @@ from collections import Counter -from odoo import _, api, fields, models +from odoo import _, api, fields, tools, models from odoo.exceptions import UserError, ValidationError from odoo.tools import OrderedSet from odoo.tools.float_utils import float_compare, float_is_zero, float_round @@ -175,6 +175,15 @@ class StockMoveLine(models.Model): res['warning'] = {'title': _('Warning'), 'message': message} return res + def init(self): + if not tools.index_exists(self._cr, 'stock_move_line_free_reservation_index'): + self._cr.execute(""" + CREATE INDEX stock_move_line_free_reservation_index + ON + stock_move_line (id, company_id, product_id, lot_id, location_id, owner_id, package_id) + WHERE + (state IS NULL OR state NOT IN ('cancel', 'done')) AND product_qty > 0""") + @api.model_create_multi def create(self, vals_list): for vals in vals_list: