From 7931d340d9b25106b6fda03dd95609a73558ebb9 Mon Sep 17 00:00:00 2001 From: Pascal Kaufmann Date: Wed, 6 Nov 2024 18:51:16 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20super=20slow=20products=E2=80=A6=20finall?= =?UTF-8?q?y=20!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/module/configureFilterSearchModule.ts | 17 ++++++----------- .../src/module/configureFiltersModule.ts | 17 ++++------------- .../src/module/configureProductsModule.ts | 7 ++++++- packages/plugins/src/worker/zombie-killer.ts | 6 +----- packages/types/products.ts | 2 ++ 5 files changed, 19 insertions(+), 30 deletions(-) diff --git a/packages/core-filters/src/module/configureFilterSearchModule.ts b/packages/core-filters/src/module/configureFilterSearchModule.ts index 07b9c9563..2736f919d 100644 --- a/packages/core-filters/src/module/configureFilterSearchModule.ts +++ b/packages/core-filters/src/module/configureFilterSearchModule.ts @@ -101,17 +101,12 @@ export const configureFilterSearchModule = ({ return leftIndex - rightIndex; }); - const relevantProducts = await modules.products.findProducts( - { - productSelector, - productIds: totalProductIds, - includeDrafts: searchQuery.includeInactive, - }, - { - projection: { _id: 1 }, - }, - ); - const relevantProductIds = relevantProducts.map(({ _id }) => _id); + const relevantProductIds = await modules.products.findProductIds({ + productSelector, + productIds: totalProductIds, + includeDrafts: searchQuery.includeInactive, + }); + return Promise.all( sortedFilters.map(async (filter) => { return loadFilter( diff --git a/packages/core-filters/src/module/configureFiltersModule.ts b/packages/core-filters/src/module/configureFiltersModule.ts index bf87148f8..e4bb3a949 100644 --- a/packages/core-filters/src/module/configureFiltersModule.ts +++ b/packages/core-filters/src/module/configureFiltersModule.ts @@ -72,19 +72,10 @@ export const configureFiltersModule = async ({ ); if (!productSelector) return []; - const products = await modules.products.findProducts( - { - productSelector, - includeDrafts: true, - sort: [], - offset: 0, - limit: 0, - }, - { - projection: { _id: true }, - }, - ); - return products.map(({ _id }) => _id); + return modules.products.findProductIds({ + productSelector, + includeDrafts: true, + }); }; const buildProductIdMap = async ( diff --git a/packages/core-products/src/module/configureProductsModule.ts b/packages/core-products/src/module/configureProductsModule.ts index 24eb0d2b0..3f0cb9030 100644 --- a/packages/core-products/src/module/configureProductsModule.ts +++ b/packages/core-products/src/module/configureProductsModule.ts @@ -209,12 +209,13 @@ export const configureProductsModule = async ({ return Products.findOne(selector, {}); }, - findProducts: async ({ limit, offset, sort, ...query }) => { + findProducts: async ({ limit, offset, sort, ...query }, options) => { const defaultSortOption: Array = [ { key: 'sequence', value: SortDirection.ASC }, { key: 'published', value: SortDirection.DESC }, ]; const products = Products.find(buildFindSelector(query), { + ...(options || {}), limit, skip: offset, sort: buildSortOptions(sort || defaultSortOption), @@ -222,6 +223,10 @@ export const configureProductsModule = async ({ return products.toArray(); }, + findProductIds: async (query) => { + return Products.distinct('_id', buildFindSelector(query)); + }, + count: async (query) => { return Products.countDocuments(buildFindSelector(query)); }, diff --git a/packages/plugins/src/worker/zombie-killer.ts b/packages/plugins/src/worker/zombie-killer.ts index fd6510b1f..69c2bcbfe 100644 --- a/packages/plugins/src/worker/zombie-killer.ts +++ b/packages/plugins/src/worker/zombie-killer.ts @@ -53,11 +53,7 @@ export const ZombieKillerWorker: IWorkerAdapter< }); // Remove unreferenced product entities - const products = await modules.products.findProducts( - { includeDrafts: true }, - { projection: { _id: 1 } }, - ); - const productIds = products.map((a) => a._id); + const productIds = await modules.products.findProductIds({ includeDrafts: true }); const deletedProductTextsCount = await modules.products.texts.deleteMany({ excludedProductIds: productIds, }); diff --git a/packages/types/products.ts b/packages/types/products.ts index 27a0ec3d0..ff7f0f8ba 100644 --- a/packages/types/products.ts +++ b/packages/types/products.ts @@ -181,6 +181,8 @@ export type ProductsModule = { options?: FindOptions, ) => Promise>; + findProductIds: (params: ProductQuery) => Promise>; + count: (query: ProductQuery) => Promise; productExists: (params: { productId?: string; slug?: string }) => Promise;