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

Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selectivity=4 and use_stat_tables= PREFERABLY

Details

    Description

      MariaDB [test]> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));
      Query OK, 0 rows affected (0.02 sec)
       
      MariaDB [test]> INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
          (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
      Query OK, 15 rows affected (0.01 sec)
      Records: 15  Duplicates: 0  Warnings: 0
      

      Case 1:

      MariaDB [test]> set @@optimizer_use_condition_selectivity=4;
      Query OK, 0 rows affected (0.00 sec)
       
      MariaDB [test]> set @@use_stat_tables= PREFERABLY;
      Query OK, 0 rows affected (0.00 sec)
       
      MariaDB [test]> explain  SELECT 1 FROM t1 AS t1_outer WHERE 
          -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2); 
      +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
      | id   | select_type  | table       | type   | possible_keys | key          | key_len | ref             | rows | Extra                    |
      +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
      |    1 | PRIMARY      | t1_outer    | index  | a             | a            | 10      | NULL            |   16 | Using where; Using index |
      |    1 | PRIMARY      | <subquery2> | eq_ref | distinct_key  | distinct_key | 4       | test.t1_outer.a |    1 |                          |
      |    2 | MATERIALIZED | t1          | range  | NULL          | a            | 5       | NULL            |    5 | Using index for group-by |
      +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
      3 rows in set (0.01 sec)
      

      Case 2:

      MariaDB [test]> set @@optimizer_use_condition_selectivity=1;
      Query OK, 0 rows affected (0.00 sec)
       
      MariaDB [test]> set @@use_stat_tables= NEVER;
      Query OK, 0 rows affected (0.00 sec)
       
      MariaDB [test]> explain  SELECT 1 FROM t1 AS t1_outer WHERE 
          -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2); 
      +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
      | id   | select_type  | table       | type  | possible_keys | key  | key_len | ref                | rows | Extra                    |
      +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
      |    1 | PRIMARY      | <subquery2> | ALL   | distinct_key  | NULL | NULL    | NULL               |    4 |                          |
      |    1 | PRIMARY      | t1_outer    | ref   | a             | a    | 5       | <subquery2>.max(b) |    4 | Using index              |
      |    2 | MATERIALIZED | t1          | range | NULL          | a    | 5       | NULL               |    4 | Using index for group-by |
      +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
      3 rows in set (0.00 sec)
      

      Attachments

        Issue Links

          Activity

            varun Varun Gupta (Inactive) created issue -
            varun Varun Gupta (Inactive) made changes -
            Field Original Value New Value
            Description
            {noformat}
            MariaDB [test]> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));
            Query OK, 0 rows affected (0.02 sec)

            MariaDB [test]> INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
                -> (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
            Query OK, 15 rows affected (0.01 sec)
            Records: 15 Duplicates: 0 Warnings: 0

            MariaDB [test]> set @@optimizer_use_condition_selectivity=4;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= PREFERABLY;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | 1 | PRIMARY | t1_outer | index | a | a | 10 | NULL | 16 | Using where; Using index |
            | 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | test.t1_outer.a | 1 | |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 5 | Using index for group-by |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            3 rows in set (0.01 sec)

            MariaDB [test]> set @@optimizer_use_condition_selectivity=1;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= NEVER;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 4 | |
            | 1 | PRIMARY | t1_outer | ref | a | a | 5 | <subquery2>.max(b) | 4 | Using index |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 4 | Using index for group-by |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            3 rows in set (0.00 sec)

            {noformat}
            varun Varun Gupta (Inactive) made changes -
            Description
            {noformat}
            MariaDB [test]> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));
            Query OK, 0 rows affected (0.02 sec)

            MariaDB [test]> INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
                -> (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
            Query OK, 15 rows affected (0.01 sec)
            Records: 15 Duplicates: 0 Warnings: 0

            MariaDB [test]> set @@optimizer_use_condition_selectivity=4;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= PREFERABLY;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | 1 | PRIMARY | t1_outer | index | a | a | 10 | NULL | 16 | Using where; Using index |
            | 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | test.t1_outer.a | 1 | |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 5 | Using index for group-by |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            3 rows in set (0.01 sec)

            MariaDB [test]> set @@optimizer_use_condition_selectivity=1;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= NEVER;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 4 | |
            | 1 | PRIMARY | t1_outer | ref | a | a | 5 | <subquery2>.max(b) | 4 | Using index |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 4 | Using index for group-by |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            3 rows in set (0.00 sec)

            {noformat}
            {noformat}
            MariaDB [test]> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));
            Query OK, 0 rows affected (0.02 sec)

            MariaDB [test]> INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
                -> (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
            Query OK, 15 rows affected (0.01 sec)
            Records: 15 Duplicates: 0 Warnings: 0
            {noformat}

            *Case 1*:
            {noformat}
            MariaDB [test]> set @@optimizer_use_condition_selectivity=4;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= PREFERABLY;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | 1 | PRIMARY | t1_outer | index | a | a | 10 | NULL | 16 | Using where; Using index |
            | 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | test.t1_outer.a | 1 | |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 5 | Using index for group-by |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            3 rows in set (0.01 sec)
            {noformat}

            *Case 2*:
            {noformat}
            MariaDB [test]> set @@optimizer_use_condition_selectivity=1;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= NEVER;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 4 | |
            | 1 | PRIMARY | t1_outer | ref | a | a | 5 | <subquery2>.max(b) | 4 | Using index |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 4 | Using index for group-by |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            3 rows in set (0.00 sec)
            {noformat}
            varun Varun Gupta (Inactive) made changes -
            Description {noformat}
            MariaDB [test]> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));
            Query OK, 0 rows affected (0.02 sec)

            MariaDB [test]> INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
                -> (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
            Query OK, 15 rows affected (0.01 sec)
            Records: 15 Duplicates: 0 Warnings: 0
            {noformat}

            *Case 1*:
            {noformat}
            MariaDB [test]> set @@optimizer_use_condition_selectivity=4;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= PREFERABLY;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | 1 | PRIMARY | t1_outer | index | a | a | 10 | NULL | 16 | Using where; Using index |
            | 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | test.t1_outer.a | 1 | |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 5 | Using index for group-by |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            3 rows in set (0.01 sec)
            {noformat}

            *Case 2*:
            {noformat}
            MariaDB [test]> set @@optimizer_use_condition_selectivity=1;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= NEVER;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 4 | |
            | 1 | PRIMARY | t1_outer | ref | a | a | 5 | <subquery2>.max(b) | 4 | Using index |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 4 | Using index for group-by |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            3 rows in set (0.00 sec)
            {noformat}
            {noformat}
            MariaDB [test]> CREATE TABLE t1 (a INT, b INT, INDEX (a,b));
            Query OK, 0 rows affected (0.02 sec)

            MariaDB [test]> INSERT INTO t1 (a, b) VALUES (1,1), (1,2), (1,3), (1,4), (1,5),
                (2,2), (2,3), (2,1), (3,1), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6);
            Query OK, 15 rows affected (0.01 sec)
            Records: 15 Duplicates: 0 Warnings: 0
            {noformat}

            *Case 1*:
            {noformat}
            MariaDB [test]> set @@optimizer_use_condition_selectivity=4;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= PREFERABLY;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            | 1 | PRIMARY | t1_outer | index | a | a | 10 | NULL | 16 | Using where; Using index |
            | 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | test.t1_outer.a | 1 | |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 5 | Using index for group-by |
            +------+--------------+-------------+--------+---------------+--------------+---------+-----------------+------+--------------------------+
            3 rows in set (0.01 sec)
            {noformat}

            *Case 2*:
            {noformat}
            MariaDB [test]> set @@optimizer_use_condition_selectivity=1;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> set @@use_stat_tables= NEVER;
            Query OK, 0 rows affected (0.00 sec)

            MariaDB [test]> explain SELECT 1 FROM t1 AS t1_outer WHERE
                -> a IN (SELECT max(b) FROM t1 GROUP BY a HAVING a < 2);
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            | 1 | PRIMARY | <subquery2> | ALL | distinct_key | NULL | NULL | NULL | 4 | |
            | 1 | PRIMARY | t1_outer | ref | a | a | 5 | <subquery2>.max(b) | 4 | Using index |
            | 2 | MATERIALIZED | t1 | range | NULL | a | 5 | NULL | 4 | Using index for group-by |
            +------+--------------+-------------+-------+---------------+------+---------+--------------------+------+--------------------------+
            3 rows in set (0.00 sec)
            {noformat}
            varun Varun Gupta (Inactive) made changes -
            Summary Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selecitivity=4 Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selecitivity=4 and use_stat_tables= PREFERABLY
            varun Varun Gupta (Inactive) made changes -
            Summary Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selecitivity=4 and use_stat_tables= PREFERABLY Query plan changes when we use GROUP BY optimization with optimizer_use_condition_selectivity=4 and use_stat_tables= PREFERABLY
            varun Varun Gupta (Inactive) made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            varun Varun Gupta (Inactive) made changes -
            Assignee Varun Gupta [ varun ] Sergei Petrunia [ psergey ]
            Status In Progress [ 3 ] In Review [ 10002 ]
            psergei Sergei Petrunia made changes -
            Assignee Sergei Petrunia [ psergey ] Varun Gupta [ varun ]
            Status In Review [ 10002 ] Stalled [ 10000 ]
            varun Varun Gupta (Inactive) made changes -
            Fix Version/s 10.0.37 [ 22917 ]
            Fix Version/s 10.1.36 [ 23117 ]
            Fix Version/s 10.2.18 [ 23112 ]
            Fix Version/s 10.3.10 [ 23140 ]
            Fix Version/s 10.4.0 [ 23115 ]
            Fix Version/s 10.2 [ 14601 ]
            Fix Version/s 10.0 [ 16000 ]
            Fix Version/s 10.1 [ 16100 ]
            Fix Version/s 10.3 [ 22126 ]
            Fix Version/s 10.4 [ 22408 ]
            Resolution Fixed [ 1 ]
            Status Stalled [ 10000 ] Closed [ 6 ]
            varun Varun Gupta (Inactive) made changes -
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 89038 ] MariaDB v4 [ 154841 ]

            People

              varun Varun Gupta (Inactive)
              varun Varun Gupta (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 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.