[MDEV-7275] Table elimination is not used for tables inside semi-join Created: 2014-12-05  Updated: 2017-11-05

Status: Open
Project: MariaDB Server
Component/s: Optimizer
Affects Version/s: 10.0.15
Fix Version/s: 10.2

Type: Bug Priority: Major
Reporter: Sergei Petrunia Assignee: Sergei Petrunia
Resolution: Unresolved Votes: 0
Labels: optimizer, semi-join, subquery, table-elimination

Issue Links:
Relates
relates to MDEV-6724 Slow query optimizer with certain sub... Open

 Description   

Table elimination is not applied inside semi-joins. This has adverse effects in a number of cases.

create table ten(a int);
insert into ten values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
 
create table one_k(a int);
insert into one_k select A.a + B.a* 10 + C.a * 100 from ten A, ten B, ten C;
 
create table t0 (a int, b int, c int);
insert into t0 select a,a,a from ten;
 
create table t1 (a int, b int, c int);
insert into t1 select a,a,a from ten;
 
create table t2 (pk int, b int, c int, primary key(pk));
insert into t2 select a,a,a from one_k;
 
create table t3 (pk int, b int, c int, primary key(pk));
insert into t3 select a,a,a from one_k;

explain select * from t0 
where t0.a in (
  select t1.a 
  from t1 
   left join t2 on t2.pk=t1.b
   left join t3 on t3.pk=t1.b
  );

Gives:

+------+-------------+-------+--------+---------------+---------+---------+----------+------+------------------------------------------------------------------+
| id   | select_type | table | type   | possible_keys | key     | key_len | ref      | rows | Extra                                                            |
+------+-------------+-------+--------+---------------+---------+---------+----------+------+------------------------------------------------------------------+
|    1 | PRIMARY     | t0    | ALL    | NULL          | NULL    | NULL    | NULL     |   10 |                                                                  |
|    1 | PRIMARY     | t1    | ALL    | NULL          | NULL    | NULL    | NULL     |   10 | Using where; Start temporary; Using join buffer (flat, BNL join) |
|    1 | PRIMARY     | t2    | eq_ref | PRIMARY       | PRIMARY | 4       | j22.t1.b |    1 | Using where; Using index                                         |
|    1 | PRIMARY     | t3    | eq_ref | PRIMARY       | PRIMARY | 4       | j22.t1.b |    1 | Using where; Using index; End temporary                          |
+------+-------------+-------+--------+---------------+---------+---------+----------+------+------------------------------------------------------------------+

An easy way to check that table elimination is applicable:

set optimizer_switch='semijoin=off';
explain ...
 
+------+--------------------+-------+------+---------------+------+---------+------+------+-------------+
| id   | select_type        | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+------+--------------------+-------+------+---------------+------+---------+------+------+-------------+
|    1 | PRIMARY            | t0    | ALL  | NULL          | NULL | NULL    | NULL |   10 | Using where |
|    2 | DEPENDENT SUBQUERY | t1    | ALL  | NULL          | NULL | NULL    | NULL |   10 | Using where |
+------+--------------------+-------+------+---------------+------+---------+------+------+-------------+
 



 Comments   
Comment by Sergei Petrunia [ 2014-12-05 ]

Another bug showing this query pattern - subqueries with eliminable outer joins.

Generated at Thu Feb 08 07:18:21 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.