From 5bef8b7ebf81e3cdcb8c8fa1d849d04b592156f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Burkard?= Date: Thu, 3 Oct 2024 14:00:13 +0200 Subject: [PATCH] optimize search when no filters are provided --- .../PgEstablishmentAggregateRepository.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/back/src/domains/establishment/adapters/PgEstablishmentAggregateRepository.ts b/back/src/domains/establishment/adapters/PgEstablishmentAggregateRepository.ts index 99fcb4de9a..30f14ecc6f 100644 --- a/back/src/domains/establishment/adapters/PgEstablishmentAggregateRepository.ts +++ b/back/src/domains/establishment/adapters/PgEstablishmentAggregateRepository.ts @@ -902,6 +902,23 @@ const searchImmersionResultsQuery = ( return qb.whereRef("searchable_by_students", "is", sql`TRUE`); return qb; }, + (qb) => { + if ( + !filters?.geoParams && + !filters?.romeCodes && + (sortedBy === "date" || sortedBy === "score") + ) { + // this is in the case when NO filters are provided, to avoid doing the joins on the whole table when we will only keep 100 results in the end + // still doing a limit of 5000 because they will be aggregated by ROME and siret + return qb + .orderBy( + sortedBy === "date" ? "update_date" : "score", + "desc", + ) + .limit(5000); + } + return qb; + }, ).as("e"), ) .innerJoin(