MongoDB 在执行 distinct 时不在某些字段上使用索引
MongoDB not uses indexes on some fields when doing distinct
我注意到 MongoDB 在查询字段上的不同值时不会使用索引。我会在某些领域使用它,但不会在其他领域使用它。
示例如下:
db.product.createIndex({"_indexed.preventieve_mondzorg-max_bedrag_p_jr": 1});
db.runCommand({distinct: "product", key:"_indexed.preventieve_mondzorg-max_bedrag_p_jr"});
此查询将不使用建立在该字段上的索引,并将进行完整的集合扫描。这就是它产生的结果:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 50,
"numIndexesAfter" : 50,
"note" : "all indexes already exist",
"ok" : 1
}
{
"values" : [
"€ 250,- | 75%",
"Geen dekking",
"...",
],
"stats" : {
"n" : 33660,
"nscanned" : 0,
"nscannedObjects" : 33660,
"timems" : 12531,
"planSummary" : "COLLSCAN"
},
"ok" : 1
}
另一方面
db.product.createIndex({"free_choice.value": 1});
db.runCommand({distinct: "product", key:"free_choice.value"});
请问指数:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 50,
"numIndexesAfter" : 50,
"note" : "all indexes already exist",
"ok" : 1
}
{
"values" : [
"gedeeltelijk",
"geen",
"ja"
],
"stats" : {
"n" : 4,
"nscanned" : 4,
"nscannedObjects" : 4,
"timems" : 2,
"planSummary" : "DISTINCT { free_choice.value: 1.0 }"
},
"ok" : 1
}
那么...这两个字段之间有什么区别?
是bug还是我做错了什么?
我是 运行 MongoDB 3.0.6 在 Vagrant 盒子里 Ubuntu 14.04.3 LTS
显然这是 MongoDB 核心的错误。或者行为不当。 MongoDB 不会对不同请求的虚线字段使用多键索引。
这是 Mongo 的回复:
The distinct optimization uses a special index access stage which
returns the distinct index keys to its parent stage. In the multikey
dotted case, however, the distinct stage would have to check for null
or undefined keys. In the case of null or undefined, it must fetch the
full document in order to disambiguate between literal nulls versus
null by virtue of missing fields. We have decided to hold off unless
we see that users really need this.
如果您真的想要这个功能,请在这里投票:SERVER-13298
我注意到 MongoDB 在查询字段上的不同值时不会使用索引。我会在某些领域使用它,但不会在其他领域使用它。
示例如下:
db.product.createIndex({"_indexed.preventieve_mondzorg-max_bedrag_p_jr": 1});
db.runCommand({distinct: "product", key:"_indexed.preventieve_mondzorg-max_bedrag_p_jr"});
此查询将不使用建立在该字段上的索引,并将进行完整的集合扫描。这就是它产生的结果:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 50,
"numIndexesAfter" : 50,
"note" : "all indexes already exist",
"ok" : 1
}
{
"values" : [
"€ 250,- | 75%",
"Geen dekking",
"...",
],
"stats" : {
"n" : 33660,
"nscanned" : 0,
"nscannedObjects" : 33660,
"timems" : 12531,
"planSummary" : "COLLSCAN"
},
"ok" : 1
}
另一方面
db.product.createIndex({"free_choice.value": 1});
db.runCommand({distinct: "product", key:"free_choice.value"});
请问指数:
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 50,
"numIndexesAfter" : 50,
"note" : "all indexes already exist",
"ok" : 1
}
{
"values" : [
"gedeeltelijk",
"geen",
"ja"
],
"stats" : {
"n" : 4,
"nscanned" : 4,
"nscannedObjects" : 4,
"timems" : 2,
"planSummary" : "DISTINCT { free_choice.value: 1.0 }"
},
"ok" : 1
}
那么...这两个字段之间有什么区别?
是bug还是我做错了什么?
我是 运行 MongoDB 3.0.6 在 Vagrant 盒子里 Ubuntu 14.04.3 LTS
显然这是 MongoDB 核心的错误。或者行为不当。 MongoDB 不会对不同请求的虚线字段使用多键索引。
这是 Mongo 的回复:
The distinct optimization uses a special index access stage which returns the distinct index keys to its parent stage. In the multikey dotted case, however, the distinct stage would have to check for null or undefined keys. In the case of null or undefined, it must fetch the full document in order to disambiguate between literal nulls versus null by virtue of missing fields. We have decided to hold off unless we see that users really need this.
如果您真的想要这个功能,请在这里投票:SERVER-13298