Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-10357

An update $push with sort is slower than expected

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.5.0
    • Component/s: Performance, Write Ops
    • None
    • ALL
    • Hide

      > db.brsort.drop()
      true
      > db.brsort.insert({_id : 1, list : []})
      > sort
      {
      "ops" : [
      {
      "ns" : "test.brsort",
      "op" : "update",
      "query" :

      { "_id" : 1 }

      ,
      "update" : {
      "$push" : {
      "list" : {
      "$each" : [

      { "b" : 1 }

      ],
      "$slice" : -100,
      "$sort" :

      { "b" : 1 }

      }
      }
      }
      }
      ],
      "parallel" : 1,
      "seconds" : 10
      }
      >benchRun(sort)

      Show
      > db.brsort.drop() true > db.brsort.insert({_id : 1, list : []}) > sort { "ops" : [ { "ns" : "test.brsort", "op" : "update", "query" : { "_id" : 1 } , "update" : { "$push" : { "list" : { "$each" : [ { "b" : 1 } ], "$slice" : -100, "$sort" : { "b" : 1 } } } } } ], "parallel" : 1, "seconds" : 10 } >benchRun(sort)

      Seems like adding a $sort to a $push + $slice commands results in an unreasonably large slowdown. A slowdown is expected for large arrays, but it is surprising to see a 5x change when sorting an already sorted array of length 100.

      Note: I have only tested this on 2.5.0, dont have comparisons between versions

      Results from reproduction code using benchRun on various array sizes:

      Array length 1:
      Sort: 18280
      No sort: 19936
      Change: 1.1x

      Array length 10:
      Sort: 15300
      No sort: 19400
      Change: 1.3x

      Array length 100:
      Sort: 3230
      No sort: 16660
      Change: 5.2x

      Array length 1,000:
      Sort: 230
      No sort: 2176
      Change: 9.5x

      Array length 10,000:
      Sort: 70
      No sort: 450
      Change: 6.4x

      Array length: 100,000:
      Sort: 30
      No sort: 260
      Change: 8.7x

            Assignee:
            andrew.emil@10gen.com Andrew Emil (Inactive)
            Reporter:
            andrew.emil@10gen.com Andrew Emil (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: