[IMP] account_report_company: performance on partner name_search

The field display_name is present in account_report_company but not in base
on the res.partner (has been added in v8 in base).
Create a hook method to keep using the slow CASE in base and switch to the
faster display_name when installing account_report_company.
This commit is contained in:
Matthieu Dietrich 2015-07-01 15:47:31 +02:00 committed by Martin Trigaux
parent 2326bb7a54
commit f2c807d99d
2 changed files with 20 additions and 12 deletions

View File

@ -43,6 +43,11 @@ class res_partner(osv.Model):
'display_name': fields.function(_display_name, type='char', string='Name', store=_display_name_store_triggers, select=1),
}
def _get_display_name(self, unaccent):
# use stored display name for better performances
return unaccent('res_partner.display_name')
class account_invoice(osv.Model):
_inherit = 'account.invoice'

View File

@ -622,6 +622,20 @@ class res_partner(osv.osv, format_address):
return super(res_partner, self)._search(cr, user, args, offset=offset, limit=limit, order=order, context=context,
count=count, access_rights_uid=access_rights_uid)
def _get_display_name(self, unaccent):
# TODO: simplify this in trunk with `display_name`, once it is stored
# Perf note: a CTE expression (WITH ...) seems to have an even higher cost
# than this query with duplicated CASE expressions. The bulk of
# the cost is the ORDER BY, and it is inevitable if we want
# relevant results for the next step, otherwise we'd return
# a random selection of `limit` results.
return """CASE WHEN company.id IS NULL OR res_partner.is_company
THEN {partner_name}
ELSE {company_name} || ', ' || {partner_name}
END""".format(partner_name=unaccent('res_partner.name'),
company_name=unaccent('company.name'))
def name_search(self, cr, uid, name, args=None, operator='ilike', context=None, limit=100):
if not args:
args = []
@ -642,18 +656,7 @@ class res_partner(osv.osv, format_address):
unaccent = get_unaccent_wrapper(cr)
# TODO: simplify this in trunk with `display_name`, once it is stored
# Perf note: a CTE expression (WITH ...) seems to have an even higher cost
# than this query with duplicated CASE expressions. The bulk of
# the cost is the ORDER BY, and it is inevitable if we want
# relevant results for the next step, otherwise we'd return
# a random selection of `limit` results.
display_name = """CASE WHEN company.id IS NULL OR res_partner.is_company
THEN {partner_name}
ELSE {company_name} || ', ' || {partner_name}
END""".format(partner_name=unaccent('res_partner.name'),
company_name=unaccent('company.name'))
display_name = self._get_display_name(unaccent)
query = """SELECT res_partner.id
FROM res_partner