fix(db-mongodb): improve find query performance (#3836)

Fixes #3904

* fix(db-mongodb): improve find query performance

* fix: add optimization to other operations which use pagination: findGlobalVersions, findVersions, queryDrafts

* fix: index createdAt field by default
This commit is contained in:
Alessio Gravili
2023-10-27 17:57:18 +02:00
committed by GitHub
parent c564a83ab6
commit 56e58e9ec7
5 changed files with 59 additions and 4 deletions

View File

@@ -56,6 +56,8 @@ export const findVersions: FindVersions = async function findVersions(
where,
})
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0
const paginationOptions: PaginateOptions = {
forceCountFn: hasNearConstraint,
lean: true,
@@ -66,7 +68,18 @@ export const findVersions: FindVersions = async function findVersions(
page,
pagination,
sort,
useEstimatedCount: hasNearConstraint,
useEstimatedCount,
}
if (!useEstimatedCount) {
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding a hint.
paginationOptions.useCustomCountFn = () => {
return Promise.resolve(
Model.countDocuments(query, {
hint: { _id: 1 },
}),
)
}
}
if (limit > 0) {