[FIX] base: simplify computation of commercial_partner_id
The computation was using an SQL query to speed it up, but the recent changes in the ORM have made this optimization useless. We have benchmarked an import of 10000 partners linked to the same parent, in a database with Sales and Stock installed. The import is slightly faster without the SQL query (7 min vs 8 min). closes odoo/odoo#36768 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
@@ -273,32 +273,8 @@ class Partner(models.Model):
|
||||
|
||||
@api.depends('is_company', 'parent_id.commercial_partner_id')
|
||||
def _compute_commercial_partner(self):
|
||||
self.env.cr.execute("""
|
||||
WITH RECURSIVE cpid(id, parent_id, commercial_partner_id, final) AS (
|
||||
SELECT
|
||||
id, parent_id, id,
|
||||
(coalesce(is_company, false) OR parent_id IS NULL) as final
|
||||
FROM res_partner
|
||||
WHERE id = ANY(%s)
|
||||
UNION
|
||||
SELECT
|
||||
cpid.id, p.parent_id, p.id,
|
||||
(coalesce(is_company, false) OR p.parent_id IS NULL) as final
|
||||
FROM res_partner p
|
||||
JOIN cpid ON (cpid.parent_id = p.id)
|
||||
WHERE NOT cpid.final
|
||||
)
|
||||
SELECT cpid.id, cpid.commercial_partner_id
|
||||
FROM cpid
|
||||
WHERE final AND id = ANY(%s);
|
||||
""", [self.ids, self.ids])
|
||||
|
||||
d = dict(self.env.cr.fetchall())
|
||||
for partner in self:
|
||||
fetched = d.get(partner.id)
|
||||
if fetched is not None:
|
||||
partner.commercial_partner_id = fetched
|
||||
elif partner.is_company or not partner.parent_id:
|
||||
if partner.is_company or not partner.parent_id:
|
||||
partner.commercial_partner_id = partner
|
||||
else:
|
||||
partner.commercial_partner_id = partner.parent_id.commercial_partner_id
|
||||
|
||||
Reference in New Issue
Block a user