我在MongoDB集合上设置了一个复合索引,但无论我如何具体地定位前缀,查询都无法使用它。
索引的创建方式:
db.collection.createIndex({"insert_time":-1,"name":"text"},{background: true})
索引规范输出:
db.collection.getIndexes()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "db.collection"
},
{
"v" : 2,
"key" : {
"insert_time" : -1,
"_fts" : "text",
"_ftsx" : 1
},
"name" : "insert_time_-1_name_text",
"ns" : "db.collection",
"background" : true,
"weights" : {
"name" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
但是,即使是在最简单的insert_time查询中,explain()
也显示出获胜的计划是执行COLLSCAN,而不使用任何索引:
db.collection.find({"insert_time": ISODate("2018-08-
05T19:00:00Z")}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "db.collection",
"indexFilterSet" : false,
"parsedQuery" : {
"insert_time" : {
"$eq" : ISODate("2018-08-05T19:00:00Z")
}
},
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"insert_time" : {
"$eq" : ISODate("2018-08-05T19:00:00Z")
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "foo",
"port" : 0000,
"version" : "3.4.10",
"gitVersion" : "078f28920cb24de0dd479b5ea6c66c644f6326e9"
},
"ok" : 1
}
尽管索引被指定为“复合索引”,但我理解应该可以通过“前缀”进行搜索,这种情况下是
insert_time
。你有任何想法为什么Mongo不使用我的索引吗?