From 7922370f1252a2482a8a77f91babb88145aff96c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 27 Apr 2016 12:08:19 +0200 Subject: [PATCH] [FIX] mail: fix performance issue with large channels When a user is subscribed to a channel with many messages, the discuss initial rpc /mail/client_action can become very slow (in production, the rpc took > 20s when subscribed to the Community mailing list). This is due to the query in _get_message_unread, counting the number of unread messages. This commit adds an index on the relation mail_channel_partner, this allows postgres to perform only index lookups, and speeds up the query by 2 order of magnitudes. --- addons/mail/models/mail_channel.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index ffd63e9d794..11de66a6a21 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -214,6 +214,11 @@ class Channel(models.Model): message = super(Channel, self.with_context(mail_create_nosubscribe=True)).message_post(body=body, subject=subject, message_type=message_type, subtype=subtype, parent_id=parent_id, attachments=attachments, content_subtype=content_subtype, **kwargs) return message + def init(self, cr): + cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('mail_channel_partner_seen_message_id_idx',)) + if not cr.fetchone(): + cr.execute('CREATE INDEX mail_channel_partner_seen_message_id_idx ON mail_channel_partner (channel_id,partner_id,seen_message_id)') + #------------------------------------------------------ # Instant Messaging API #------------------------------------------------------