Add indexes for quarantined media lookups

This commit is contained in:
Travis Ralston
2025-12-16 16:17:33 -07:00
parent f4320b5a49
commit f40b352493
4 changed files with 53 additions and 2 deletions

View File

@@ -177,6 +177,35 @@ class MediaRepositoryBackgroundUpdateStore(SQLBaseStore):
],
)
self.db_pool.updates.register_background_index_update(
update_name="local_media_repository_quarantined_ts_idx",
index_name="local_media_repository_quarantined_by_quarantined_ts_media_id",
table="local_media_repository",
where_clause="quarantined_by IS NOT NULL",
columns=[
# We include columns in both the WHERE and ORDER BY clauses to make
# the resulting query a bit more efficient.
"quarantined_by",
"quarantined_ts",
"media_id",
],
)
self.db_pool.updates.register_background_index_update(
update_name="remote_media_cache_quarantined_ts_idx",
index_name="remote_media_cache_quarantined_by_quarantined_ts_media_origin_media_id",
table="remote_media_cache",
where_clause="quarantined_by IS NOT NULL",
columns=[
# We include columns in both the WHERE and ORDER BY clauses to make
# the resulting query a bit more efficient.
"quarantined_by",
"quarantined_ts",
"media_origin",
"media_id",
],
)
self.db_pool.updates.register_background_update_handler(
BG_UPDATE_REMOVE_MEDIA_REPO_INDEX_WITHOUT_METHOD_2,
self._drop_media_index_without_method,

View File

@@ -969,6 +969,11 @@ class RoomWorkerStore(CacheInvalidationWorkerStore):
) -> list[str]:
# We order by quarantined timestamp *and* media ID (including origin, when
# known) to ensure the ordering is stable for established servers.
#
# Note: these queries are backed by indexes. If the queries change, update
# them:
# - local_media_repository_quarantined_by_quarantined_ts_media_id
# - remote_media_cache_quarantined_by_quarantined_ts_media_origin_media_id
if local:
sql = "SELECT '' as media_origin, media_id FROM local_media_repository WHERE quarantined_by IS NOT NULL ORDER BY quarantined_ts, media_id ASC LIMIT ? OFFSET ?"
else:

View File

@@ -23,5 +23,4 @@ ALTER TABLE remote_media_cache ADD COLUMN quarantined_ts BIGINT;
UPDATE local_media_repository SET quarantined_ts = 0 WHERE quarantined_by IS NOT NULL;
UPDATE remote_media_cache SET quarantined_ts = 0 WHERE quarantined_by IS NOT NULL;
-- Note: We *probably* should have an index on quarantined_ts, but we're going
-- to try to defer that to a future migration after seeing the performance impact.
-- Note: the index is added in 05_add_quarantined_by_index.sql

View File

@@ -0,0 +1,18 @@
--
-- This file is licensed under the Affero General Public License (AGPL) version 3.
--
-- Copyright (C) 2025 Element Creations, Ltd
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- See the GNU Affero General Public License for more details:
-- <https://www.gnu.org/licenses/agpl-3.0.html>.
INSERT INTO background_updates (ordering, update_name, progress_json) VALUES
-- we should start at ordering 9305, but there's higher conflict if we steal 9306's ordering, so
-- we'll steal 9304's ordering instead. 9304 is where the applicable columns were added.
(9304, 'local_media_repository_quarantined_ts_idx', '{}'),
(9305, 'remote_media_cache_quarantined_ts_idx', '{}');