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:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user