Mongoid 3.1.6 Rails 3.2.21 MongoDB 2.4.9
我们发现使用 find()
和 where().first
存在奇怪的性能问题:
$ rails c
2.1.5 :001 > Benchmark.ms { User.find('5091e4beccbce30200000006') }
=> 7.95
2.1.5 :002 > Benchmark.ms { User.find('5091e4beccbce30200000006') }
=> 0.27599999999999997
2.1.5 :003 > Benchmark.ms { User.find('5091e4beccbce30200000006') }
=> 0.215
2.1.5 :004 > exit
$ rails c
2.1.5 :001 > Benchmark.ms { User.where(id: '5091e4beccbce30200000006').first }
=> 7.779999999999999
2.1.5 :002 > Benchmark.ms { User.where(id: '5091e4beccbce30200000006').first }
=> 4.84
2.1.5 :003 > Benchmark.ms { User.where(id: '5091e4beccbce30200000006').first }
=> 5.297
2.1.5 :004 > exit
这两个似乎都会触发相同的查询。有人可以解释一下为什么我们在性能上看到如此巨大的差异吗?
配置:
production:
sessions:
default:
uri: <%= REDACTED %>
options:
consistency: :strong
safe: true
max_retries: 1
retry_interval: 0
options:
identity_map_enabled: true
id
将成为MongoDB内部的_id
。Mongoid通常会为您将字符串转换为ObjectId。 - mu is too shortwhere(...).first
比find
花费更长时间)。 - mu is too short