Reputation: 403
I am new to MongoDB and trying to understand why are my queries so slow (30-150 seconds per query!). My database contains about 60 million documents. In my queries, I need to combine parametric and fulltext search. This is the query I am trying to analyze:
db.collection.explain("executionStats").find(
{"property.multi.value_title": "Pearson Education (US)",
"pricing.price" : {$gte: 70, $lte: 600},
$text: { $search: "app" }
}).count()
And this is the explain result:
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"property.multi.value_title" : {
"$eq" : "Pearson Education (US)"
}
},
{
"pricing.price" : {
"$lte" : 600
}
},
{
"pricing.price" : {
"$gte" : 70
}
},
{
"$text" : {
"$search" : "app",
"$language" : "none",
"$caseSensitive" : false,
"$diacriticSensitive" : false
}
}
]
},
"winningPlan" : {
"stage" : "COUNT",
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"pricing.price" : {
"$lte" : 600
}
},
{
"pricing.price" : {
"$gte" : 70
}
},
{
"property.multi.value_title" : {
"$eq" : "Pearson Education (US)"
}
}
]
},
"inputStage" : {
"stage" : "TEXT",
"indexPrefix" : {
},
"indexName" : "translate.cs.content_text",
"parsedTextQuery" : {
"terms" : [
"app"
],
"negatedTerms" : [ ],
"phrases" : [ ],
"negatedPhrases" : [ ]
},
"textIndexVersion" : 3,
"inputStage" : {
"stage" : "TEXT_MATCH",
"inputStage" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "OR",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"_fts" : "text",
"_ftsx" : 1
},
"indexName" : "translate.cs.content_text",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
}
}
}
}
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 0,
"executionTimeMillis" : 29794,
"totalKeysExamined" : 7996,
"totalDocsExamined" : 15992,
"executionStages" : {
"stage" : "COUNT",
"nReturned" : 0,
"executionTimeMillisEstimate" : 27400,
"works" : 7997,
"advanced" : 0,
"needTime" : 7996,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"nCounted" : 6,
"nSkipped" : 0,
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$and" : [
{
"pricing.price" : {
"$lte" : 600
}
},
{
"pricing.price" : {
"$gte" : 70
}
},
{
"property.multi.value_title" : {
"$eq" : "Pearson Education (US)"
}
}
]
},
"nReturned" : 6,
"executionTimeMillisEstimate" : 27400,
"works" : 7997,
"advanced" : 6,
"needTime" : 7990,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"docsExamined" : 7996,
"alreadyHasObj" : 7996,
"inputStage" : {
"stage" : "TEXT",
"nReturned" : 7996,
"executionTimeMillisEstimate" : 27390,
"works" : 7997,
"advanced" : 7996,
"needTime" : 0,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"indexPrefix" : {
},
"indexName" : "translate.cs.content_text",
"parsedTextQuery" : {
"terms" : [
"app"
],
"negatedTerms" : [ ],
"phrases" : [ ],
"negatedPhrases" : [ ]
},
"textIndexVersion" : 3,
"inputStage" : {
"stage" : "TEXT_MATCH",
"nReturned" : 7996,
"executionTimeMillisEstimate" : 27390,
"works" : 7997,
"advanced" : 7996,
"needTime" : 0,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"docsRejected" : 0,
"inputStage" : {
"stage" : "FETCH",
"nReturned" : 7996,
//HERE
"executionTimeMillisEstimate" : 27380,
"works" : 7997,
"advanced" : 7996,
"needTime" : 0,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"docsExamined" : 7996,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "OR",
"nReturned" : 7996,
"executionTimeMillisEstimate" : 175,
"works" : 7997,
"advanced" : 7996,
"needTime" : 0,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"dupsTested" : 7996,
"dupsDropped" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 7996,
"executionTimeMillisEstimate" : 165,
"works" : 7997,
"advanced" : 7996,
"needTime" : 0,
"needYield" : 0,
"saveState" : 1220,
"restoreState" : 1220,
"isEOF" : 1,
"keyPattern" : {
"_fts" : "text",
"_ftsx" : 1
},
"indexName" : "translate.cs.content_text",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {
},
"keysExamined" : 7996,
"seeks" : 1,
"dupsTested" : 7996,
"dupsDropped" : 0
}
}
}
}
}
}
}
},
"serverInfo" : {
"host" : "ubuntu2004",
"port" : 27017,
"version" : "4.4.1",
"gitVersion" : "ad91a93a5a31e175f5cbf8c69561e788bbc55ce1"
},
"ok" : 1
}
I couldn't specify my language in the text index, because it is not supported my MongoDB.
I was looking at the time estimates at the executionStats part, and I noticed that after the IXSCAN
and OR
operations, that took about 170ms each, the time suddenly jumped to "executionTimeMillisEstimate" : 27380
at the FETCH
part.
Can someone please explain what does it mean and how can I improve the performance of this query?
EDIT: these are indexes on my collection:
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "translate.cs.content_text",
"default_language" : "none",
"language_override" : "none",
"weights" : {
"translate.cs.content" : 1
},
"textIndexVersion" : 3
},
{
"v" : 2,
"key" : {
"property" : 1
},
"name" : "property_1"
},
{
"v" : 2,
"key" : {
"property.multi.value_title" : 1
},
"name" : "property.multi.value_title_1"
}
Upvotes: 1
Views: 1945
Reputation: 1680
You need to create a query that ensure selectivity. For this you can created a compound index. Also is good to mention that you can specified a language in the text index (default is english)
db.collection.createIndex(
{
property.multi.value_title: 1,
field: "text",
pricing.price: 1
},
{ default_language: "spanish" }
)
For compound indexes, ESR is helpful in deciding the order of fields in the index:
- First, add those fields against which Equality queries are run.
- The next fields to be indexed should reflect the Sort order of the query.
- The last fields represent the Range of data to be accessed.
Upvotes: 2
Reputation: 14480
My guess is you either have very slow disk or not enough memory for this workload.
Look at I/O wait time in top
to verify the workload is I/O bound.
Upvotes: 1