gpt4 book ai didi

ruby-on-rails - where() 与 find() 的奇怪性能问题

转载 作者:可可西里 更新时间:2023-11-01 09:55:36 27 4
gpt4 key购买 nike

Mongoid 3.1.6 rails 3.2.21MongoDB 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

最佳答案

这是我的假设,为什么第一个要慢几个数量级(我是从 mongo 的角度写的,对 ruby​​ 的了解为零)。

您第一次触发查询时,它不在工作集中,这导致性能下降。它已经存在的连续时间因此性能更好。如果您的文档数量很少,我会发现这种行为很奇怪(因为我希望所有文档都在一个工作集中)。

第二部分 $where让我感到惊讶,因为我希望所有数字都比 find() 大(第一个事件不是这种情况),因为:

The $where provides greater flexibility, but requires that the database processes the JavaScript expression or function for each document in the collection.

关于ruby-on-rails - where() 与 find() 的奇怪性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com