Optimize Sanity Check SQL

Instead of
SELECT COUNT(media_id) from mediaapi_thumbnail WHERE media_id NOT IN (SELECT media_id FROM mediaapi_media_repository);
we do
SELECT COUNT(media_id) from mediaapi_thumbnail WHERE NOT EXISTS (SELECT media_id FROM mediaapi_media_repository);

Per Till's (@s7evink:matrix.org) suggestion. All credit to him
This commit is contained in:
Sebastian Spaeth 2024-03-12 18:02:44 +01:00
parent 0a03429abf
commit 7a4d497353

View file

@ -177,7 +177,7 @@ class MediaRepository:
def sanity_check_thumbnails(self) -> None:
"""Warn if we have thumbnails in the db that do not refer to existing media"""
with self.conn.cursor() as cur:
cur.execute("SELECT COUNT(media_id) from mediaapi_thumbnail WHERE media_id NOT IN (SELECT media_id FROM mediaapi_media_repository);")
cur.execute("SELECT COUNT(media_id) from mediaapi_thumbnail WHERE NOT EXISTS (SELECT media_id FROM mediaapi_media_repository);")
row = cur.fetchone()
if row is not None and row[0]:
logging.error("You have {} thumbnails in your db that do not refer to media. This needs fixing (we don't do that)!".format(row[0]))