有一個集合people
用以下指標:過濾和排序下列哪些查詢將使用索引?
{"first_name": 1, "address.state": -1, "address.city": -1, "ssn": 1}
對於兩個過濾&排序, 以下哪些查詢會使用索引? Which of the following queries will use the index?
:{ "first_name": { $gt: "J" } }).sort({ "address.city": -1 }
{ "first_name": "Jessica" }).sort({ "address.state": 1, "address.city": 1 }
{ "first_name": "Jessica", "address.state": { $lt: "S"} }).sort({ "address.state": 1 }
{"address.city":"West Cindy"}).sort({ "address.city":-1}
{"address.state":"South Dakota","first_name": "Jessica"}).sort({ "address.city":-1}
我已經通過下面的問題去了
但它只解釋了過濾的索引,我需要使用兩個過濾器以及排序功能的索引。
另外,如何確定索引是否用於兩個篩選器&排序 或未使用?
加上'.explain();' –
【解釋】(https://docs.mongodb.com/manual/reference/explain-results/#executionstats)會告訴你在哪裏指數被使用,但作爲B-Tree指數的tumb的規則,他們將從左到右工作,從索引中的第一項開始。您可以查看關於B-tree索引的官方維基百科(這裏是https://en.wikipedia.org/wiki/B-tree)。同樣對於排序將會影響索引的方向(ASC或DESC) –
@Dan Ionescu,複合索引用於向左(而不是從右到左)。所以複合索引的第一個字段可以直接使用,而不是第二個... – JJussi