From c5b879bfd29a972d8b8f8fb95038694ac8bdc485 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Tue, 23 Feb 2016 15:44:04 +0100 Subject: [PATCH] [FIX] crm.opportunity.report: query performance Using a subselect is more efficient than constructing a complete temporary table of all messages, and then joining them. This way only messages or relevant opportunities are used, as most of the time the report is accessed for a specific subset of opportunities (by date, salesman, stage, etc.). The query planner can then use the dedicated index to quickly access the corresponding messages. On a sample database with 1 million crm.lead entries and 10 million mail.message entries this brings the time for a typical read_group call on crm.opportunity.report down from 300s to 500ms (600x faster) --- addons/crm/report/crm_opportunity_report.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/addons/crm/report/crm_opportunity_report.py b/addons/crm/report/crm_opportunity_report.py index c690e79799d..b612d3ad349 100644 --- a/addons/crm/report/crm_opportunity_report.py +++ b/addons/crm/report/crm_opportunity_report.py @@ -68,7 +68,9 @@ class crm_opportunity_report(osv.Model): c.company_id, c.priority, c.team_id, - activity.nbr_activities, + (SELECT COUNT(*) + FROM mail_message m + WHERE m.model = 'crm.lead' and m.res_id = c.id) as nbr_activity, c.active, c.campaign_id, c.source_id, @@ -85,14 +87,7 @@ class crm_opportunity_report(osv.Model): c.date_conversion as date_conversion FROM "crm_lead" c - LEFT JOIN ( - SELECT m.res_id, COUNT(*) nbr_activities - FROM "mail_message" m - WHERE m.model = 'crm.lead' - GROUP BY m.res_id ) activity - ON - (activity.res_id = c.id) LEFT JOIN "crm_stage" stage ON stage.id = c.stage_id - GROUP BY c.id, activity.nbr_activities, stage.name + GROUP BY c.id, stage.name )""")