Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
recipients: Reduce query count for new DirectMessageGroup.
bedo8e6797d9abdc
diff --git a/zerver/models/recipients.py b/zerver/models/recipients.pyindex 1ef1ca13593..9e03180fa54 100644--- a/zerver/models/recipients.py+++ b/zerver/models/recipients.py@@ -2,8 +2,9 @@ import hashlibfrom collections import defaultdictfrom typing import TYPE_CHECKING-from django.db import models, transaction+from django.db import connection, models, transactionfrom django.db.models import QuerySet+from psycopg2.sql import SQL, Identifierfrom typing_extensions import overridefrom zerver.lib.display_recipient import get_display_recipient@@ -156,23 +157,69 @@ def get_or_create_direct_message_group(id_list: list[int]) -> DirectMessageGrouphuddle_hash=direct_message_group_hash,group_size=len(id_list),)- if created:- recipient = Recipient.objects.create(- type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP- )- direct_message_group.recipient = recipient- direct_message_group.save(update_fields=["recipient"])- subs_to_create = [- Subscription(- recipient=recipient,- user_profile_id=user_profile_id,- is_user_active=is_active,++ if not created:+ return direct_message_group++ recipient = Recipient.objects.create(+ type_id=direct_message_group.id, type=Recipient.DIRECT_MESSAGE_GROUP+ )++ # The equivalent effect of doing+ # direct_message_group.save(update_fields=["recipient"]) is handled+ # in the following raw query via UPDATE to save us a query.+ direct_message_group.recipient = recipient++ # In a single raw query, we do 2 required things after creating+ # a new DirectMessageGroup object:+ # 1. Update the recipient field to reflect the above assignment in db.+ # 2. Bulk create subscriptions for the participants.+ query = SQL(+ """+ UPDATE {dmg_table}+ SET recipient_id = %(recipient_id)s+ WHERE {dmg_table}.id = %(dmg_id)s;++ INSERT INTO {subscription_table}+ (+ user_profile_id,+ is_user_active,+ recipient_id,+ active,+ is_muted,+ color,+ pin_to_top)- for user_profile_id, is_active in UserProfile.objects.filter(id__in=id_list)- .distinct("id")- .values_list("id", "is_active")- ]- Subscription.objects.bulk_create(subs_to_create)+ SELECT+ {user_profile_table}.id,+ {user_profile_table}.is_active,+ %(recipient_id)s,+ %(active)s,+ %(is_muted)s,+ %(color)s,+ %(pin_to_top)s+ FROM {user_profile_table}+ WHERE {user_profile_table}.id = ANY(%(participant_ids)s);+ """+ ).format(+ dmg_table=Identifier(DirectMessageGroup._meta.db_table),+ subscription_table=Identifier(Subscription._meta.db_table),+ user_profile_table=Identifier(UserProfile._meta.db_table),+ )+ with connection.cursor() as cursor:+ cursor.execute(+ query,+ {+ "recipient_id": recipient.id,+ "dmg_id": direct_message_group.id,+ "active": Subscription._meta.get_field("active").get_default(),+ "is_muted": Subscription._meta.get_field("is_muted").get_default(),+ "color": Subscription._meta.get_field("color").get_default(),+ "pin_to_top": Subscription._meta.get_field("pin_to_top").get_default(),+ "participant_ids": id_list,+ },+ )+return direct_message_groupdiff --git a/zerver/tests/test_channel_fetch.py b/zerver/tests/test_channel_fetch.pyindex ba394ce8dae..4bf69240627 100644--- a/zerver/tests/test_channel_fetch.py+++ b/zerver/tests/test_channel_fetch.py@@ -883,7 +883,7 @@ class GetSubscribersTest(ZulipTestCase):polonius.id,]- with self.assert_database_query_count(67):+ with self.assert_database_query_count(61):self.subscribe_via_post(self.user_profile,stream_names,diff --git a/zerver/tests/test_example.py b/zerver/tests/test_example.pyindex 2be2e167668..d13d3d41859 100644--- a/zerver/tests/test_example.py+++ b/zerver/tests/test_example.py@@ -365,7 +365,7 @@ class TestQueryCounts(ZulipTestCase):cordelia = self.example_user("cordelia")# when direct message group doesn't exist and should be created as part of the flow- with self.assert_database_query_count(22):+ with self.assert_database_query_count(20):self.send_personal_message(from_user=hamlet,to_user=cordelia,diff --git a/zerver/tests/test_message_send.py b/zerver/tests/test_message_send.pyindex dc04dc9c1f7..29f8a72541b 100644--- a/zerver/tests/test_message_send.py+++ b/zerver/tests/test_message_send.py@@ -3010,7 +3010,7 @@ class PersonalMessageSendTest(ZulipTestCase):prospero = self.example_user("prospero")# A normal user sends a personal message.- with self.assert_database_query_count(22):+ with self.assert_database_query_count(20):self.send_personal_message(hamlet, cordelia)# Give guests limited user access.@@ -3021,12 +3021,12 @@ class PersonalMessageSendTest(ZulipTestCase):# A guest with limited user access sends a personal message# to another accessible user.- with self.assert_database_query_count(23):+ with self.assert_database_query_count(21):self.send_personal_message(polonius, hamlet)# A guest with limited user access sends a personal message# to themself.- with self.assert_database_query_count(19):+ with self.assert_database_query_count(17):self.send_personal_message(polonius, polonius)# A guest with limited user access sends a personal message@@ -3059,7 +3059,7 @@ class PersonalMessageSendTest(ZulipTestCase):# A normal user sends the first message# to a new DirectMessageGroup.- with self.assert_database_query_count(26):+ with self.assert_database_query_count(24):self.send_group_direct_message(iago, recipients)# A normal user sends a message@@ -3078,7 +3078,7 @@ class PersonalMessageSendTest(ZulipTestCase):# A guest with limited user access sends the first message# to a new DirectMessageGroup.- with self.assert_database_query_count(28):+ with self.assert_database_query_count(26):self.send_group_direct_message(polonius, recipients)# A guest with limited user access sends a message@@ -3176,7 +3176,7 @@ class PersonalMessageSendTest(ZulipTestCase):acting_user=None,)othello = self.example_user("othello")- with self.assert_database_query_count(21):+ with self.assert_database_query_count(19):self.send_personal_message(user_profile, othello)def test_direct_message_permission_group_setting(self) -> None:@@ -3204,7 +3204,7 @@ class PersonalMessageSendTest(ZulipTestCase):acting_user=None,)# Tests if the user is allowed to send to administrators.- with self.assert_database_query_count(22):+ with self.assert_database_query_count(20):self.send_personal_message(user_profile, admin)self.send_personal_message(admin, user_profile)# Tests if we can send messages to self irrespective of the value of the setting.@@ -3222,7 +3222,7 @@ class PersonalMessageSendTest(ZulipTestCase):# We can send to this direct message group as it has administrator as one of the# recipient.- with self.assert_database_query_count(22):+ with self.assert_database_query_count(20):self.send_group_direct_message(user_profile, direct_message_group)self.send_group_direct_message(admin, direct_message_group)diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.pyindex 619c43c728b..205d9e795d3 100644--- a/zerver/tests/test_signup.py+++ b/zerver/tests/test_signup.py@@ -1068,7 +1068,7 @@ class LoginTest(ZulipTestCase):# to sending messages, such as getting the welcome bot, looking up# the alert words for a realm, etc.with (- self.assert_database_query_count(99),+ self.assert_database_query_count(97),self.assert_memcached_count(19),self.captureOnCommitCallbacks(execute=True),):diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 5fea269ff9a..3a25c7c2187 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -1154,7 +1154,7 @@ class QueryCountTest(ZulipTestCase):prereg_user = PreregistrationUser.objects.get(email="[email protected]")with (- self.assert_database_query_count(95),+ self.assert_database_query_count(91),self.assert_memcached_count(24),self.capture_send_event_calls(expected_num_events=11) as events,):