diff options
| author | Raphael Collet <rco@odoo.com> | 2020-05-05 12:56:02 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2020-05-20 07:56:19 +0000 |
| commit | 0fa101a8bf13671162bda91deb98bd0c7ec76f3f (patch) | |
| tree | 3f4b0556018540c1915a5e7ddfb90a1ae41b30c6 /odoo | |
| parent | 020536142cf9fe9124675ef5c7e46eaf41b9c528 (diff) | |
[IMP] core: avoid useless LEFT JOIN in `search_count` queries
Before this commit, a `search_count` was preparing the ORDER BY clause
even when not using it. The ORDER BY clause generation can introduce
some JOINs in the query, which are useless when the ORDER BY clause is
not used, like in the case of `search_count`.
Diffstat (limited to 'odoo')
| -rw-r--r-- | odoo/addons/base/tests/test_expression.py | 6 | ||||
| -rw-r--r-- | odoo/models.py | 9 |
2 files changed, 5 insertions, 10 deletions
diff --git a/odoo/addons/base/tests/test_expression.py b/odoo/addons/base/tests/test_expression.py index 973574655ec..4e0e5fc0be9 100644 --- a/odoo/addons/base/tests/test_expression.py +++ b/odoo/addons/base/tests/test_expression.py @@ -1032,12 +1032,6 @@ class TestQueries(TransactionCase): with self.assertQueries([''' SELECT COUNT(1) FROM "res_partner_title" - LEFT JOIN "ir_translation" AS "res_partner_title__name" ON - ("res_partner_title"."id" = "res_partner_title__name"."res_id" - AND "res_partner_title__name"."type" = 'model' - AND "res_partner_title__name"."name" = %s - AND "res_partner_title__name"."lang" = %s - AND "res_partner_title__name"."value" != %s) WHERE ("res_partner_title"."id" = %s) ''']): Model.search_count([('id', '=', 1)]) diff --git a/odoo/models.py b/odoo/models.py index 43ac7929843..bbdb91b0369 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -4376,19 +4376,20 @@ Record ids: %(records)s query = self._where_calc(args) self._apply_ir_rules(query, 'read') - order_by = self._generate_order_by(order, query) - from_clause, where_clause, where_clause_params = query.get_sql() - - where_str = where_clause and (" WHERE %s" % where_clause) or '' if count: # Ignore order, limit and offset when just counting, they don't make sense and could # hurt performance + from_clause, where_clause, where_clause_params = query.get_sql() + where_str = where_clause and (" WHERE %s" % where_clause) or '' query_str = 'SELECT count(1) FROM ' + from_clause + where_str self._cr.execute(query_str, where_clause_params) res = self._cr.fetchone() return res[0] + order_by = self._generate_order_by(order, query) + from_clause, where_clause, where_clause_params = query.get_sql() + where_str = where_clause and (" WHERE %s" % where_clause) or '' limit_str = limit and ' limit %d' % limit or '' offset_str = offset and ' offset %d' % offset or '' query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str |
