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

Merge of derived tables / views is not transparent for semi-join optimizations

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Fixed
    • None
    • 5.5.57
    • Optimizer
    • None

    Description

      For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table

      SELECT * FROM ( Q ) t 
      

      is always executed without semi-join optimization.
      In a general case any merge of a derived table / view blocks usage of semi-join optimizations
      for it.
      Here' s a test case that demonstrate this problem.

      create table t1 (a int) engine=myisam;
      insert into t1  values (5),(3),(2),(7),(2),(5),(1); 
      create table t2 (b int, index idx(b)) engine=myisam;
      insert into t2 values (2),(3),(2),(1),(3),(4);
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      insert into t2 select b+10 from t2;
      analyze table t1,t2;
      explain select a from t1 where a in (select b from t2); 
      explain select * from (select a from t1 where a in (select b from t2)) t; 
      

      For the EXPLAINs from the testcase the following is returned:

      +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
      | id   | select_type | table | type | possible_keys | key  | key_len | ref       | rows | Extra                       |
      +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
      |    1 | PRIMARY     | t1    | ALL  | NULL          | NULL | NULL    | NULL      |    7 | Using where                 |
      |    1 | PRIMARY     | t2    | ref  | idx           | idx  | 5       | test.t1.a |  140 | Using index; FirstMatch(t1) |
      +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
       
      MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t; 
      +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
      | id   | select_type        | table | type           | possible_keys | key  | key_len | ref  | rows | Extra       |
      +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
      |    1 | SIMPLE             | t1    | ALL            | NULL          | NULL | NULL    | NULL |    7 | Using where |
      |    3 | DEPENDENT SUBQUERY | t2    | index_subquery | idx           | idx  | 5       | func |  140 | Using index |
      +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
      

      This is not expected as the merge transformation applied to t converts Q1 to Q.

      Attachments

        Activity

          igor Igor Babaev (Inactive) created issue -
          igor Igor Babaev (Inactive) made changes -
          Field Original Value New Value
          Description For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table
          {noformat}
          SELECT * FROM ( Q ) t
          {noformat|
          is always executed without semi-join optimization.
          In a general case any merge of a derived table / view blocks usage of semi-join optimizations
          for it.
          Here' s a test case that demonstrate this problem.
          {noformat}
          create table t1 (a int) engine=myisam;
          insert into t1 values (5),(3),(2),(7),(2),(5),(1);
          create table t2 (b int, index idx(b)) engine=myisam;
          insert into t2 values (2),(3),(2),(1),(3),(4);
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          explain select a from t1 where a in (select b from t2);
          explain select * from (select a from t1 where a in (select b from t2)) t;
          {noformat}
          For the EXPLAINs from the testcase the following is returned:
          {noformat}
          MariaDB [test]> explain select a from t1 where a in (select b from t2);
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 1 | PRIMARY | t2 | ref | idx | idx | 5 | test.t1.a | 11 | Using index; FirstMatch(t1) |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t;
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 3 | DEPENDENT SUBQUERY | t2 | index_subquery | idx | idx | 5 | func | 89 | Using index |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          {noformat}
          This is not expected as the merge transformation applied to t converts Q1 to Q.
          For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table
          {noformat}
          SELECT * FROM ( Q ) t
          {noformat}
          is always executed without semi-join optimization.
          In a general case any merge of a derived table / view blocks usage of semi-join optimizations
          for it.
          Here' s a test case that demonstrate this problem.
          {noformat}
          create table t1 (a int) engine=myisam;
          insert into t1 values (5),(3),(2),(7),(2),(5),(1);
          create table t2 (b int, index idx(b)) engine=myisam;
          insert into t2 values (2),(3),(2),(1),(3),(4);
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          explain select a from t1 where a in (select b from t2);
          explain select * from (select a from t1 where a in (select b from t2)) t;
          {noformat}
          For the EXPLAINs from the testcase the following is returned:
          {noformat}
          MariaDB [test]> explain select a from t1 where a in (select b from t2);
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 1 | PRIMARY | t2 | ref | idx | idx | 5 | test.t1.a | 11 | Using index; FirstMatch(t1) |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t;
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 3 | DEPENDENT SUBQUERY | t2 | index_subquery | idx | idx | 5 | func | 89 | Using index |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          {noformat}
          This is not expected as the merge transformation applied to t converts Q1 to Q.
          igor Igor Babaev (Inactive) made changes -
          Status Open [ 1 ] In Progress [ 3 ]
          igor Igor Babaev (Inactive) made changes -
          Description For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table
          {noformat}
          SELECT * FROM ( Q ) t
          {noformat}
          is always executed without semi-join optimization.
          In a general case any merge of a derived table / view blocks usage of semi-join optimizations
          for it.
          Here' s a test case that demonstrate this problem.
          {noformat}
          create table t1 (a int) engine=myisam;
          insert into t1 values (5),(3),(2),(7),(2),(5),(1);
          create table t2 (b int, index idx(b)) engine=myisam;
          insert into t2 values (2),(3),(2),(1),(3),(4);
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          insert into t2 select * from t2;
          explain select a from t1 where a in (select b from t2);
          explain select * from (select a from t1 where a in (select b from t2)) t;
          {noformat}
          For the EXPLAINs from the testcase the following is returned:
          {noformat}
          MariaDB [test]> explain select a from t1 where a in (select b from t2);
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 1 | PRIMARY | t2 | ref | idx | idx | 5 | test.t1.a | 11 | Using index; FirstMatch(t1) |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t;
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 3 | DEPENDENT SUBQUERY | t2 | index_subquery | idx | idx | 5 | func | 89 | Using index |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          {noformat}
          This is not expected as the merge transformation applied to t converts Q1 to Q.
          For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table
          {noformat}
          SELECT * FROM ( Q ) t
          {noformat}
          is always executed without semi-join optimization.
          In a general case any merge of a derived table / view blocks usage of semi-join optimizations
          for it.
          Here' s a test case that demonstrate this problem.
          {noformat}
          create table t1 (a int) engine=myisam;
          insert into t1 values (5),(3),(2),(7),(2),(5),(1);
          create table t2 (b int, index idx(b)) engine=myisam;
          insert into t2 values (2),(3),(2),(1),(3),(4);
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          analyze table t1,t2;
          explain select a from t1 where a in (select b from t2);
          explain select * from (select a from t1 where a in (select b from t2)) t;
          {noformat}
          For the EXPLAINs from the testcase the following is returned:
          {noformat}
          MariaDB [test]> explain select a from t1 where a in (select b from t2);
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 1 | PRIMARY | t2 | ref | idx | idx | 5 | test.t1.a | 11 | Using index; FirstMatch(t1) |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t;
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 3 | DEPENDENT SUBQUERY | t2 | index_subquery | idx | idx | 5 | func | 89 | Using index |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          {noformat}
          This is not expected as the merge transformation applied to t converts Q1 to Q.
          Summary Merge of derived tables / views iis not transparent for semi-join optimizations Merge of derived tables / views is not transparent for semi-join optimizations
          igor Igor Babaev (Inactive) made changes -
          Description For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table
          {noformat}
          SELECT * FROM ( Q ) t
          {noformat}
          is always executed without semi-join optimization.
          In a general case any merge of a derived table / view blocks usage of semi-join optimizations
          for it.
          Here' s a test case that demonstrate this problem.
          {noformat}
          create table t1 (a int) engine=myisam;
          insert into t1 values (5),(3),(2),(7),(2),(5),(1);
          create table t2 (b int, index idx(b)) engine=myisam;
          insert into t2 values (2),(3),(2),(1),(3),(4);
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          analyze table t1,t2;
          explain select a from t1 where a in (select b from t2);
          explain select * from (select a from t1 where a in (select b from t2)) t;
          {noformat}
          For the EXPLAINs from the testcase the following is returned:
          {noformat}
          MariaDB [test]> explain select a from t1 where a in (select b from t2);
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 1 | PRIMARY | t2 | ref | idx | idx | 5 | test.t1.a | 11 | Using index; FirstMatch(t1) |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t;
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 3 | DEPENDENT SUBQUERY | t2 | index_subquery | idx | idx | 5 | func | 89 | Using index |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          {noformat}
          This is not expected as the merge transformation applied to t converts Q1 to Q.
          For any query Q for which a semi-join optimization is used the equivalent query Q1 with derived table
          {noformat}
          SELECT * FROM ( Q ) t
          {noformat}
          is always executed without semi-join optimization.
          In a general case any merge of a derived table / view blocks usage of semi-join optimizations
          for it.
          Here' s a test case that demonstrate this problem.
          {noformat}
          create table t1 (a int) engine=myisam;
          insert into t1 values (5),(3),(2),(7),(2),(5),(1);
          create table t2 (b int, index idx(b)) engine=myisam;
          insert into t2 values (2),(3),(2),(1),(3),(4);
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          insert into t2 select b+10 from t2;
          analyze table t1,t2;
          explain select a from t1 where a in (select b from t2);
          explain select * from (select a from t1 where a in (select b from t2)) t;
          {noformat}
          For the EXPLAINs from the testcase the following is returned:
          {noformat}
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
          | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 1 | PRIMARY | t2 | ref | idx | idx | 5 | test.t1.a | 140 | Using index; FirstMatch(t1) |
          +------+-------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+

          MariaDB [test]> explain select * from (select a from t1 where a in (select b from t2)) t;
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          | 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 7 | Using where |
          | 3 | DEPENDENT SUBQUERY | t2 | index_subquery | idx | idx | 5 | func | 140 | Using index |
          +------+--------------------+-------+----------------+---------------+------+---------+------+------+-------------+
          {noformat}
          This is not expected as the merge transformation applied to t converts Q1 to Q.
          igor Igor Babaev (Inactive) made changes -
          Assignee Igor Babaev [ igor ] Oleksandr Byelkin [ sanja ]
          Status In Progress [ 3 ] In Review [ 10002 ]
          igor Igor Babaev (Inactive) made changes -
          Assignee Oleksandr Byelkin [ sanja ] Igor Babaev [ igor ]
          igor Igor Babaev (Inactive) made changes -
          Assignee Igor Babaev [ igor ] Oleksandr Byelkin [ sanja ]

          ok to push

          sanja Oleksandr Byelkin added a comment - ok to push
          sanja Oleksandr Byelkin made changes -
          Status In Review [ 10002 ] Stalled [ 10000 ]
          sanja Oleksandr Byelkin made changes -
          Assignee Oleksandr Byelkin [ sanja ] Igor Babaev [ igor ]
          igor Igor Babaev (Inactive) added a comment - - edited

          The fix for this bug was pushed into the 5.5 tree.

          igor Igor Babaev (Inactive) added a comment - - edited The fix for this bug was pushed into the 5.5 tree.
          igor Igor Babaev (Inactive) made changes -
          Fix Version/s 5.5.57 [ 22539 ]
          Fix Version/s 10.2 [ 14601 ]
          Fix Version/s 5.5 [ 15800 ]
          Fix Version/s 10.0 [ 16000 ]
          Fix Version/s 10.1 [ 16100 ]
          Resolution Fixed [ 1 ]
          Status Stalled [ 10000 ] Closed [ 6 ]
          serg Sergei Golubchik made changes -
          Workflow MariaDB v3 [ 80579 ] MariaDB v4 [ 152073 ]

          People

            igor Igor Babaev (Inactive)
            igor Igor Babaev (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.