MongoDB DBA · 题目 546 · 单选

Given the following query: db.books.find( { "category": "kids books", "price": { "$lt": 20.00 } } ).sort( { "number_sold": -1 } ) Which index on the books collection will be the most performant for the above query? Choose the best answer:

题目选项

  1. A.
    {"number_sold": 1, "category": 1, "price": 1}
  2. B.
    {"price": 1, "number_sold": 1, "category": 1}
  3. C.
    {"category": 1, "price": 1, "number_sold": 1}
  4. D.
    {"number_sold": 1, "price": 1, "category": 1}
  5. E.
    {"category": 1, "number_sold": 1, "price": 1}

答案置信度:高

正确答案:E

解析语言:中文

按 ESR 规则(等值 → 排序 → 范围)排列字段:category 是等值条件、number_sold 是排序字段、price 是范围条件,所以最优索引是 { category: 1, number_sold: 1, price: 1 }。C 把 price 放在排序字段之前,索引无法支撑排序;A、D 以 number_sold 开头,无法用 category 做前缀等值定位、只能全索引扫描;B 同样用不上 category 的等值匹配。单字段排序可由索引反向扫描实现,所以 number_sold: 1 也能满足 sort: -1。

所在套题

此题在题库中有 1 条记录。公开详情合并相同题干和选项;完整公开题展示置信度最高的一条记录,其他题目仅展示摘要。按套学习保留每条记录原有的答案、解析和置信度。

作者 · 作品 · 开源

作者与 MongoDB 作品

专注数据平台、MongoDB 迁移同步与云运维产品研发。

有任何 MongoDB 需求,均可联系作者。

联系作者 ↗mlhp123@outlook.com

支持作者

如果题库对您有帮助,欢迎自愿扫码打赏。

支付宝扫码打赏作者打开打赏原图 ↗