From 89dfa42e182d718d27da69dbbb3e97c37a2a4d2a Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Sat, 6 Jun 2026 15:44:04 -0400 Subject: [PATCH] =?UTF-8?q?fix(showcase):=20over-sample=20+=20random-order?= =?UTF-8?q?=20to=20break=20near-dup=20clustering=20=E2=80=94=20#699?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TABLESAMPLE SYSTEM_ROWS reads CONTIGUOUS rows from each sampled page, so sequentially-imported near-duplicates (multi-image posts, variant sets) came back adjacent and clustered in the showcase ("three near-identical in a row"). Sample limit*5 rows (spanning more pages) then ORDER BY random() before taking limit — breaks the physical adjacency for much better spread, still cheap (random() over a few hundred rows, not the whole table). Co-Authored-By: Claude Opus 4.8 (1M context) --- backend/app/services/showcase_service.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/app/services/showcase_service.py b/backend/app/services/showcase_service.py index f34051b..e60870c 100644 --- a/backend/app/services/showcase_service.py +++ b/backend/app/services/showcase_service.py @@ -20,11 +20,20 @@ class ShowcaseService: async def random_sample(self, limit: int = 60) -> list[dict]: if limit < 1 or limit > 200: raise ValueError("limit must be between 1 and 200") + # Over-sample then random-order (#699): SYSTEM_ROWS reads CONTIGUOUS rows + # from each sampled page, so sequentially-imported near-duplicates + # (multi-image posts, variant sets) come back adjacent and cluster in the + # showcase ("three near-identical in a row"). Sampling a multiple of + # `limit` spans more pages, and ORDER BY random() before taking `limit` + # breaks the physical adjacency — far better spread, still cheap + # (random() over a few hundred rows, not the whole table). + oversample = min(limit * 5, 1000) stmt = select(ImageRecord).from_statement( text( - "SELECT * FROM image_record " - "TABLESAMPLE SYSTEM_ROWS(:n)" - ).bindparams(n=limit) + "SELECT * FROM (" + " SELECT * FROM image_record TABLESAMPLE SYSTEM_ROWS(:o)" + ") sub ORDER BY random() LIMIT :n" + ).bindparams(o=oversample, n=limit) ) rows = (await self.session.execute(stmt)).scalars().all() return [