Skip to content

Commit

Permalink
MDEV-17518: Range optimization doesn't use ON expressions from nested…
Browse files Browse the repository at this point in the history
… outer joins

Part#2: take into account that join nest that we are marking as constant
might already have constant tables in it. Don't count these tables twice.
  • Loading branch information
spetrunia committed Nov 5, 2018
1 parent f0cf85f commit e058a25
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
15 changes: 15 additions & 0 deletions mysql-test/main/join_outer.result
Expand Up @@ -2564,4 +2564,19 @@ drop table t1,t2,t3;
set optimizer_use_condition_selectivity= @tmp1;
set use_stat_tables= @tmp2;
set histogram_size= @tmp3;
# Another test
CREATE TABLE t1 (i1 int) ;
CREATE TABLE t2 (pk int NOT NULL PRIMARY KEY) ;
CREATE TABLE t3 (pk int NOT NULL, i1 int, PRIMARY KEY (pk)) ;
INSERT INTO t3 VALUES (2, NULL);
CREATE TABLE t4 (pk int NOT NULL, i1 int, PRIMARY KEY (pk), KEY i1 (i1)) ;
CREATE VIEW v4 AS SELECT * FROM t4;
SELECT 1
FROM t3 RIGHT JOIN t1 ON t3.i1 = t1.i1
LEFT JOIN v4
RIGHT JOIN t2 ON v4.i1 = t2.pk ON t1.i1 = t2.pk
WHERE t3.pk IN (2);
1
drop view v4;
drop table t1,t2,t3,t4;
SET optimizer_switch=@save_optimizer_switch;
17 changes: 17 additions & 0 deletions mysql-test/main/join_outer.test
Expand Up @@ -2084,5 +2084,22 @@ set optimizer_use_condition_selectivity= @tmp1;
set use_stat_tables= @tmp2;
set histogram_size= @tmp3;

--echo # Another test
CREATE TABLE t1 (i1 int) ;
CREATE TABLE t2 (pk int NOT NULL PRIMARY KEY) ;
CREATE TABLE t3 (pk int NOT NULL, i1 int, PRIMARY KEY (pk)) ;
INSERT INTO t3 VALUES (2, NULL);

CREATE TABLE t4 (pk int NOT NULL, i1 int, PRIMARY KEY (pk), KEY i1 (i1)) ;
CREATE VIEW v4 AS SELECT * FROM t4;

SELECT 1
FROM t3 RIGHT JOIN t1 ON t3.i1 = t1.i1
LEFT JOIN v4
RIGHT JOIN t2 ON v4.i1 = t2.pk ON t1.i1 = t2.pk
WHERE t3.pk IN (2);

drop view v4;
drop table t1,t2,t3,t4;

SET optimizer_switch=@save_optimizer_switch;
15 changes: 15 additions & 0 deletions mysql-test/main/join_outer_jcl6.result
Expand Up @@ -2575,6 +2575,21 @@ drop table t1,t2,t3;
set optimizer_use_condition_selectivity= @tmp1;
set use_stat_tables= @tmp2;
set histogram_size= @tmp3;
# Another test
CREATE TABLE t1 (i1 int) ;
CREATE TABLE t2 (pk int NOT NULL PRIMARY KEY) ;
CREATE TABLE t3 (pk int NOT NULL, i1 int, PRIMARY KEY (pk)) ;
INSERT INTO t3 VALUES (2, NULL);
CREATE TABLE t4 (pk int NOT NULL, i1 int, PRIMARY KEY (pk), KEY i1 (i1)) ;
CREATE VIEW v4 AS SELECT * FROM t4;
SELECT 1
FROM t3 RIGHT JOIN t1 ON t3.i1 = t1.i1
LEFT JOIN v4
RIGHT JOIN t2 ON v4.i1 = t2.pk ON t1.i1 = t2.pk
WHERE t3.pk IN (2);
1
drop view v4;
drop table t1,t2,t3,t4;
SET optimizer_switch=@save_optimizer_switch;
set join_cache_level=default;
show variables like 'join_cache_level';
Expand Down
19 changes: 11 additions & 8 deletions sql/sql_select.cc
Expand Up @@ -4316,14 +4316,17 @@ void mark_join_nest_as_const(JOIN *join,
}
JOIN_TAB *tab= tbl->table->reginfo.join_tab;

tab->type= JT_CONST;
tab->info= ET_IMPOSSIBLE_ON_CONDITION;
tab->table->const_table= 1;

join->const_table_map|= tab->table->map;
*found_const_table_map|= tab->table->map;
set_position(join,(*const_count)++,tab,(KEYUSE*) 0);
mark_as_null_row(tab->table); // All fields are NULL
if (!(join->const_table_map & tab->table->map))
{
tab->type= JT_CONST;
tab->info= ET_IMPOSSIBLE_ON_CONDITION;
tab->table->const_table= 1;

join->const_table_map|= tab->table->map;
*found_const_table_map|= tab->table->map;
set_position(join,(*const_count)++,tab,(KEYUSE*) 0);
mark_as_null_row(tab->table); // All fields are NULL
}
}
}

Expand Down

0 comments on commit e058a25

Please sign in to comment.