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

Commit d5b2a55

Browse files
committed
Slight optimization to avoid unnecessary queries.
1 parent ca012f4 commit d5b2a55

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

synapse/storage/databases/main/relations.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ def _get_thread_summaries_txn(
490490
if parent_event_id not in latest_event_ids:
491491
latest_event_ids[parent_event_id] = child_event_id
492492

493+
# If no threads were found, bail.
494+
if not latest_event_ids:
495+
return {}, latest_event_ids
496+
493497
# Fetch the number of threaded replies.
494498
sql = """
495499
SELECT parent.event_id, COUNT(child.event_id) FROM events AS child
@@ -502,7 +506,14 @@ def _get_thread_summaries_txn(
502506
AND relation_type = ?
503507
GROUP BY parent.event_id
504508
"""
505-
# TODO Re-generate args since we know some events don't have threads now.
509+
510+
# Regenerate the arguments since only threads found above could
511+
# possibly have any replies.
512+
clause, args = make_in_list_sql_clause(
513+
txn.database_engine, "relates_to_id", latest_event_ids.keys()
514+
)
515+
args.append(RelationTypes.THREAD)
516+
506517
txn.execute(sql % (clause,), args)
507518
counts = dict(cast(List[Tuple[str, int]], txn.fetchall()))
508519

0 commit comments

Comments
 (0)