messages: Stop joining through every table.

Calling `.select_related()` with no arguments joins through every
possible table, recursively.  In this case, this currently produces a
query which joins through forty-three tables.

This is rather inefficient, particularly for what is a very common
call which should be very fast.

No callsite depends on having prefetched any joined table on the
object; drop all of the joins.
This commit is contained in:
Alex Vandiver 2024-03-22 05:27:40 +00:00 committed by Tim Abbott
parent a5cf0ec526
commit 6ace34c374
1 changed files with 1 additions and 3 deletions

View File

@ -622,9 +622,7 @@ def get_usermessage_by_message_id(
user_profile: UserProfile, message_id: int user_profile: UserProfile, message_id: int
) -> Optional[UserMessage]: ) -> Optional[UserMessage]:
try: try:
return UserMessage.objects.select_related().get( return UserMessage.objects.get(user_profile=user_profile, message_id=message_id)
user_profile=user_profile, message_id=message_id
)
except UserMessage.DoesNotExist: except UserMessage.DoesNotExist:
return None return None