[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`.
This commit is contained in:
Raphael Collet
2020-05-20 07:56:19 +00:00
parent 020536142c
commit 0fa101a8bf
2 changed files with 5 additions and 10 deletions
@@ -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)])
+5 -4
View File
@@ -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