Fix query for room participation (#18345)

Follow on from #18068

Currently the subquery in `UPDATE` is pointless, as it will still just
update all `room_membership` rows. Instead, we should look at the
current membership event ID (which is easily retrieved from
`local_current_membership`). We also add a `AND NOT participant` to noop
the `UPDATE` when the `participant` flag is already set.

cc @H-Shay
This commit is contained in:
Erik Johnston 2025-04-16 14:14:56 +01:00 committed by GitHub
parent 0046d7278b
commit c16a981f22
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 12 deletions

1
changelog.d/18345.bugfix Normal file
View File

@ -0,0 +1 @@
Fix minor performance regression caused by tracking of room participation. Regressed in v1.128.0.

View File

@ -1622,14 +1622,11 @@ class RoomMemberWorkerStore(EventsWorkerStore, CacheInvalidationWorkerStore):
sql = """ sql = """
UPDATE room_memberships UPDATE room_memberships
SET participant = true SET participant = true
WHERE (user_id, room_id) IN ( WHERE event_id IN (
SELECT user_id, room_id SELECT event_id FROM local_current_membership
FROM room_memberships WHERE user_id = ? AND room_id = ?
WHERE user_id = ?
AND room_id = ?
ORDER BY event_stream_ordering DESC
LIMIT 1
) )
AND NOT participant
""" """
txn.execute(sql, (user_id, room_id)) txn.execute(sql, (user_id, room_id))
@ -1651,11 +1648,10 @@ class RoomMemberWorkerStore(EventsWorkerStore, CacheInvalidationWorkerStore):
) -> bool: ) -> bool:
sql = """ sql = """
SELECT participant SELECT participant
FROM room_memberships FROM local_current_membership AS l
WHERE user_id = ? INNER JOIN room_memberships AS r USING (event_id)
AND room_id = ? WHERE l.user_id = ?
ORDER BY event_stream_ordering DESC AND l.room_id = ?
LIMIT 1
""" """
txn.execute(sql, (user_id, room_id)) txn.execute(sql, (user_id, room_id))
res = txn.fetchone() res = txn.fetchone()