Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

message: Move join to recipients outside of LIMIT, via CTE.

Alex Vandiver70ed1ee46c2b
diff --git a/zerver/lib/message.py b/zerver/lib/message.py
index 7b9ddf5a247..dcc270a3f4c 100644
--- a/zerver/lib/message.py
+++ b/zerver/lib/message.py
@@ -6,9 +6,10 @@ from typing import Any, TypedDict
from django.conf import settings
from django.db import connection
-from django.db.models import Exists, Max, OuterRef, QuerySet, Sum
+from django.db.models import Exists, F, Max, OuterRef, QuerySet, Sum
from django.utils.timezone import now as timezone_now
from django.utils.translation import gettext as _
+from django_cte import With
from psycopg2.sql import SQL
from analytics.lib.counts import COUNT_STATS
@@ -845,14 +846,17 @@ def get_raw_unread_data(
.exclude(
message__recipient_id__in=excluded_recipient_ids,
)
+ .annotate(
+ recipient_id=F("message__recipient_id"),
+ sender_id=F("message__sender_id"),
+ topic=F(MESSAGE__TOPIC),
+ )
.values(
"message_id",
- "message__sender_id",
- MESSAGE__TOPIC,
- "message__recipient_id",
- "message__recipient__type",
- "message__recipient__type_id",
+ "sender_id",
+ "topic",
"flags",
+ "recipient_id",
)
.order_by("-message_id")
)
@@ -867,8 +871,34 @@ def get_raw_unread_data(
where=[UserMessage.where_unread()],
)
- # Limit unread messages for performance reasons.
- user_msgs = list(user_msgs[:MAX_UNREAD_MESSAGES])
+ # Limit unread messages for performance reasons. We do this
+ # inside a CTE, such that the join to Recipients, below, can't be
+ # implied to remove rows, and thus allows a Nested Loop join,
+ # potentially memoized to reduce the number of Recipient lookups.
+ cte = With(user_msgs[:MAX_UNREAD_MESSAGES])
+
+ user_msgs = (
+ cte.join(Recipient, id=cte.col.recipient_id)
+ .with_cte(cte)
+ .annotate(
+ message_id=cte.col.message_id,
+ sender_id=cte.col.sender_id,
+ recipient_id=cte.col.recipient_id,
+ topic=cte.col.topic,
+ flags=cte.col.flags,
+ recipient__type=F("type"),
+ recipient__type_id=F("type_id"),
+ )
+ .values(
+ "message_id",
+ "sender_id",
+ "topic",
+ "flags",
+ "recipient_id",
+ "recipient__type",
+ "recipient__type_id",
+ )
+ )
rows = list(reversed(user_msgs))
return extract_unread_data_from_um_rows(rows, user_profile)
@@ -938,13 +968,13 @@ def extract_unread_data_from_um_rows(
for row in rows:
total_unreads += 1
message_id = row["message_id"]
- msg_type = row["message__recipient__type"]
- recipient_id = row["message__recipient_id"]
- sender_id = row["message__sender_id"]
+ msg_type = row["recipient__type"]
+ recipient_id = row["recipient_id"]
+ sender_id = row["sender_id"]
if msg_type == Recipient.STREAM:
- stream_id = row["message__recipient__type_id"]
- topic_name = row[MESSAGE__TOPIC]
+ stream_id = row["recipient__type_id"]
+ topic_name = row["topic"]
stream_dict[message_id] = dict(
stream_id=stream_id,
topic=topic_name,
@@ -954,7 +984,7 @@ def extract_unread_data_from_um_rows(
elif msg_type == Recipient.PERSONAL:
if sender_id == user_profile.id:
- other_user_id = row["message__recipient__type_id"]
+ other_user_id = row["recipient__type_id"]
else:
other_user_id = sender_id
@@ -980,8 +1010,8 @@ def extract_unread_data_from_um_rows(
mentions.add(message_id)
if is_stream_wildcard_mentioned or is_topic_wildcard_mentioned:
if msg_type == Recipient.STREAM:
- stream_id = row["message__recipient__type_id"]
- topic_name = row[MESSAGE__TOPIC]
+ stream_id = row["recipient__type_id"]
+ topic_name = row["topic"]
if not is_row_muted(stream_id, recipient_id, topic_name):
mentions.add(message_id)
else: # nocoverage # TODO: Test wildcard mentions in direct messages.
diff --git a/zerver/models/recipients.py b/zerver/models/recipients.py
index c49f5d774ff..01aad00b486 100644
--- a/zerver/models/recipients.py
+++ b/zerver/models/recipients.py
@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING
from django.db import models, transaction
from django.db.models import QuerySet
+from django_cte import CTEManager
from typing_extensions import override
from zerver.lib.display_recipient import get_display_recipient
@@ -52,6 +53,8 @@ class Recipient(models.Model):
# The type group direct messages.
DIRECT_MESSAGE_GROUP = 3
+ objects = CTEManager() # type: ignore[django-manager-missing] # django-stubs cannot resolve the custom CTEManager yet https://github.com/typeddjango/django-stubs/issues/1023
+
class Meta:
unique_together = ("type", "type_id")