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.

narrow: Reduce find_first_unread_anchor query count.

Shubham Padiad412485b053f
diff --git a/zerver/lib/narrow.py b/zerver/lib/narrow.py
index a8fb4b968e2..829dcd237ac 100644
--- a/zerver/lib/narrow.py
+++ b/zerver/lib/narrow.py
@@ -1140,31 +1140,39 @@ def find_first_unread_anchor(
sa_conn: Connection,
user_profile: UserProfile | None,
narrow: list[NarrowParameter] | None,
+ query: Select,
+ is_dm_narrow: bool,
+ inner_msg_id_col: ColumnElement[Integer],
+ need_user_message: bool,
) -> int:
# For anonymous web users, all messages are treated as read, and so
# always return LARGER_THAN_MAX_MESSAGE_ID.
if user_profile is None:
return LARGER_THAN_MAX_MESSAGE_ID
- # We always need UserMessage in our query, because it has the unread
- # flag for the user.
- need_user_message = True
-
- query, inner_msg_id_col = get_base_query_for_search(
- realm_id=user_profile.realm_id,
- user_profile=user_profile,
- need_user_message=need_user_message,
- )
- query = query.add_columns(column("flags", Integer))
+ # Looking at the name get_base_query_for_search, one would think that
+ # we can just rebuild the query again with need_user_message set to True
+ # regardless of the existing value of need_user_message. But,
+ # get_base_query_for_search executes 1 query which is getting recursive
+ # user group memberships.
+ if not need_user_message:
+ # We always need UserMessage in our query, because it has the unread
+ # flag for the user.
+ query, inner_msg_id_col = get_base_query_for_search(
+ realm_id=user_profile.realm_id,
+ user_profile=user_profile,
+ need_user_message=True,
+ )
+ query = query.add_columns(column("flags", Integer))
- query, _is_search, is_dm_narrow = add_narrow_conditions(
- user_profile=user_profile,
- inner_msg_id_col=inner_msg_id_col,
- query=query,
- narrow=narrow,
- is_web_public_query=False,
- realm=user_profile.realm,
- )
+ query, _is_search, is_dm_narrow = add_narrow_conditions(
+ user_profile=user_profile,
+ inner_msg_id_col=inner_msg_id_col,
+ query=query,
+ narrow=narrow,
+ is_web_public_query=False,
+ realm=user_profile.realm,
+ )
condition = column("flags", Integer).op("&")(UserMessage.flags.read.mask) == 0
@@ -1444,7 +1452,7 @@ def fetch_messages(
if need_user_message:
query = query.add_columns(column("flags", Integer))
- query, is_search, _is_dm_narrow = add_narrow_conditions(
+ query, is_search, is_dm_narrow = add_narrow_conditions(
user_profile=user_profile,
inner_msg_id_col=inner_msg_id_col,
query=query,
@@ -1466,6 +1474,10 @@ def fetch_messages(
sa_conn,
user_profile,
narrow,
+ query,
+ is_dm_narrow,
+ inner_msg_id_col,
+ need_user_message,
)
anchored_to_left = anchor == 0
diff --git a/zerver/tests/test_message_fetch.py b/zerver/tests/test_message_fetch.py
index 5200a3f47b4..78b2cca302a 100644
--- a/zerver/tests/test_message_fetch.py
+++ b/zerver/tests/test_message_fetch.py
@@ -36,8 +36,10 @@ from zerver.lib.narrow import (
BadNarrowOperatorError,
NarrowBuilder,
NarrowParameter,
+ add_narrow_conditions,
exclude_muting_conditions,
find_first_unread_anchor,
+ get_base_query_for_search,
is_spectator_compatible,
ok_to_include_history,
post_process_limited_query,
@@ -4264,25 +4266,57 @@ class GetOldMessagesTest(ZulipTestCase):
user_profile = hamlet
- with queries_captured() as queries, get_sqlalchemy_connection() as sa_conn:
- anchor = find_first_unread_anchor(
- sa_conn=sa_conn,
+ def test_find_first_unread_anchor(
+ narrow: list[NarrowParameter], need_user_message: bool
+ ) -> tuple[int, list[Any]]:
+ query, inner_msg_id_col = get_base_query_for_search(
+ realm_id=user_profile.realm_id,
user_profile=user_profile,
- narrow=[],
+ need_user_message=need_user_message,
)
+ query = query.add_columns(column("flags", Integer))
+ query, _is_search, is_dm_narrow = add_narrow_conditions(
+ user_profile=user_profile,
+ inner_msg_id_col=inner_msg_id_col,
+ query=query,
+ narrow=narrow,
+ realm=user_profile.realm,
+ is_web_public_query=False,
+ )
+
+ with queries_captured() as queries, get_sqlalchemy_connection() as sa_conn:
+ anchor = find_first_unread_anchor(
+ sa_conn=sa_conn,
+ user_profile=user_profile,
+ narrow=narrow,
+ query=query,
+ is_dm_narrow=is_dm_narrow,
+ inner_msg_id_col=inner_msg_id_col,
+ need_user_message=need_user_message,
+ )
+ return anchor, queries
+
+ anchor, queries = test_find_first_unread_anchor([], need_user_message=False)
self.assert_length(queries, 4)
self.assertEqual(anchor, first_message_id)
+ # If need_user_message is set to True, we don't need to call
+ # get_base_query_for_search inside find_first_unread_anchor.
+ # This saves us an extra call that get_base_query_for_search
+ # makes to get_recursive_membership_groups in case of
+ # need_user_message being True.
+ anchor, queries = test_find_first_unread_anchor([], need_user_message=True)
+ self.assert_length(queries, 3)
+ self.assertEqual(anchor, first_message_id)
+
# Looking for the first-unread in DMs leaves off the muted
# topics queries and limits
- with queries_captured() as queries, get_sqlalchemy_connection() as sa_conn:
- anchor = find_first_unread_anchor(
- sa_conn=sa_conn,
- user_profile=user_profile,
- narrow=[NarrowParameter(operator="is", operand="dm")],
- )
- self.assert_length(queries, 2)
- self.assertTrue("muted" not in queries[1].sql)
+ anchor, queries = test_find_first_unread_anchor(
+ [NarrowParameter(operator="is", operand="dm")],
+ need_user_message=True,
+ )
+ self.assert_length(queries, 1)
+ self.assertTrue("muted" not in queries[0].sql)
self.assertEqual(anchor, dm_message_id)
# With the same data setup, we now want to test that a reasonable