From 7ce057d3a38f1247da26fa6abd27ec7b8eac9e00 Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Thu, 15 Dec 2022 14:20:48 -0600 Subject: [PATCH] Preload sitemap images to prevent `o(n)` query issue Significantly improves performance of sitemap queries by ~5x. --- .../src/graphql/resolvers/platform/content.js | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/services/graphql-server/src/graphql/resolvers/platform/content.js b/services/graphql-server/src/graphql/resolvers/platform/content.js index aadafc6f6..7c94667fd 100644 --- a/services/graphql-server/src/graphql/resolvers/platform/content.js +++ b/services/graphql-server/src/graphql/resolvers/platform/content.js @@ -862,7 +862,6 @@ module.exports = { */ ContentSitemapUrl: { loc: (content, _, ctx) => createSitemapLoc(content, ctx), - images: (content, _, { basedb }) => loadSitemapImages({ content, basedb }), }, /** @@ -1157,10 +1156,29 @@ module.exports = { sort, }); const docs = []; + const imageIds = []; await cursor.forEach((doc) => { docs.push({ ...doc, changefreq, priority }); + imageIds.push(...getAsArray(doc, 'images')); + }); + + const imageCursor = await basedb.findCursor('platform.Asset', { + ...criteriaFor('assetImage'), _id: { $in: imageIds }, + }, { + projection: { + name: 1, + caption: 1, + filePath: 1, + fileName: 1, + cropDimensions: 1, + }, }); - return docs; + + const imageMap = new Map(await imageCursor.map(image => [`${image._id}`, image]).toArray()); + return docs.map(doc => ({ + ...doc, + images: getAsArray(doc, 'images').map(imageId => imageMap.get(`${imageId}`)).filter(v => v), + })); }, contentSitemapNewsUrls: async (_, { input }, { basedb, site }) => {