Hey there,
I have a large collection of ~70m documents. I want to query the collection based on a time period. The field does have an index and according to the explain command the index is used.
db.requests.find({ createdAt: { $gte: new Date("2023-01-01"), $lt: new Date("2023-02-01") } }).explain("executionStats")
The execution stats are:
executionStats: {
executionSuccess: true,
nReturned: 4116735,
executionTimeMillis: 10913,
totalKeysExamined: 4116735,
totalDocsExamined: 4116735,
executionStages: {
stage: 'FETCH',
nReturned: 4116735,
executionTimeMillisEstimate: 1634,
works: 4116736,
advanced: 4116735,
needTime: 0,
needYield: 0,
saveState: 4116,
restoreState: 4116,
isEOF: 1,
docsExamined: 4116735,
alreadyHasObj: 0,
inputStage: {
stage: 'IXSCAN',
nReturned: 4116735,
executionTimeMillisEstimate: 711,
works: 4116736,
advanced: 4116735,
needTime: 0,
needYield: 0,
saveState: 4116,
restoreState: 4116,
isEOF: 1,
keyPattern: {
createdAt: 1
},
indexName: 'createdAt_1',
isMultiKey: false,
multiKeyPaths: {
createdAt: []
},
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: {
createdAt: [
'[new Date(1672531200000), new Date(1675209600000))'
]
},
keysExamined: 4116735,
seeks: 1,
dupsTested: 0,
dupsDropped: 0
}
}
}
Any idea why the query takes 10 seconds and if there is anything I can do to improve the performance?