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

Commit 79d2e2e

Browse files
authored
Speed up membership queries for users with forgotten rooms (#15385)
1 parent 89a71e7 commit 79d2e2e

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

changelog.d/15385.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Speed up membership queries for users with forgotten rooms.

synapse/storage/databases/main/roommember.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,11 @@ async def get_rooms_for_local_user_where_membership_is(
419419
)
420420

421421
# Now we filter out forgotten and excluded rooms
422-
rooms_to_exclude = await self.get_forgotten_rooms_for_user(user_id)
422+
rooms_to_exclude: AbstractSet[str] = set()
423+
424+
# Users can't forget joined/invited rooms, so we skip the check for such look ups.
425+
if any(m not in (Membership.JOIN, Membership.INVITE) for m in membership_list):
426+
rooms_to_exclude = await self.get_forgotten_rooms_for_user(user_id)
423427

424428
if excluded_rooms is not None:
425429
# Take a copy to avoid mutating the in-cache set
@@ -1391,6 +1395,12 @@ def __init__(
13911395
columns=["user_id", "room_id"],
13921396
where_clause="forgotten = 1",
13931397
)
1398+
self.db_pool.updates.register_background_index_update(
1399+
"room_membership_user_room_index",
1400+
index_name="room_membership_user_room_idx",
1401+
table="room_memberships",
1402+
columns=["user_id", "room_id"],
1403+
)
13941404

13951405
async def _background_add_membership_profile(
13961406
self, progress: JsonDict, batch_size: int
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Copyright 2023 The Matrix.org Foundation C.I.C
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
-- Add an index to `room_membership(user_id, room_id)` to make querying for
17+
-- forgotten rooms faster.
18+
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
19+
(7403, 'room_membership_user_room_index', '{}');

0 commit comments

Comments
 (0)