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

Improve performance of _get_state_groups_from_groups_txn #7567

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/7567.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve query performance for fetching state from a PostgreSQL database.
12 changes: 6 additions & 6 deletions synapse/storage/data_stores/state/bg_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ def _get_state_groups_from_groups_txn(
SELECT prev_state_group FROM state_group_edges e, state s
WHERE s.state_group = e.state_group
)
SELECT DISTINCT type, state_key, last_value(event_id) OVER (
PARTITION BY type, state_key ORDER BY state_group ASC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS event_id FROM state_groups_state
SELECT DISTINCT ON (type, state_key)
type, state_key, event_id
FROM state_groups_state
WHERE state_group IN (
SELECT state_group FROM state
)
) %s
ORDER BY type, state_key, state_group DESC
"""

for group in groups:
args = [group]
args.extend(where_args)

txn.execute(sql + where_clause, args)
txn.execute(sql % (where_clause,), args)
for row in txn:
typ, state_key, event_id = row
key = (typ, state_key)
Expand Down