performance: Optimize max_message_id calculation.

We calculate `max_message_id` for the mobile client.

Our query now no longer joins to the Message table
and just grabs one value instead of fat objects.
This commit is contained in:
Steve Howell 2020-02-29 14:34:00 +00:00 committed by Tim Abbott
parent 6e4dcc714e
commit eb368c9c92
1 changed files with 7 additions and 4 deletions

View File

@ -53,7 +53,7 @@ from zerver.lib.user_groups import user_groups_in_realm_serialized
from zerver.lib.user_status import get_user_info_dict
from zerver.tornado.event_queue import request_event_queue, get_user_events
from zerver.models import (
Client, Message, Realm, UserProfile,
Client, Message, Realm, UserProfile, UserMessage,
get_user_profile_by_id, realm_filters_for_realm,
custom_profile_fields_for_realm, get_realm_domains,
get_default_stream_groups, CustomProfileField, Stream
@ -112,9 +112,12 @@ def fetch_initial_state_data(user_profile: UserProfile,
# The client should use get_messages() to fetch messages
# starting with the max_message_id. They will get messages
# newer than that ID via get_events()
messages = Message.objects.filter(usermessage__user_profile=user_profile).order_by('-id')[:1]
if messages:
state['max_message_id'] = messages[0].id
user_messages = UserMessage.objects \
.filter(user_profile=user_profile) \
.order_by('-message_id') \
.values('message_id')[:1]
if user_messages:
state['max_message_id'] = user_messages[0]['message_id']
else:
state['max_message_id'] = -1