我非常感谢以下有关查询的任何帮助:
db.searches.find({ $text: { $search: "dragon" }},{product_name:1,_id:0} ).limit(30)
"搜索"集合有1.26亿个文档,我们的目标是实现亚秒级查询性能。
集合的格式非常简单:
{
"_id" : ObjectId("584b15d12bb299260a000006"),
"product_name" : "This is a product name",
"price" : 876
}
索引和系统配置文件结果都在下面。我们不明白为什么阶段如此缓慢 - 总时间为161秒。同样,不太常见的单词(例如狗)比普通单词(例如龙)快得多,鉴于我们只需要30个文件,这似乎很奇怪。
这是在带有连线虎的MongoDB 3.4.0上运行的。索引应该在内存中,因为它们占用服务器上128GB内存的不到50%。
INDEX
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "marketplaces.searches"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1,
"price" : 1.0
},
"name" : "search_index_name_price",
"ns" : "marketplaces.searches",
"background" : true,
"default_language" : "en",
"language_override" : "en",
"weights" : {
"product_name" : 1
},
"textIndexVersion" : 3
}
]
系统简介
{
"op" : "query",
"ns" : "marketplaces.searches",
"query" : {
"find" : "searches",
"filter" : {
"$text" : {
"$search" : "dragon"
}
},
"limit" : 30.0,
"singleBatch" : false,
"projection" : {
"product_name" : 1.0,
"_id" : 0.0
}
},
"keysExamined" : 1563505,
"docsExamined" : 1563505,
"cursorExhausted" : true,
"numYield" : 14547,
"locks" : {
"Global" : {
"acquireCount" : {
"r" : NumberLong(29098)
}
},
"Database" : {
"acquireCount" : {
"r" : NumberLong(14549)
}
},
"Collection" : {
"acquireCount" : {
"r" : NumberLong(14549)
}
}
},
"nreturned" : 30,
"responseLength" : 4132,
"protocol" : "op_command",
"millis" : 161315,
"planSummary" : "IXSCAN { _fts: \"text\", _ftsx: 1, price: 1.0 }",
"execStats" : {
"stage" : "LIMIT",
"nReturned" : 30,
"executionTimeMillisEstimate" : 145005,
"works" : 1563538,
"advanced" : 30,
"needTime" : 1563507,
"needYield" : 0,
"saveState" : 14547,
"restoreState" : 14547,
"isEOF" : 1,
"invalidates" : 0,
"limitAmount" : 30,
"inputStage" : {
"stage" : "PROJECTION",
"nReturned" : 30,
"executionTimeMillisEstimate" : 144635,
"works" : 1563537,
"advanced" : 30,
"needTime" : 1563507,
"needYield" : 0,
"saveState" : 14547,
"restoreState" : 14547,
"isEOF" : 0,
"invalidates" : 0,
"transformBy" : {
"product_name" : 1.0,
"_id" : 0.0
},
"inputStage" : {
"stage" : "TEXT",
"nReturned" : 30,
"executionTimeMillisEstimate" : 144258,
"works" : 1563537,
"advanced" : 30,
"needTime" : 1563507,
"needYield" : 0,
"saveState" : 14547,
"restoreState" : 14547,
"isEOF" : 0,
"invalidates" : 0,
"indexPrefix" : {},
"indexName" : "search_index_name_price",
"parsedTextQuery" : {
"terms" : [
"dragon"
],
"negatedTerms" : [],
"phrases" : [],
"negatedPhrases" : []
},
"textIndexVersion" : 3,
"inputStage" : {
"stage" : "TEXT_MATCH",
"nReturned" : 30,
"executionTimeMillisEstimate" : 143819,
"works" : 1563537,
"advanced" : 30,
"needTime" : 1563507,
"needYield" : 0,
"saveState" : 14547,
"restoreState" : 14547,
"isEOF" : 0,
"invalidates" : 0,
"docsRejected" : 0,
"inputStage" : {
"stage" : "TEXT_OR",
"nReturned" : 30,
"executionTimeMillisEstimate" : 143608,
"works" : 1563537,
"advanced" : 30,
"needTime" : 1563507,
"needYield" : 0,
"saveState" : 14547,
"restoreState" : 14547,
"isEOF" : 0,
"invalidates" : 0,
"docsExamined" : 1563505,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1563505,
"executionTimeMillisEstimate" : 20499,
"works" : 1563506,
"advanced" : 1563505,
"needTime" : 0,
"needYield" : 0,
"saveState" : 14547,
"restoreState" : 14547,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"_fts" : "text",
"_ftsx" : 1,
"price" : 1.0
},
"indexName" : "search_index_name_price",
"isMultiKey" : true,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "backward",
"indexBounds" : {},
"keysExamined" : 1563505,
"seeks" : 1,
"dupsTested" : 1563505,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
}
}
},
"ts" : ISODate("2017-04-28T14:34:57.320Z"),
"client" : "127.0.0.1",
"appName" : "MongoDB Shell",
"allUsers" : [],
"user" : ""
}
除非我们能够进一步调整,否则可以考虑使用Elasticsearch。