为 distinct 建立索引以使用覆盖查询
indexing for distinct to use covered query
我无法使用索引优化不同的查询。
我的 collection 看起来像这样:
{
"_id" : ObjectId("592ed92296232608d00358bd"),
"measurement" : ObjectId("592ed92196232608d0034c23"),
"loc" : {
"coordinates" : [
2.65939299848366,
50.4380671935187
],
"type" : "Point"
},
"elements" : [
ObjectId("592ed92196232608d0034c24"),
ObjectId("592ed92196232608d0034c26"),
ObjectId("592ed92196232608d0034c28")
]
}
我正在尝试执行类似
的查询
db.mycol.distinct('elements', {
$and:[
measurement:{
$in:[
ObjectId("592ed92196232608d0034c23"),
ObjectId("592ed92196232608d0034c24")
]
},
{
loc:{
$geoWithin:{
$geometry:{
type:'Polygon',
coordinates:[[
[
2.0214843750000004,
50.25071752130677
],
[
2.0214843750000004,
50.65294336725709
],
[
3.0487060546875004,
50.65294336725709
],
[
3.0487060546875004,
50.25071752130677
],
[
2.0214843750000004,
50.25071752130677
]
]]
}
}
}
}
]
})
我有这个索引:
{
measurement: 1,
loc: '2dsphere',
elements: 1
}
查询计划 (db.mycol.explain().distinct(...)
) 显示 IXSCAN,但查询时间过长。我添加了索引,希望它可以使用 Mongo covered query。该文件指出
- all the fields in the query are part of an index,
- and all the fields returned in the results are in the same index.
所以我猜我需要一个包含 elements
的索引。但是根据查询执行时间来看,并没有使用。
为这样的查询索引 collection 的最佳方法是什么?
涵盖的查询不适用于数组。
来自问题中引用的同一页面:
Restrictions on Indexed Fields
An index cannot cover a query if:
- any of the indexed fields in any of the documents in the collection includes an array. If an indexed field is an array, the index becomes a multi-key index and cannot support a covered query.
我无法使用索引优化不同的查询。
我的 collection 看起来像这样:
{
"_id" : ObjectId("592ed92296232608d00358bd"),
"measurement" : ObjectId("592ed92196232608d0034c23"),
"loc" : {
"coordinates" : [
2.65939299848366,
50.4380671935187
],
"type" : "Point"
},
"elements" : [
ObjectId("592ed92196232608d0034c24"),
ObjectId("592ed92196232608d0034c26"),
ObjectId("592ed92196232608d0034c28")
]
}
我正在尝试执行类似
的查询db.mycol.distinct('elements', {
$and:[
measurement:{
$in:[
ObjectId("592ed92196232608d0034c23"),
ObjectId("592ed92196232608d0034c24")
]
},
{
loc:{
$geoWithin:{
$geometry:{
type:'Polygon',
coordinates:[[
[
2.0214843750000004,
50.25071752130677
],
[
2.0214843750000004,
50.65294336725709
],
[
3.0487060546875004,
50.65294336725709
],
[
3.0487060546875004,
50.25071752130677
],
[
2.0214843750000004,
50.25071752130677
]
]]
}
}
}
}
]
})
我有这个索引:
{
measurement: 1,
loc: '2dsphere',
elements: 1
}
查询计划 (db.mycol.explain().distinct(...)
) 显示 IXSCAN,但查询时间过长。我添加了索引,希望它可以使用 Mongo covered query。该文件指出
- all the fields in the query are part of an index,
- and all the fields returned in the results are in the same index.
所以我猜我需要一个包含 elements
的索引。但是根据查询执行时间来看,并没有使用。
为这样的查询索引 collection 的最佳方法是什么?
涵盖的查询不适用于数组。
来自问题中引用的同一页面:
Restrictions on Indexed Fields
An index cannot cover a query if:
- any of the indexed fields in any of the documents in the collection includes an array. If an indexed field is an array, the index becomes a multi-key index and cannot support a covered query.