Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4191

Performance of lookup in an embedded relation

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • 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?

            Assignee:
            Unassigned Unassigned
            Reporter:
            stanley90x Marek Stanczyk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: