From c8ca71fc62b44dbb6ba9eebf9091dae87c723cf4 Mon Sep 17 00:00:00 2001 From: Nicolas Lempereur Date: Fri, 30 Sep 2016 11:43:30 +0200 Subject: [PATCH] [IMP] stock_account: stock_history slow read_group On a given instance with 600 000 stock.history, the query changed by this commit takes: - before this change => 32 minutes - after this change => 20 seconds opw-685747 --- addons/stock_account/wizard/stock_valuation_history.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/stock_account/wizard/stock_valuation_history.py b/addons/stock_account/wizard/stock_valuation_history.py index a7c31e2381a..65169c6dc13 100644 --- a/addons/stock_account/wizard/stock_valuation_history.py +++ b/addons/stock_account/wizard/stock_valuation_history.py @@ -56,11 +56,11 @@ class stock_history(osv.osv): for ids in group_lines.values(): for product_id in ids: line_ids.add(product_id) - line_ids = list(line_ids) lines_rec = {} if line_ids: - cr.execute('SELECT id, product_id, price_unit_on_quant, company_id, quantity FROM stock_history WHERE id in %s', (tuple(line_ids),)) - lines_rec = cr.dictfetchall() + move_ids = tuple(abs(line_id) for line_id in line_ids) + cr.execute('SELECT id, product_id, price_unit_on_quant, company_id, quantity FROM stock_history WHERE move_id in %s', (move_ids,)) + lines_rec = tuple(rec for rec in cr.dictfetchall() if rec['id'] in line_ids) lines_dict = dict((line['id'], line) for line in lines_rec) product_ids = list(set(line_rec['product_id'] for line_rec in lines_rec)) products_rec = self.pool['product.product'].read(cr, uid, product_ids, ['cost_method', 'id'], context=context)