From e0db9d42e6c011117636f085ebd2c7f1bde2e339 Mon Sep 17 00:00:00 2001 From: Bertrand Chenal Date: Fri, 19 Feb 2010 11:48:56 +0100 Subject: [PATCH] Optimisation for stock quantities computation: - Avoid to sort locations - Use UNION ALL instead of UNION --- product.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/product.py b/product.py index 665634747b..b9527d7f8f 100644 --- a/product.py +++ b/product.py @@ -363,7 +363,7 @@ class Product(ModelSQL, ModelView): if with_childs: query, args = location_obj.search(cursor, user, [ ('parent', 'child_of', location_ids), - ], context=context, query_string=True) + ], context=context, query_string=True, order=[]) where_clause = " IN (" + query + ") " where_vals = args else: @@ -405,6 +405,10 @@ class Product(ModelSQL, ModelView): dest_clause_from = dest_clause_to ="" dest_vals = [] + # The main select clause is a union between two similar + # subqueries. One that sums incoming moves towards locations + # and on that sums outgoing moves. UNION ALL is used because + # we already know that there will be no duplicates. select_clause = \ "SELECT location, product, uom, sum(quantity) AS quantity "\ "FROM ( "\ @@ -414,7 +418,7 @@ class Product(ModelSQL, ModelView): "WHERE (%s) " \ "AND to_location %s "\ "GROUP BY to_location, product ,uom "\ - "UNION "\ + "UNION ALL "\ "SELECT from_location AS location, product, uom, "\ "-sum(quantity) AS quantity "\ "FROM stock_move " + product_template_join + \