Hi Team,
I have the below aggreagtion query where I am using $addField $lookup and $unwind and I have the Index which is using but it still takes more than 13 Seconds to execute this query.
Can anyone let me know why this is taking so much time for this query and the possible ways to optimize this?
db.getCollection('booked-slot').aggregate([{"$match":{"facility":ObjectId("63ee2e8458b6fd9eb66511ce")}},{"$addFields":{"userId":{"$cond":{"if":{"$eq":["$participant.isPrimary",true]},"then":"$participant.primaryUserId","else":"$participant.secondaryUserId"}},"dateValue":{"$dateFromString":{"dateString":"$date"}}}},{"$lookup":{"from":"users","localField":"userId","foreignField":"_id","as":"user"}},{"$unwind":{"path":"$user","preserveNullAndEmptyArrays":true}}]).explain("executionStats")`
Here is the explain plan for this.
db.getCollection('booked-slot').aggregate([{"$match":{"facility":ObjectId("63ee2e8458b6fd9eb66511ce")}},{"$addFields":{"userId":{"$cond":{"if":{"$eq":["$participant.isPrimary",true]},"then":"$participant.primaryUserId","else":"$participant.secondaryUserId"}},"dateValue":{"$dateFromString":{"dateString":"$date"}}}},{"$lookup":{"from":"users","localField":"userId","foreignField":"_id","as":"user"}},{"$unwind":{"path":"$user","preserveNullAndEmptyArrays":true}}]).explain("executionStats")
{
explainVersion: '1',
stages: [
{
'$cursor': {
queryPlanner: {
namespace: 'test_tuning.booked-slot',
indexFilterSet: false,
parsedQuery: { facility: { '$eq': ObjectId("63ee2e8458b6fd9eb66511ce") } },
queryHash: '1EA0CE2C',
planCacheKey: 'F5E357E3',
maxIndexedOrSolutionsReached: false,
maxIndexedAndSolutionsReached: false,
maxScansToExplodeReached: false,
winningPlan: {
stage: 'FETCH',
inputStage: {
stage: 'IXSCAN',
keyPattern: { facility: 1, state: 1, date: -1, slotStartTime: -1 },
indexName: 'facility_1_state_1_date_-1_slotStartTime_-1',
isMultiKey: false,
multiKeyPaths: { facility: [], state: [], date: [], slotStartTime: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: {
facility: [
"[ObjectId('63ee2e8458b6fd9eb66511ce'), ObjectId('63ee2e8458b6fd9eb66511ce')]"
],
state: [ '[MinKey, MaxKey]' ],
date: [ '[MaxKey, MinKey]' ],
slotStartTime: [ '[MaxKey, MinKey]' ]
}
}
},
rejectedPlans: []
},
executionStats: {
executionSuccess: true,
nReturned: 95563,
executionTimeMillis: 13425,
totalKeysExamined: 95563,
totalDocsExamined: 95563,
executionStages: {
stage: 'FETCH',
nReturned: 95563,
executionTimeMillisEstimate: 71,
works: 95564,
advanced: 95563,
needTime: 0,
needYield: 0,
saveState: 156,
restoreState: 156,
isEOF: 1,
docsExamined: 95563,
alreadyHasObj: 0,
inputStage: {
stage: 'IXSCAN',
nReturned: 95563,
executionTimeMillisEstimate: 31,
works: 95564,
advanced: 95563,
needTime: 0,
needYield: 0,
saveState: 156,
restoreState: 156,
isEOF: 1,
keyPattern: { facility: 1, state: 1, date: -1, slotStartTime: -1 },
indexName: 'facility_1_state_1_date_-1_slotStartTime_-1',
isMultiKey: false,
multiKeyPaths: { facility: [], state: [], date: [], slotStartTime: [] },
isUnique: false,
isSparse: false,
isPartial: false,
indexVersion: 2,
direction: 'forward',
indexBounds: {
facility: [
"[ObjectId('63ee2e8458b6fd9eb66511ce'), ObjectId('63ee2e8458b6fd9eb66511ce')]"
],
state: [ '[MinKey, MaxKey]' ],
date: [ '[MaxKey, MinKey]' ],
slotStartTime: [ '[MaxKey, MinKey]' ]
},
keysExamined: 95563,
seeks: 1,
dupsTested: 0,
dupsDropped: 0
}
}
}
},
nReturned: Long("95563"),
executionTimeMillisEstimate: Long("233")
},
{
'$addFields': {
userId: {
'$cond': [
{ '$eq': [ '$participant.isPrimary', { '$const': true } ] },
'$participant.primaryUserId',
'$participant.secondaryUserId'
]
},
dateValue: { '$dateFromString': { dateString: '$date' } }
},
nReturned: Long("95563"),
executionTimeMillisEstimate: Long("236")
},
{
'$lookup': {
from: 'users',
as: 'user',
localField: 'userId',
foreignField: '_id',
unwinding: { preserveNullAndEmptyArrays: true }
},
totalDocsExamined: Long("95562"),
totalKeysExamined: Long("95562"),
collectionScans: Long("0"),
indexesUsed: [ '_id_' ],
nReturned: Long("95563"),
executionTimeMillisEstimate: Long("13418")
}
],
serverInfo: {
host: 'ip-10-22-11-89.ap-south-1.compute.internal',
port: 27017,
version: '6.0.5',
gitVersion: 'c9a99c120371d4d4c52cbb15dac34a36ce8d3b1d'
},
serverParameters: {
internalQueryFacetBufferSizeBytes: 104857600,
internalQueryFacetMaxOutputDocSizeBytes: 104857600,
internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600,
internalDocumentSourceGroupMaxMemoryBytes: 104857600,
internalQueryMaxBlockingSortMemoryUsageBytes: 104857600,
internalQueryProhibitBlockingMergeOnMongoS: 0,
internalQueryMaxAddToSetBytes: 104857600,
internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600
},
command: {
aggregate: 'booked-slot',
pipeline: [
{ '$match': { facility: ObjectId("63ee2e8458b6fd9eb66511ce") } },
{
'$addFields': {
userId: {
'$cond': {
if: { '$eq': [ '$participant.isPrimary', true ] },
then: '$participant.primaryUserId',
else: '$participant.secondaryUserId'
}
},
dateValue: { '$dateFromString': { dateString: '$date' } }
}
},
{
'$lookup': {
from: 'users',
localField: 'userId',
foreignField: '_id',
as: 'user'
}
},
{
'$unwind': { path: '$user', preserveNullAndEmptyArrays: true }
}
],
cursor: {},
'$db': 'test_tuning'
},
Please find the document counts in each collection:
booked-slot - 95563
users - 698422