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

Condition pushdown from HAVING into WHERE

Details

    Description

      Consider a query

      SELECT agg_func(...), ... 
      FROM table ... WHERE ... 
      GROUP BY col 
      HAVING cond1(col) AND cond2(agg_func(...))

      Here, cond1(col) can be pushed down from HAVING into WHERE clause. cond2(agg_func(..)) cannot be pushed down.

      Basically, we can push down conditions that remain constant across the GROUP BY groups. Pushdown allows to filter out the whole groups.

      This task is about

      • splitting the condition that can be pushed down
      • moving from HAVING into WHERE, adjusting it appropriately

      Attachments

        Issue Links

          Activity

            psergei Sergei Petrunia added a comment - See also https://mariadb.com/kb/en/mariadb/moving-from-oracle-to-mariadb/#comment_1839

            Note that despite MDEV-9197 being fixed, this particular issue is not fixed on the current 10.3 tree. I was motivated to check again, because I got this question on the "optimizer status update and plans" session in New York.

            create table t20 (a int, b int, c int); 
            insert into t20 values (1,1,1), (2,2,2), (3,3,3);
            

            An example that shows HAVING->WHERE pushdown is not happening:

            explain format=json 
            select a, max(b) from t20 group by a having a<4;
             
            EXPLAIN: {
              "query_block": {
                "select_id": 1,
                "having_condition": "t20.a < 4",
                "filesort": {
                  "sort_key": "t20.a",
                  "temporary_table": {
                    "table": {
                      "table_name": "t20",
                      "access_type": "ALL",
                      "rows": 6,
                      "filtered": 100
                    }
                  }
                }
              }
            }
            

            An example shows that if one uses derived table, pushdown will happen:

            explain format=json
            select * 
            from
              (select a, max(b) from t20 group by a) TBL
            where 
              TBL.a<4;
             
            EXPLAIN: {
              "query_block": {
                "select_id": 1,
                "table": {
                  "table_name": "<derived2>",
                  "access_type": "ALL",
                  "rows": 6,
                  "filtered": 100,
                  "attached_condition": "TBL.a < 4",
                  "materialized": {
                    "query_block": {
                      "select_id": 2,
                      "filesort": {
                        "sort_key": "t20.a",
                        "temporary_table": {
                          "table": {
                            "table_name": "t20",
                            "access_type": "ALL",
                            "rows": 6,
                            "filtered": 100,
                            "attached_condition": "t20.a < 4"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
            

            psergei Sergei Petrunia added a comment - Note that despite MDEV-9197 being fixed, this particular issue is not fixed on the current 10.3 tree. I was motivated to check again, because I got this question on the "optimizer status update and plans" session in New York. create table t20 (a int, b int, c int); insert into t20 values (1,1,1), (2,2,2), (3,3,3); An example that shows HAVING->WHERE pushdown is not happening: explain format=json select a, max(b) from t20 group by a having a<4;   EXPLAIN: { "query_block": { "select_id": 1, "having_condition": "t20.a < 4", "filesort": { "sort_key": "t20.a", "temporary_table": { "table": { "table_name": "t20", "access_type": "ALL", "rows": 6, "filtered": 100 } } } } } An example shows that if one uses derived table, pushdown will happen: explain format=json select * from (select a, max(b) from t20 group by a) TBL where TBL.a<4;   EXPLAIN: { "query_block": { "select_id": 1, "table": { "table_name": "<derived2>", "access_type": "ALL", "rows": 6, "filtered": 100, "attached_condition": "TBL.a < 4", "materialized": { "query_block": { "select_id": 2, "filesort": { "sort_key": "t20.a", "temporary_table": { "table": { "table_name": "t20", "access_type": "ALL", "rows": 6, "filtered": 100, "attached_condition": "t20.a < 4" } } } } } } } }

            Patches for this task was pushed into 10.4

            igor Igor Babaev (Inactive) added a comment - Patches for this task was pushed into 10.4

            People

              shagalla Galina Shalygina (Inactive)
              psergei Sergei Petrunia
              Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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