Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-38303

Optimization: short-cut SELECT with aggregation if HAVING has turned false

    XMLWordPrintable

Details

    • Q4/2026 Server Development

    Description

      (Coming from Monty's observations of queries ServiceNow makes)

      Consider a query:

      select ... from t1 where ... having count(value)= 0;
      select ... from t1 where ... having count(value)< 10;
      

      The idea is, as soon as we get enough matching row(s), we know the query would produce an empty resultset. This means we can stop the execution.

      Requirements

      Implicit grouping is needed, or GROUP BY with "HAVING group_col=.." so there's only one group.

      Execution

      The aggregate function could have an attached "end detector". "end detection" will detect when the HAVING condition becomes false and signal to stop the execution.


      Implementation considerations

      We walk the HAVING and AND-parts of HAVING and find there aggregate comparison expression(s) STOPPABLE_EXPR that have the property:

      If the value of aggregate is FALSE for a subset of the group, it will remain
      false for the whole group.
      

      We record the STOPPABLE_EXPR.

      Then, the grouping algorithm can check the STOPPABLE_EXPR after adding a row into group.
      If its value is FALSE, stop the scan.
      (Note that no "end detector" is attached to aggregate functions, as was initially suggested.)

      The criteria for STOPPABLE_EXPR are just pattern matching for:

       COUNT(...) = 0 
       COUNT(...) < literal_const
       COUNT(...) <= literal_const
      

      (and also swap left-and-right sides, e.g. 0=COUNT() ...)

      In the future we can also add

      MAX(...) < const
      

      Attachments

        Activity

          People

            bsrikanth Srikanth Bondalapati
            psergei Sergei Petrunia
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - 15d
                15d
                Remaining:
                Time Spent - 4d Remaining Estimate - 8d
                8d
                Logged:
                Time Spent - 4d Remaining Estimate - 8d Time Not Required
                4d

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.