mongodb AND查询遇到多个index时候可能会做交集——和复合索引不同
关于MongoDB中索引文档的一个问题?
-
To illustrate index intersection, consider a collection orders that has the following indexes:
{ qty: 1 }
{ item: 1 }
MongoDB can use the intersection of the two indexes to support the following query:
db.orders.find( { item: "abc123", qty: { $gt: 15 } } )
上面是MongoDB的索引文档,说下面这个查询能交叉利用上面两个索引进行优化,可是根据我的理解,需要建立一个多重索引才行,如下:
{ qty: 1, item: 1 }
建立两个索引是建立两个独立的B+树,多重索引则是建立一颗B+树,如果两颗B+树是独立的,怎么才能交叉利用呢?
还有下面这个例子,也不知道如何才能混合利用起来?
Consider a collection orders with the following indexes:
{ qty: 1 }
{ status: 1, ord_date: -1 }
To fulfill the following query which specifies a condition on both the qty field and the status field, MongoDB can use the intersection of the two indexes:
db.orders.find( { qty: { $gt: 10 } , status: "A" } )
看问题似乎很神奇。。不过实际看过去也没啥变化。。
determine if MongoDB used index intersection, run explain(); the results of explain()will include either an AND_SORTED stage or an AND_HASH stage.
归并排序或hash 组合。 类似join的做法。
Stages are descriptive of the operation; e.g.
COLLSCAN
for a collection scanIXSCAN
for scanning index keysFETCH
for retrieving documentsSHARD_MERGE
for merging results from shards
Index Intersection
For an index intersection plan, the result will include either an AND_SORTED
stage or an AND_HASH
stage with an inputStages
array that details the indexes; e.g.:
{
"stage" : "AND_SORTED",
"inputStages" : [
{
"stage" : "IXSCAN",
...
},
{
"stage" : "IXSCAN",
...
}
]
}
In previous versions of MongoDB, cursor.explain()
returned the cursor
field with the value of Complex Plan
for index intersections.
转自:http://www.ihowandwhy.com/z/%E5%85%B3%E4%BA%8EMongoDB%E4%B8%AD%E7%B4%A2%E5%BC%95%E6%96%87%E6%A1%A3%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98%EF%BC%9F
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」