ExportXMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 9.0.0, 9.0.2
    • Component/s: None
    • Query Optimization
    • ALL
    • Hide

      This is the problematic query: 

      ```javascript

      [
        {
          "$lookup": {
            "from": "orders",
            "let":

      {         "let_product_id___1": "$product_id"       }

      ,
            "pipeline": [
              {
                "$group": {
                  "_id":

      {               "product_id": "$product_id"             }

      ,
                  "count":

      {               "$sum": 1             }

                }
              },
              {
                "$sort":

      {             "_id": 1           }

              },
              {
                "$project":

      {             "_id": false,             "product_id": "$_id.product_id",             "count": true           }

              },
              {
                "$project":

      {             "_id": false,             "product_id": "$product_id",             "count": "$count"           }

              },
              {
                "$match": {
                  "$expr":

      {               "$eq": [                 "$$let_product_id___1",                 "$product_id"               ]             }

                }
              }
            ],
            "as": "join_alias_mongo8 orders grouped by product category and id - Product"
          }
        },
        {
          "$unwind":

      {       "path": "$join_alias_mongo8 orders grouped by product category and id - Product",       "preserveNullAndEmptyArrays": true     }

        },
        {
          "$group": {
            "_id": null,
            "count":

      {         "$sum": 1       }

          }
        },
        {
          "$sort":

      {       "_id": 1     }

        },
        {
          "$project":

      {       "_id": false,       "count": true     }

        }
      ]

      ```

      I noticed that the performance is restored if a limit is added above the `$match`, which i believe prevents the optimizer from moving the `$match`. 

       

      ```javascript

      [
        {
          "$lookup": {
            "from": "orders",
            "let":

      {         "let_product_id___1": "$product_id"       }

      ,
            "pipeline": [
              {
                "$group": {
                  "_id":

      {               "product_id": "$product_id"             }

      ,
                  "count":

      {               "$sum": 1             }

                }
              },
              {
                "$sort":

      {             "_id": 1           }

              },
              {
                "$project":

      {             "_id": false,             "product_id": "$_id.product_id",             "count": true           }

              },
              {
                "$project":

      {             "_id": false,             "product_id": "$product_id",             "count": "$count"           }

              },
             

      {           "$limit": 9223372036854775807         }

      ,
              {
                "$match": {
                  "$expr":

      {               "$eq": [                 "$$let_product_id___1",                 "$product_id"               ]             }

                }
              }
            ],
            "as": "join_alias_mongo9, orders grouped by product id and category - Product"
          }
        },
        {
          "$unwind":

      {       "path": "$join_alias_mongo9, orders grouped by product id and category - Product",       "preserveNullAndEmptyArrays": true     }

        },
        {
          "$group": {
            "_id": null,
            "count":

      {         "$sum": 1       }

          }
        },
        {
          "$sort":

      {       "_id": 1     }

        },
        {
          "$project":

      {       "_id": false,       "count": true     }

        }
      ]

      ```

      Show
      This is the problematic query:  ```javascript [   {     "$lookup": {       "from": "orders",       "let": {         "let_product_id___1": "$product_id"       } ,       "pipeline": [         {           "$group": {             "_id": {               "product_id": "$product_id"             } ,             "count": {               "$sum": 1             }           }         },         {           "$sort": {             "_id": 1           }         },         {           "$project": {             "_id": false,             "product_id": "$_id.product_id",             "count": true           }         },         {           "$project": {             "_id": false,             "product_id": "$product_id",             "count": "$count"           }         },         {           "$match": {             "$expr": {               "$eq": [                 "$$let_product_id___1",                 "$product_id"               ]             }           }         }       ],       "as": "join_alias_mongo8 orders grouped by product category and id - Product"     }   },   {     "$unwind": {       "path": "$join_alias_mongo8 orders grouped by product category and id - Product",       "preserveNullAndEmptyArrays": true     }   },   {     "$group": {       "_id": null,       "count": {         "$sum": 1       }     }   },   {     "$sort": {       "_id": 1     }   },   {     "$project": {       "_id": false,       "count": true     }   } ] ``` I noticed that the performance is restored if a limit is added above the `$match`, which i believe prevents the optimizer from moving the `$match`.    ```javascript [   {     "$lookup": {       "from": "orders",       "let": {         "let_product_id___1": "$product_id"       } ,       "pipeline": [         {           "$group": {             "_id": {               "product_id": "$product_id"             } ,             "count": {               "$sum": 1             }           }         },         {           "$sort": {             "_id": 1           }         },         {           "$project": {             "_id": false,             "product_id": "$_id.product_id",             "count": true           }         },         {           "$project": {             "_id": false,             "product_id": "$product_id",             "count": "$count"           }         },         {           "$limit": 9223372036854775807         } ,         {           "$match": {             "$expr": {               "$eq": [                 "$$let_product_id___1",                 "$product_id"               ]             }           }         }       ],       "as": "join_alias_mongo9, orders grouped by product id and category - Product"     }   },   {     "$unwind": {       "path": "$join_alias_mongo9, orders grouped by product id and category - Product",       "preserveNullAndEmptyArrays": true     }   },   {     "$group": {       "_id": null,       "count": {         "$sum": 1       }     }   },   {     "$sort": {       "_id": 1     }   },   {     "$project": {       "_id": false,       "count": true     }   } ] ```
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Joining a collection to the result of an aggregation on a collection takes much longer in Mongo 9 than on Mongo 8 (> 180 seconds vs. < 10 seconds before). I believe this is due to the optimizer re-ordering the `$match` clause, introduced in this commit.

        1. explain_9.0.2_noindex.json
          8 kB
        2. explain_9.0.2_index.json
          8 kB
        3. explain_8.3.6_noindex.json
          7 kB

            Assignee:
            Vesko Karaganev
            Reporter:
            Riley Thompson (EXT)
            Votes:
            0 Vote for this issue
            Watchers:
            13 Start watching this issue

              Created:
              Updated: