前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >Mongodb 查询优化

Mongodb 查询优化

作者头像
乐事
修改2020-05-11 10:52:37
修改2020-05-11 10:52:37
1.1K00
代码可运行
举报
文章被收录于专栏:日常笔记日常笔记
运行总次数:0
代码可运行

A good writeup of how your index should be created is available in Optimizing MongoDB Compound Indexes. Let's take the main point of the article, where the compound index ordering should be equality --> sort --> range:

Your query "shape" is:

代码语言:javascript
代码运行次数:0
复制
db.collection.find({category:..., _id: {$gt:...}})
             .sort({sticky:-1, lastPostAt:-1, _id:1})
             .limit(25)

We see that:

  • category:... is equality
  • sticky:-1, lastPostAt:-1, _id:1 is sort
  • _id: {$gt:...} is range

So the compound index you need is:

代码语言:javascript
代码运行次数:0
复制
{category:1, sticky:-1, lastPostAt:-1, _id:1}

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档