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

Performance of lookup in an embedded relation

    XMLWordPrintable

    Details

    • Type: Question
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: 4.0.0 final
    • Fix Version/s: 12_01_17
    • Component/s: None
    • Labels:
      None
    • # Replies:
      3
    • Last comment by Customer:
      true

      Description

      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?

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since reply:
                3 years, 3 weeks, 5 days ago
                Date of 1st Reply: