-
Type: Task
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 4.0.0 final
-
Component/s: None
-
None
I have a model with an embeds_many relation, on which I perform several lookups for business logic calculations. I noticed the lookup is slow, which is quite unexpected because the main document is already completely loaded from the database. I tried to look up using the Ruby Array select method and surprisingly it's much faster (simple benchmark on an embedded collection with 520 items):
[15] pry(main)> Benchmark.measure { 10.times { p.positions.where(:start_date.lte => Date.new(2015, 9, 1)).map(&:start_amount) } }.real => 2.168851 [16] pry(main)> Benchmark.measure { 10.times { p.positions.select { |pp| pp.start_date < Date.new(2015, 9, 1) }.map(&:start_amount) } }.real => 0.050369
What is the reason for this? Is there a way to make the where fast, or should I replace it to select everywhere?