Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Preload sitemap images to prevent o(n) query issue
Browse files Browse the repository at this point in the history
Significantly improves performance of sitemap queries by ~5x.
  • Loading branch information
zarathustra323 committed Dec 15, 2022
1 parent 3aec203 commit 7ce057d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions services/graphql-server/src/graphql/resolvers/platform/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,6 @@ module.exports = {
*/
ContentSitemapUrl: {
loc: (content, _, ctx) => createSitemapLoc(content, ctx),
images: (content, _, { basedb }) => loadSitemapImages({ content, basedb }),
},

/**
Expand Down Expand Up @@ -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 }) => {
Expand Down

0 comments on commit 7ce057d

Please sign in to comment.