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

when sharded, no need to merge groups if $group _id is the shard key or original document _id

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major - P3
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 3.1 Required
    • Component/s: Aggregation Framework
    • Labels:
      None

      Description

      Copied from SERVER-4961:

      On sharded environment, using early grouping, besides the use of an index, it would be nice that we be able to avoid the mongos regrouping process.

      I'll try to explain that:

        * result_node1: [
           {
             id: "value1",
             totalcount: 50
           },
           {
             id: "value2",
             totalcount: 100
           },
         ]
       * result_node2: [
           {
             id: "value1",
             totalcount: 60
           }
         ]
      

      The real results(after mongos regroup) must looks like:

       [
           {
             id: "value1",
             totalcount: 110
           },
           {
             id: "value2",
             totalcount: 100
           },
       ]
      

      But, in some cases, mongos regrouping process is nonsense since the grouping key is same as sharding key. So, never got same group key from different shards.

      So, the prior example, now looks like:

        * result_node1: [
           {
             id: "value1",
             totalcount: 110
           }
         ]
       * result_node2: [
           {
             id: "value2",
             totalcount: 100
           }
         ]
      

      The real results must looks like:

       [
           {
             id: "value1",
             totalcount: 110
           },
           {
             id: "value2",
             totalcount: 100
           },
       ]
      

      So, the point is mongos regrouping process is a waste of time when you group using same key as sharding key.

        Activity

        Hide
        samuelgmartinez Samuel García Martínez added a comment - - edited

        Since in this case mongos regrouping process isn't needed, mongos shouldn't fetch entire resultset, sending limit/skip to shards (if there is no sort operation).

        Show
        samuelgmartinez Samuel García Martínez added a comment - - edited Since in this case mongos regrouping process isn't needed, mongos shouldn't fetch entire resultset, sending limit/skip to shards (if there is no sort operation).
        Hide
        samuelgmartinez Samuel García Martínez added a comment -

        I developed a fix for this issue. Is there any process or prerrequisites to do a pull request on Github with this fix?

        To give this more accuracy, $group _id can be a superset of shardkey too.

        Show
        samuelgmartinez Samuel García Martínez added a comment - I developed a fix for this issue. Is there any process or prerrequisites to do a pull request on Github with this fix? To give this more accuracy, $group _id can be a superset of shardkey too.
        Hide
        ian@10gen.com Ian Whalen added a comment -

        @samuel, the first step is to fill out the contributor agreement - http://www.10gen.com/contributor - and then open a pull request at https://github.com/mongodb/mongo/pulls

        Show
        ian@10gen.com Ian Whalen added a comment - @samuel, the first step is to fill out the contributor agreement - http://www.10gen.com/contributor - and then open a pull request at https://github.com/mongodb/mongo/pulls
        Hide
        samuelgmartinez Samuel García Martínez added a comment -

        Hi. I submitted a pull request for this issue. I hope it helps.

        https://github.com/mongodb/mongo/pull/294

        Show
        samuelgmartinez Samuel García Martínez added a comment - Hi. I submitted a pull request for this issue. I hope it helps. https://github.com/mongodb/mongo/pull/294
        Hide
        asya Asya Kamsky added a comment -

        The same optimization can be extended if $group is done on the original document _id field (as would be the case if you $unwind and $group by _id to process some array in the document).

        Show
        asya Asya Kamsky added a comment - The same optimization can be extended if $group is done on the original document _id field (as would be the case if you $unwind and $group by _id to process some array in the document).
        Hide
        jon.rangel@10gen.com Jon Rangel added a comment -

        This optimization should also support hashed shard keys.

        e.g. if sharding on {foo:"hashed"}, grouping on "foo" should employ the optimization.

        Show
        jon.rangel@10gen.com Jon Rangel added a comment - This optimization should also support hashed shard keys. e.g. if sharding on { foo:"hashed" }, grouping on "foo" should employ the optimization.

          People

          • Votes:
            7 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

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