我有一个带有简单索引的简单类
class A
include Mongoid::Document
field :value
index({value: 1})在命令行中,我重新编制了索引
bundle exec rake db:mongoid:remove_indexes
bundle exec rake db:mongoid:create_indexes但是当我在rails控制台中运行一个简单的查询时
A.where(value: "1").to_amongodb日志清楚地显示了一个COLLSCAN
command test_development.a command: find { find: "a", filter: { value: "1" },
$db: "test_development", lsid: { id: UUID("fa470127-398a-4f06-9a17-57b058017cf7") } }
planSummary: COLLSCAN keysExamined:0 docsExamined:816688 cursorExhausted:1
numYields:6380 nreturned:0 我做错了什么?
更新-添加了更多信息:
db.case_data.getIndexSpecs()
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "simple_development.case_data"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "value_text",
"ns" : "simple_development.case_data",
"weights" : {
"value" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]explain的输出:
db.case_data.find({value: "test"}).explain()
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "simple_development.case_data",
"indexFilterSet" : false,
"parsedQuery" : {
"value" : {
"$eq" : "test"
}
},
"queryHash" : "7E4E9C25",
"planCacheKey" : "7E4E9C25",
"winningPlan" : {
"stage" : "COLLSCAN",
"filter" : {
"value" : {
"$eq" : "test"
}
},
"direction" : "forward"
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "msc-2",
"port" : 27017,
"version" : "4.2.8",
"gitVersion" : "43d25964249164d76d5e04dd6cf38f6111e21f5f"
},
"ok" : 1
}发布于 2020-10-13 21:34:36
在Mongoid 7.1.1和mongodb 4.2.9的某些组合中,rake任务和mongorestore实际上都不会重新创建索引。他们说他们有,但他们没有。
此命令将生成COLLSCAN:
bundle exec rake db:mongoid:remove_indexes
bundle exec rake db:mongoid:create_indexes使用mongo控制台创建索引,或者在Rails控制台中的每个模型中执行以下操作:
A.remove_indexes
A.create_indexes导致没有COLLSCAN。
我已经用干净的数据库恢复科学地重复了这一点,并且可以肯定地说,rake命令和mongorestore命令不会重新创建索引。我上面的解决方案解决了这个问题。
谢谢大家的帮助,凯文
发布于 2020-10-13 16:57:20
我不知道为什么索引是这样创建的,可能是因为您没有在model类中指定字段的类型。尝试指定字段的类型,然后手动删除mongo shell db.getCollection('X').dropIndex('value_text')中的索引,然后运行rake来创建索引rake db:create_indexes。
如果这不起作用,我会删除索引并在mongo shell db.getCollection('X').createIndex({'value': 1}, {name: 'value_1', background: true})中手动创建一个(或者等待一个更好的答案?)
https://stackoverflow.com/questions/64253133
复制相似问题