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

Table elimination does not work across derived tables

Details

    Description

      A dataset (not necessarily minimal):

      create table t1 (a int);
      insert into t1 select seq from seq_1_to_10;
       
      create table t11 (
        a int not null,
        b int,
        key(a)
      );
      insert into t11 select A.seq, A.seq+B.seq 
      from 
        seq_1_to_100 A,
        seq_1_to_1000 B;
      

      create table t12 (
        pk int primary key,
        col1 int
      );
      insert into t12 select seq, seq from seq_1_to_100000;
      

      A non-mergeable view where table t12 can be eliminated:

      create view v2b as 
      select 
        t11.a as a,
        count(*) as b
      from
        t11 left join t12 on t12.pk=t11.b
      group by
        t11.a;
      

      EXPLAIN shows it it is indeed eliminated:

      explain
      select t1.*
      from 
        t1 left join v2b on v2b.a=t1.a;
      

      +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
      | id   | select_type     | table      | type | possible_keys | key  | key_len | ref     | rows | Extra       |
      +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
      |    1 | PRIMARY         | t1         | ALL  | NULL          | NULL | NULL    | NULL    | 10   |             |
      |    1 | PRIMARY         | <derived2> | ref  | key0          | key0 | 5       | j5.t1.a | 2    | Using where |
      |    2 | LATERAL DERIVED | t11        | ref  | a             | a    | 4       | j5.t1.a | 1    |             |
      +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
      

      Now, let's add a column from t12 into the select list:

      create view v2c as 
      select 
        t11.a as a,
        max(t12.col1) as b
      from
        t11 left join t12 on t12.pk=t11.b
      group by
        t11.a;
      

      and run a query that doesn't use it:

      explain
      select t1.* 
      from 
        t1 left join v2c on v2c.a=t1.a;
      

      EXPLAIN shows t12 was not eliminated:

      +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
      | id   | select_type     | table      | type   | possible_keys | key     | key_len | ref      | rows | Extra       |
      +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
      |    1 | PRIMARY         | t1         | ALL    | NULL          | NULL    | NULL    | NULL     | 10   |             |
      |    1 | PRIMARY         | <derived2> | ref    | key0          | key0    | 5       | j5.t1.a  | 2    | Using where |
      |    2 | LATERAL DERIVED | t11        | ref    | a             | a       | 4       | j5.t1.a  | 1    |             |
      |    2 | LATERAL DERIVED | t12        | eq_ref | PRIMARY       | PRIMARY | 4       | j5.t11.b | 1    | Using where |
      +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
      

      Attachments

        Issue Links

          Activity

            psergei Sergei Petrunia created issue -
            psergei Sergei Petrunia made changes -
            Field Original Value New Value
            Description A dataset (not necessarily minimal)

            {code}
            create table t1 (a int);
            insert into t1 select seq from seq_1_to_10;

            create table t11 (
              a int not null,
              b int,
              key(a)
            );
            insert into t11 select A.seq, A.seq+B.seq
            from
              seq_1_to_100 A,
              seq_1_to_1000 B;
            {code}
            {code}
            create table t12 (
              pk int primary key,
              col1 int
            );
            insert into t12 select seq, seq from seq_1_to_100000;
            {code}

            {code}
            create view v2b as
            select
              t11.a as a,
              count(*) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}
            {code}
            explain
            select t1.*
            from
              t1 left join v2b on v2b.a=t1.a;
            {code}
            {code}
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            {code}
            create view v2c as
            select
              t11.a as a,
              max(t12.col1) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}
            {code}
            explain
            select t1.*
            from
              t1 left join v2c on v2c.a=t1.a;
            {code}
            {code}
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            | 2 | LATERAL DERIVED | t12 | eq_ref | PRIMARY | PRIMARY | 4 | j5.t11.b | 1 | Using where |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            {code}
            A dataset (not necessarily minimal):

            {code:sql}
            create table t1 (a int);
            insert into t1 select seq from seq_1_to_10;

            create table t11 (
              a int not null,
              b int,
              key(a)
            );
            insert into t11 select A.seq, A.seq+B.seq
            from
              seq_1_to_100 A,
              seq_1_to_1000 B;
            {code}

            {code:sql}
            create table t12 (
              pk int primary key,
              col1 int
            );
            insert into t12 select seq, seq from seq_1_to_100000;
            {code}

            A non-mergeable view where table t12 can be eliminated:
            {code:sql}
            create view v2b as
            select
              t11.a as a,
              count(*) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}
            EXPLAIN shows it it is indeed eliminated:

            {code:sql}
            explain
            select t1.*
            from
              t1 left join v2b on v2b.a=t1.a;
            {code}

            {code}
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            {code}

            Now, let's add a column from t12 into the select list:
            {code:sql}
            create view v2c as
            select
              t11.a as a,
              max(t12.col1) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}

            but make the view to not use it:
            {code:sql}
            explain
            select t1.*
            from
              t1 left join v2c on v2c.a=t1.a;
            {code}

            EXPLAIN shows it is not eliminated:
            {code}
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            | 2 | LATERAL DERIVED | t12 | eq_ref | PRIMARY | PRIMARY | 4 | j5.t11.b | 1 | Using where |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            {code}
            psergei Sergei Petrunia made changes -
            Description A dataset (not necessarily minimal):

            {code:sql}
            create table t1 (a int);
            insert into t1 select seq from seq_1_to_10;

            create table t11 (
              a int not null,
              b int,
              key(a)
            );
            insert into t11 select A.seq, A.seq+B.seq
            from
              seq_1_to_100 A,
              seq_1_to_1000 B;
            {code}

            {code:sql}
            create table t12 (
              pk int primary key,
              col1 int
            );
            insert into t12 select seq, seq from seq_1_to_100000;
            {code}

            A non-mergeable view where table t12 can be eliminated:
            {code:sql}
            create view v2b as
            select
              t11.a as a,
              count(*) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}
            EXPLAIN shows it it is indeed eliminated:

            {code:sql}
            explain
            select t1.*
            from
              t1 left join v2b on v2b.a=t1.a;
            {code}

            {code}
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            {code}

            Now, let's add a column from t12 into the select list:
            {code:sql}
            create view v2c as
            select
              t11.a as a,
              max(t12.col1) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}

            but make the view to not use it:
            {code:sql}
            explain
            select t1.*
            from
              t1 left join v2c on v2c.a=t1.a;
            {code}

            EXPLAIN shows it is not eliminated:
            {code}
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            | 2 | LATERAL DERIVED | t12 | eq_ref | PRIMARY | PRIMARY | 4 | j5.t11.b | 1 | Using where |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            {code}
            A dataset (not necessarily minimal):

            {code:sql}
            create table t1 (a int);
            insert into t1 select seq from seq_1_to_10;

            create table t11 (
              a int not null,
              b int,
              key(a)
            );
            insert into t11 select A.seq, A.seq+B.seq
            from
              seq_1_to_100 A,
              seq_1_to_1000 B;
            {code}

            {code:sql}
            create table t12 (
              pk int primary key,
              col1 int
            );
            insert into t12 select seq, seq from seq_1_to_100000;
            {code}

            A non-mergeable view where table t12 can be eliminated:
            {code:sql}
            create view v2b as
            select
              t11.a as a,
              count(*) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}
            EXPLAIN shows it it is indeed eliminated:

            {code:sql}
            explain
            select t1.*
            from
              t1 left join v2b on v2b.a=t1.a;
            {code}

            {code}
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            +------+-----------------+------------+------+---------------+------+---------+---------+------+-------------+
            {code}

            Now, let's add a column from t12 into the select list:
            {code:sql}
            create view v2c as
            select
              t11.a as a,
              max(t12.col1) as b
            from
              t11 left join t12 on t12.pk=t11.b
            group by
              t11.a;
            {code}

            and run a query that doesn't use it:
            {code:sql}
            explain
            select t1.*
            from
              t1 left join v2c on v2c.a=t1.a;
            {code}

            EXPLAIN shows t12 was not eliminated:
            {code}
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
            | 1 | PRIMARY | <derived2> | ref | key0 | key0 | 5 | j5.t1.a | 2 | Using where |
            | 2 | LATERAL DERIVED | t11 | ref | a | a | 4 | j5.t1.a | 1 | |
            | 2 | LATERAL DERIVED | t12 | eq_ref | PRIMARY | PRIMARY | 4 | j5.t11.b | 1 | Using where |
            +------+-----------------+------------+--------+---------------+---------+---------+----------+------+-------------+
            {code}
            psergei Sergei Petrunia made changes -
            Labels optimizer-feature
            psergei Sergei Petrunia made changes -
            Assignee Sergei Petrunia [ psergey ] Sergei Krivonos [ JIRAUSER49805 ]
            serg Sergei Golubchik made changes -
            Workflow MariaDB v3 [ 124004 ] MariaDB v4 [ 143054 ]
            psergei Sergei Petrunia made changes -
            Labels optimizer-feature optimizer-easy optimizer-feature
            psergei Sergei Petrunia made changes -
            Assignee Sergei Krivonos [ JIRAUSER49805 ] Oleg Smirnov [ JIRAUSER50405 ]
            oleg.smirnov Oleg Smirnov made changes -
            Status Open [ 1 ] In Progress [ 3 ]
            oleg.smirnov Oleg Smirnov made changes -
            Fix Version/s 10.9 [ 26905 ]
            Fix Version/s 10.7 [ 24805 ]
            psergei Sergei Petrunia made changes -
            oleg.smirnov Oleg Smirnov made changes -
            Status In Progress [ 3 ] Stalled [ 10000 ]
            serg Sergei Golubchik made changes -
            Fix Version/s 10.10 [ 27530 ]
            Fix Version/s 10.9 [ 26905 ]
            serg Sergei Golubchik made changes -
            Assignee Oleg Smirnov [ JIRAUSER50405 ] Sergei Golubchik [ serg ]
            serg Sergei Golubchik made changes -
            Assignee Sergei Golubchik [ serg ] Sergei Petrunia [ psergey ]
            Status Stalled [ 10000 ] In Review [ 10002 ]
            serg Sergei Golubchik made changes -
            Priority Major [ 3 ] Critical [ 2 ]
            psergei Sergei Petrunia made changes -
            Assignee Sergei Petrunia [ psergey ] Oleg Smirnov [ JIRAUSER50405 ]
            Status In Review [ 10002 ] Stalled [ 10000 ]
            oleg.smirnov Oleg Smirnov made changes -
            Status Stalled [ 10000 ] In Progress [ 3 ]
            oleg.smirnov Oleg Smirnov made changes -
            Assignee Oleg Smirnov [ JIRAUSER50405 ] Sergei Petrunia [ psergey ]
            Status In Progress [ 3 ] In Review [ 10002 ]
            psergei Sergei Petrunia made changes -
            Status In Review [ 10002 ] Stalled [ 10000 ]
            psergei Sergei Petrunia made changes -
            Status Stalled [ 10000 ] In Testing [ 10301 ]
            psergei Sergei Petrunia made changes -
            Assignee Sergei Petrunia [ psergey ] Elena Stepanova [ elenst ]
            psergei Sergei Petrunia made changes -
            Assignee Elena Stepanova [ elenst ] Oleg Smirnov [ JIRAUSER50405 ]
            Status In Testing [ 10301 ] Stalled [ 10000 ]
            oleg.smirnov Oleg Smirnov made changes -
            oleg.smirnov Oleg Smirnov made changes -
            Status Stalled [ 10000 ] In Testing [ 10301 ]
            psergei Sergei Petrunia made changes -
            Assignee Oleg Smirnov [ JIRAUSER50405 ] Elena Stepanova [ elenst ]
            psergei Sergei Petrunia made changes -
            Affects Version/s 10.2 [ 14601 ]
            Affects Version/s 10.3 [ 22126 ]
            Affects Version/s 10.4 [ 22408 ]
            Affects Version/s 10.5 [ 23123 ]
            Affects Version/s 10.6 [ 24028 ]
            Issue Type Bug [ 1 ] Task [ 3 ]
            oleg.smirnov Oleg Smirnov made changes -
            oleg.smirnov Oleg Smirnov made changes -
            alice Alice Sherepa made changes -
            Assignee Elena Stepanova [ elenst ] Alice Sherepa [ alice ]
            alice Alice Sherepa made changes -
            alice Alice Sherepa made changes -
            alice Alice Sherepa made changes -
            alice Alice Sherepa made changes -
            Status In Testing [ 10301 ] Stalled [ 10000 ]
            alice Alice Sherepa made changes -
            Assignee Alice Sherepa [ alice ] Oleg Smirnov [ JIRAUSER50405 ]
            oleg.smirnov Oleg Smirnov made changes -
            Fix Version/s 10.10.0 [ 27912 ]
            Fix Version/s 10.10 [ 27530 ]
            Resolution Fixed [ 1 ]
            Status Stalled [ 10000 ] Closed [ 6 ]
            Roel Roel Van de Paar made changes -
            ralf.gebhardt Ralf Gebhardt made changes -
            Labels optimizer-easy optimizer-feature Preview_10.10 optimizer-easy optimizer-feature

            People

              oleg.smirnov Oleg Smirnov
              psergei Sergei Petrunia
              Votes:
              0 Vote for this issue
              Watchers:
              7 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.