Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Slight optimization to avoid unnecessary queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Feb 9, 2022
1 parent f3241b0 commit ae9323c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion synapse/storage/databases/main/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@ def _get_thread_summaries_txn(
if parent_event_id not in latest_event_ids:
latest_event_ids[parent_event_id] = child_event_id

# If no threads were found, bail.
if not latest_event_ids:
return {}, latest_event_ids

# Fetch the number of threaded replies.
sql = """
SELECT parent.event_id, COUNT(child.event_id) FROM events AS child
Expand All @@ -502,7 +506,14 @@ def _get_thread_summaries_txn(
AND relation_type = ?
GROUP BY parent.event_id
"""
# TODO Re-generate args since we know some events don't have threads now.

# Regenerate the arguments since only threads found above could
# possibly have any replies.
clause, args = make_in_list_sql_clause(
txn.database_engine, "relates_to_id", latest_event_ids.keys()
)
args.append(RelationTypes.THREAD)

txn.execute(sql % (clause,), args)
counts = dict(cast(List[Tuple[str, int]], txn.fetchall()))

Expand Down

0 comments on commit ae9323c

Please sign in to comment.