[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 <amoyaux@users.noreply.github.com>
This commit is contained in:
Arnold Moyaux
2021-01-11 11:46:29 +00:00
parent 31bf2fd446
commit 49be9e4904
+10 -1
View File
@@ -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: