SELECT * FROM t1 WHERE EXISTS (SELECT NO_SUCH_TABLE.NO_SUCH_FIELD FROM t1);
ERROR 42S22: Unknown column 'NO_SUCH_TABLE.NO_SUCH_FIELD' in 'field list'
drop table t1;
#
# LP BUG#884657 Wrong result with exists2in , correlated subquery
#
CREATE TABLE t1 ( a varchar(1)) ;
INSERT INTO t1 VALUES ('c'),('b');
CREATE TABLE t2 ( b varchar(1)) ;
INSERT INTO t2 VALUES ('v'),('v'),('c'),(NULL),('x'),('i'),('e'),('p'),('s'),('j'),('z'),('c'),('a'),('q'),('y'),(NULL),('r'),('v'),(NULL),('r');
CREATE TABLE t3 ( a int NOT NULL , b varchar(1)) ;
INSERT INTO t3 VALUES (29,'c');
SELECT *
FROM t1, t2
WHERE EXISTS (
SELECT a
FROM t3
WHERE t3.b = t1.a
AND t3.b <> t2.b
);
a b
c v
c v
c x
c i
c e
c p
c s
c j
c z
c a
c q
c y
c r
c v
c r
INSERT INTO t3 VALUES (2,'c');
alter table t1 add index aa (a);
alter table t3 add index bb (b);
-- EXIST to IN then semijoin (has priority over IN to EXISTS)
set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=on,materialization=off,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
a
c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index aa aa 4 NULL 2 100.00 Using index
1 PRIMARY t3 ALL bb NULL NULL NULL 2 100.00 Using where; FirstMatch(t1); Using join buffer (flat, BNL join)
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` semi join (`test`.`t3`) where (`test`.`t3`.`b` = `test`.`t1`.`a`)
-- EXIST to IN then IN to EXISTS
set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
a
c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL aa 4 NULL 2 100.00 Using where; Using index
2 DEPENDENT SUBQUERY t3 ALL bb NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t3`.`b` from `test`.`t3` where (<cache>(`test`.`t1`.`a`) = `test`.`t3`.`b`)))
-- EXIST2IN then MATERIALIZATION
set optimizer_switch='exists_to_in=on,in_to_exists=off,semijoin=off,materialization=on,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
a
c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL aa 4 NULL 2 100.00 Using where; Using index
2 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 100.00
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (select `test`.`t3`.`b` from `test`.`t3` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where ((`test`.`t1`.`a` = `<subquery2>`.`b`)))))
-- NO EXIST2IN
set optimizer_switch='exists_to_in=off,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
a
c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t3 WHERE t3.b = t1.a);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL aa 4 NULL 2 100.00 Using where; Using index
2 DEPENDENT SUBQUERY t3 ALL bb NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select `test`.`t3`.`a` from `test`.`t3` where (`test`.`t3`.`b` = `test`.`t1`.`a`))
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
drop table t1,t2,t3;
#
# From group_min_max.test
#
create table t1 (
a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(64) default ' '
test.t3 analyze status Table is already up to date
explain select a1,a2,b,c,min(c), max(c) from t1
where exists ( select * from t2
where t2.c in (select c from t3 where t3.c > t1.b) and
t2.c > 'b1' )
group by a1,a2,b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL idx_t1_1 163 NULL 128 Using where; Using index
2 DEPENDENT SUBQUERY t2 index NULL idx_t2_1 163 NULL 164 Using where; Using index
2 DEPENDENT SUBQUERY t3 index NULL idx_t3_1 10 NULL 192 Using where; Using index; FirstMatch(t2); Using join buffer (flat, BNL join)
select a1,a2,b,c,min(c), max(c) from t1
where exists ( select * from t2
where t2.c in (select c from t3 where t3.c > t1.b) and
t2.c > 'b1' )
group by a1,a2,b;
a1 a2 b c min(c) max(c)
a a a a111 a111 d111
a a b e112 e112 h112
a b a i121 i121 l121
a b b m122 m122 p122
b a a a211 a211 d211
b a b e212 e212 h212
b b a i221 i221 l221
b b b m222 m222 p222
c a a a311 a311 d311
c a b e312 e312 h312
c b a i321 i321 l321
c b b m322 m322 p322
d a a a411 a411 d411
d a b e412 e412 h412
d b a i421 i421 l421
d b b m422 m422 p422
explain select a1,a2,b,c,min(c), max(c) from t1
where exists ( select * from t2
where t2.c in (select c from t3 where t3.c > t1.c) and
t2.c > 'b1' )
group by a1,a2,b;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t1 index NULL idx_t1_1 163 NULL 128 Using where; Using index
2 DEPENDENT SUBQUERY t2 index NULL idx_t2_1 163 NULL 164 Using where; Using index
2 DEPENDENT SUBQUERY t3 index NULL idx_t3_1 10 NULL 192 Using where; Using index; FirstMatch(t2); Using join buffer (flat, BNL join)
select a1,a2,b,c,min(c), max(c) from t1
where exists ( select * from t2
where t2.c in (select c from t3 where t3.c > t1.c) and
t2.c > 'b1' )
group by a1,a2,b;
a1 a2 b c min(c) max(c)
a a a a111 a111 d111
a a b e112 e112 h112
a b a i121 i121 l121
a b b m122 m122 p122
b a a a211 a211 d211
b a b e212 e212 h212
b b a i221 i221 l221
b b b m222 m222 p222
c a a a311 a311 d311
c a b e312 e312 h312
c b a i321 i321 l321
c b b m322 m322 o322
d a a a411 a411 d411
d a b e412 e412 h412
d b a i421 i421 l421
d b b m422 m422 o422
drop table t1, t2, t3;
CREATE TABLE t1 ( a INT );
INSERT INTO t1 VALUES (7),(0);
CREATE TABLE t2 ( b INT );
INSERT INTO t2 VALUES (0),(8);
SELECT * FROM t1 WHERE
EXISTS ( SELECT * FROM t2 WHERE b = a )
OR a > 0;
a
7
0
explain extended
SELECT * FROM t1 WHERE
EXISTS ( SELECT * FROM t2 WHERE b = a )
OR a > 0;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 2 100.00
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (<expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,`test`.`t1`.`a` in ( <materialize> (select `test`.`t2`.`b` from `test`.`t2` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where ((`test`.`t1`.`a` = `<subquery2>`.`b`)))))) or (`test`.`t1`.`a` > 0))
drop tables t1,t2;
CREATE TABLE t1 ( a INT );
INSERT INTO t1 VALUES (1),(5);
CREATE TABLE t2 ( b INT ) ENGINE=MyISAM;
INSERT INTO t2 VALUES (1);
CREATE TABLE t3 ( c INT );
INSERT INTO t3 VALUES (4),(5);
SET optimizer_switch='exists_to_in=on,subquery_cache=off,materialization=on,in_to_exists=off,semijoin=off';
explain extended
SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
2 SUBQUERY t2 system NULL NULL NULL NULL 1 100.00
3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.b' of SELECT #3 was resolved in SELECT #2
Note 1003 select (select 1 from dual where (not(((1 is not null) and <in_optimizer>(1,1 in ( <materialize> (select `test`.`t3`.`c` from `test`.`t3` where (`test`.`t3`.`c` is not null) ), <primary_index_lookup>(1 in <temporary table> on distinct_key where ((1 = `<subquery3>`.`c`))))))))) AS `( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )` from `test`.`t1`
SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )
1
1
SET optimizer_switch='exists_to_in=on,subquery_cache=off';
explain extended
SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
2 SUBQUERY t2 system NULL NULL NULL NULL 1 100.00
3 MATERIALIZED t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.b' of SELECT #3 was resolved in SELECT #2
Note 1003 select (select 1 from dual where (not(((1 is not null) and <in_optimizer>(1,1 in ( <materialize> (select `test`.`t3`.`c` from `test`.`t3` where (`test`.`t3`.`c` is not null) ), <primary_index_lookup>(1 in <temporary table> on distinct_key where ((1 = `<subquery3>`.`c`))))))))) AS `( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )` from `test`.`t1`
SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )
1
1
SET optimizer_switch='exists_to_in=off,subquery_cache=off';
explain extended
SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00
2 SUBQUERY t2 system NULL NULL NULL NULL 1 100.00
3 SUBQUERY t3 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t2.b' of SELECT #3 was resolved in SELECT #2
Note 1003 select (select 1 from dual where (not(exists(select `test`.`t3`.`c` from `test`.`t3` where (`test`.`t3`.`c` = 1))))) AS `( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )` from `test`.`t1`
SELECT ( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) ) FROM t1;
( SELECT b FROM t2 WHERE NOT EXISTS ( SELECT c FROM t3 WHERE c = b ) )
1
1
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
drop table t1,t2,t3;
# multi condition test
CREATE TABLE t1 ( a varchar(1), a1 varchar(1)) ;
INSERT INTO t1 VALUES ('c', 'c'), ('b', 'b');
CREATE TABLE t3 ( a int NOT NULL , b varchar(1), b1 varchar(1)) ;
INSERT INTO t3 VALUES (29,'c','c');
INSERT INTO t3 VALUES (2,'c','c');
alter table t1 add index aa (a,a1);
alter table t3 add index bb (b,b1);
-- EXIST to IN then semijoin (has priority over IN to EXISTS)
set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=on,materialization=off,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
a a1
c c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t3 index bb bb 8 NULL 2 100.00 Using where; Using index; LooseScan
1 PRIMARY t1 ref aa aa 8 test.t3.b,test.t3.b1 2 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` semi join (`test`.`t3`) where ((`test`.`t1`.`a` = `test`.`t3`.`b`) and (`test`.`t1`.`a1` = `test`.`t3`.`b1`))
set optimizer_switch='exists_to_in=on,in_to_exists=on,semijoin=off,materialization=off,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
a a1
c c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL aa 8 NULL 2 100.00 Using where; Using index
2 DEPENDENT SUBQUERY t3 index_subquery bb bb 8 func,func 2 100.00 Using index; Using where
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a1`),<exists>(<index_lookup>(<cache>(`test`.`t1`.`a`) in t3 on bb where ((<cache>(`test`.`t1`.`a`) = `test`.`t3`.`b`) and (<cache>(`test`.`t1`.`a1`) = `test`.`t3`.`b1`)))))
-- EXIST2IN then MATERIALIZATION
set optimizer_switch='exists_to_in=on,in_to_exists=off,semijoin=off,materialization=on,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
a a1
c c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL aa 8 NULL 2 100.00 Using where; Using index
2 MATERIALIZED t3 index NULL bb 8 NULL 2 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` where <in_optimizer>((`test`.`t1`.`a`,`test`.`t1`.`a1`),(`test`.`t1`.`a`,`test`.`t1`.`a1`) in ( <materialize> (select `test`.`t3`.`b`,`test`.`t3`.`b1` from `test`.`t3` where 1 ), <primary_index_lookup>(`test`.`t1`.`a` in <temporary table> on distinct_key where ((`test`.`t1`.`a` = `<subquery2>`.`b`) and (`test`.`t1`.`a1` = `<subquery2>`.`b1`)))))
-- NO EXIST2IN
set optimizer_switch='exists_to_in=off,subquery_cache=off';
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
a a1
c c
explain extended
SELECT * FROM t1 WHERE EXISTS ( SELECT * FROM t3 WHERE t3.b = t1.a and t3.b1 = t1.a1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 index NULL aa 8 NULL 2 100.00 Using where; Using index
2 DEPENDENT SUBQUERY t3 ref bb bb 8 test.t1.a,test.t1.a1 2 100.00 Using index
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t1.a1' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a`,`test`.`t1`.`a1` AS `a1` from `test`.`t1` where exists(select 1 from `test`.`t3` where ((`test`.`t3`.`b` = `test`.`t1`.`a`) and (`test`.`t3`.`b1` = `test`.`t1`.`a1`)))
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
drop table t1,t3;
#
# MDEV-159 Assertion about not marked for read failed in
SET optimizer_switch = REPLACE( @@optimizer_switch, '=on', '=off' );
SET optimizer_switch='in_to_exists=on,exists_to_in=on';
CREATE TABLE t1 ( a VARCHAR(1) );
INSERT INTO t1 VALUES ('k'),('m');
CREATE TABLE t2 ( b INT,
c VARCHAR(1),
d VARCHAR(1) NOT NULL );
INSERT INTO t2 VALUES
(4,'j','j'),(6,'v','v');
CREATE ALGORITHM=MERGE VIEW v AS SELECT * FROM t2 WHERE b < 1;
SELECT c FROM v
WHERE EXISTS (
SELECT * FROM t1, t2
WHERE a <= v.d AND b = v.b
);
c
explain extended
SELECT c FROM v
WHERE EXISTS (
SELECT * FROM t1, t2
WHERE a <= v.d AND b = v.b
);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1276 Field or reference 'v.d' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'v.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t2`.`c` AS `c` from `test`.`t2` where (<in_optimizer>(`test`.`t2`.`b`,<exists>(select `test`.`t2`.`b` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` <= `test`.`t2`.`d`) and (<cache>(`test`.`t2`.`b`) = `test`.`t2`.`b`)))) and (`test`.`t2`.`b` < 1))
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
drop view v;
drop table t1,t2;
#
# MDEV-160 Exists2In: Crash in in hp_movelink with subquery_cache=ON
#
SET optimizer_switch = 'in_to_exists=on,subquery_cache=on,exists_to_in=on';
CREATE TABLE t1 (
a VARCHAR(3) NOT NULL,
b VARCHAR(50)
);
INSERT INTO t1 VALUES
('USA','Chinese'),('USA','English'),
('FRA','French'),('ITA','Italian');
CREATE TABLE t2 ( c VARCHAR(3) );
INSERT INTO t2 VALUES ('USA'),('FRA');
SELECT * FROM t1 AS alias1, t1 AS alias2
WHERE EXISTS (
SELECT * FROM t1, t2
WHERE a <= alias2.a AND c = alias1.b
) OR alias1 .a = 'foo';
a b a b
SELECT * FROM t1 AS alias1, t1 AS alias2
WHERE EXISTS (
SELECT * FROM t1, t2
WHERE a <= alias2.a AND c = alias1.a
) OR alias1 .a = 'foo';
a b a b
USA Chinese USA Chinese
USA English USA Chinese
FRA French USA Chinese
USA Chinese USA English
USA English USA English
FRA French USA English
USA Chinese FRA French
USA English FRA French
FRA French FRA French
USA Chinese ITA Italian
USA English ITA Italian
FRA French ITA Italian
drop table t1,t2;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# MDEV-160 Exists2In: Crash in in hp_movelink with subquery_cache=ON
#
SET optimizer_switch = 'in_to_exists=on,subquery_cache=on,exists_to_in=on';
CREATE TABLE t1 (
a VARCHAR(3) NOT NULL,
b VARCHAR(50)
);
INSERT INTO t1 VALUES
('USA','Chinese'),('USA','English'),
('FRA','French'),('ITA','Italian');
CREATE TABLE t2 ( c VARCHAR(3) );
INSERT INTO t2 VALUES ('USA'),('FRA');
SELECT * FROM t1 AS alias1, t1 AS alias2
WHERE EXISTS (
SELECT * FROM t1, t2
WHERE a <= alias2.a AND c = alias1.b
) OR alias1 .a = 'foo';
a b a b
explain extended
SELECT * FROM t1 AS alias1, t1 AS alias2
WHERE EXISTS (
SELECT * FROM t1, t2
WHERE a <= alias2.a AND c = alias1.b
) OR alias1 .a = 'foo';
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 4 100.00
1 PRIMARY alias2 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join)
Warnings:
Note 1276 Field or reference 'test.alias2.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.alias1.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`alias1`.`a` AS `a`,`test`.`alias1`.`b` AS `b`,`test`.`alias2`.`a` AS `a`,`test`.`alias2`.`b` AS `b` from `test`.`t1` `alias1` join `test`.`t1` `alias2` where (<expr_cache><`test`.`alias1`.`b`,`test`.`alias2`.`a`>(<in_optimizer>(`test`.`alias1`.`b`,<exists>(select `test`.`t2`.`c` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` <= `test`.`alias2`.`a`) and (<cache>(`test`.`alias1`.`b`) = `test`.`t2`.`c`))))) or (`test`.`alias1`.`a` = 'foo'))
drop table t1,t2;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# MDEV-245 Exists2In: Wrong result (extra rows) with
# exists_to_in=ON, materialization=OFF, NOT EXISTS subquery
#
SET optimizer_switch='materialization=off,exists_to_in=on';
CREATE TABLE t1 ( a INT ) ;
INSERT INTO t1 VALUES (0),(8),(1);
CREATE TABLE t2 ( b INT ) ;
INSERT INTO t2 VALUES (1),(2),(3);
SELECT * FROM t1 WHERE NOT EXISTS ( SELECT * FROM t2 WHERE b = a );
a
0
8
explain extended
SELECT * FROM t1 WHERE NOT EXISTS ( SELECT * FROM t2 WHERE b = a );
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 3 100.00 Using where
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 3 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (not(((`test`.`t1`.`a` is not null) and <expr_cache><`test`.`t1`.`a`>(<in_optimizer>(`test`.`t1`.`a`,<exists>(select `test`.`t2`.`b` from `test`.`t2` where ((`test`.`t2`.`b` is not null) and (<cache>(`test`.`t1`.`a`) = `test`.`t2`.`b`))))))))
drop table t1,t2;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# MDEV-243 Wrong result (extra or missing rows) with
# exists_to_in + materialization, EXISTS subquery
#
SET optimizer_switch='index_merge=on,index_merge_union=on,index_merge_sort_union=on,index_merge_intersection=on,index_merge_sort_intersection=off,engine_condition_pushdown=off,index_condition_pushdown=on,derived_merge=on,derived_with_keys=on,firstmatch=on,loosescan=on,materialization=on,in_to_exists=on,semijoin=on,partial_match_rowid_merge=on,partial_match_table_scan=on,subquery_cache=on,mrr=off,mrr_cost_based=off,mrr_sort_keys=off,outer_join_with_cache=on,semijoin_with_cache=on,join_cache_incremental=on,join_cache_hashed=on,join_cache_bka=on,optimize_join_buffer_size=off,table_elimination=on,extended_keys=off,exists_to_in=on';
CREATE TABLE t1 ( a VARCHAR(1), b VARCHAR(1) );
INSERT INTO t1 VALUES ('v','v'),('s','v');
SELECT * FROM t1 AS alias
WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b );
a b
s v
explain extended
SELECT * FROM t1 AS alias
WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b );
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY alias ALL NULL NULL NULL NULL 2 100.00 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; FirstMatch(alias); Using join buffer (flat, BNL join)
Warnings:
Note 1276 Field or reference 'test.alias.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.alias.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`alias`.`a` AS `a`,`test`.`alias`.`b` AS `b` from `test`.`t1` `alias` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`alias`.`b`) and (`test`.`alias`.`b` > `test`.`alias`.`a`))
SET optimizer_switch = REPLACE( @@optimizer_switch, '=on', '=off' );
SET optimizer_switch = 'exists_to_in=on,materialization=on,semijoin=off';
SELECT * FROM t1 AS alias
WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b );
a b
s v
explain extended
SELECT * FROM t1 AS alias
WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b );
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY alias ALL NULL NULL NULL NULL 2 100.00 Using where
2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.alias.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.alias.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`alias`.`a` AS `a`,`test`.`alias`.`b` AS `b` from `test`.`t1` `alias` where <in_optimizer>(`test`.`alias`.`b`,<exists>(select `test`.`t1`.`a` from `test`.`t1` where ((`test`.`t1`.`a` > `test`.`alias`.`a`) and (<cache>(`test`.`alias`.`b`) = `test`.`t1`.`a`))))
SET optimizer_switch = 'exists_to_in=on,materialization=on,semijoin=on';
SELECT * FROM t1 AS alias
WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b );
a b
s v
explain extended
SELECT * FROM t1 AS alias
WHERE EXISTS ( SELECT * FROM t1 WHERE a > alias.a AND a = alias.b );
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY alias ALL NULL NULL NULL NULL 2 100.00 Using where
1 PRIMARY t1 ALL NULL NULL NULL NULL 2 100.00 Using where; Start temporary; End temporary
Warnings:
Note 1276 Field or reference 'test.alias.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.alias.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`alias`.`a` AS `a`,`test`.`alias`.`b` AS `b` from `test`.`t1` `alias` semi join (`test`.`t1`) where ((`test`.`t1`.`a` = `test`.`alias`.`b`) and (`test`.`alias`.`b` > `test`.`alias`.`a`))
drop table t1;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# MDEV-403 Wrong result (missing rows) with subquery in
# EXISTS and an OR condition outside
#
CREATE TABLE t1 (a INT);
INSERT INTO t1 VALUES (2),(3);
CREATE TABLE t2 (b INT);
INSERT INTO t2 VALUES (1),(3);
SET optimizer_switch = 'exists_to_in=off,in_to_exists=on';
SELECT * FROM t1 AS alias1, t2 AS alias2
WHERE EXISTS (
SELECT 1 FROM t2 WHERE b = alias1.a AND b > alias2.b
) OR a = 5;
a b
3 1
SET optimizer_switch = 'exists_to_in=on,in_to_exists=on';
SELECT * FROM t1 AS alias1, t2 AS alias2
WHERE EXISTS (
SELECT 1 FROM t2 WHERE b = alias1.a AND b > alias2.b
) OR a = 5;
a b
3 1
explain extended
SELECT * FROM t1 AS alias1, t2 AS alias2
WHERE EXISTS (
SELECT 1 FROM t2 WHERE b = alias1.a AND b > alias2.b
) OR a = 5;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY alias1 ALL NULL NULL NULL NULL 2 100.00
1 PRIMARY alias2 ALL NULL NULL NULL NULL 2 100.00 Using where; Using join buffer (flat, BNL join)
2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where
Warnings:
Note 1276 Field or reference 'test.alias1.a' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.alias2.b' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`alias1`.`a` AS `a`,`test`.`alias2`.`b` AS `b` from `test`.`t1` `alias1` join `test`.`t2` `alias2` where (<expr_cache><`test`.`alias1`.`a`,`test`.`alias2`.`b`>(<in_optimizer>(`test`.`alias1`.`a`,<exists>(select `test`.`t2`.`b` from `test`.`t2` where ((`test`.`t2`.`b` > `test`.`alias2`.`b`) and (<cache>(`test`.`alias1`.`a`) = `test`.`t2`.`b`))))) or (`test`.`alias1`.`a` = 5))
drop table t1, t2;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# MDEV-404: Wrong result (extra rows) with STRAIGHT_JOIN,
# EXISTS subquery, NOT NULL column
# (same as above)
#
SET optimizer_switch = 'exists_to_in=on,in_to_exists=on';
CREATE TABLE t1 (a INT, b VARCHAR(1) NOT NULL);
INSERT INTO t1 VALUES (1,'s'),(2,'e');
SELECT STRAIGHT_JOIN * FROM t1 AS alias1, t1 AS alias2
WHERE EXISTS ( SELECT 1 FROM t1 WHERE b < alias2.b AND a = alias1.a );
a b a b
2 e 1 s
drop table t1;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# MDEV-3800: ORDER BY doesn't work with exists_to_in=ON on
# a query with EXISTS subquery and OR condition
#
SET optimizer_switch = 'in_to_exists=on,exists_to_in=on';
CREATE TABLE t1 (a INT, b VARCHAR(1)) ENGINE=MyISAM;
INSERT INTO t1 VALUES (4,'j'),(6,'v'),(3,'c');
CREATE TABLE t2 (c VARCHAR(1)) ENGINE=MyISAM;
INSERT INTO t2 VALUES ('b'),('y');
SELECT a FROM t1
WHERE EXISTS (
SELECT 1 FROM t2 WHERE c = b
) OR b NOT IN ('U')
ORDER BY a;
a
3
4
6
select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`b` in (select `test`.`t2`.`c` from `test`.`t2` where 1 ) or (`test`.`t1`.`b` <> 'U') order by `test`.`t1`.`a`;
a
3
4
6
drop table t1,t2;
set optimizer_switch=default;
set optimizer_switch='exists_to_in=on';
#
# correct calculation of reserved items (postreview-fix)
#
create table t1 (col1 int, col2 int, col3 int);
insert into t1 values (1,2,3),(2,3,4),(4,5,6);
create table t2 as select * from t1;
explain extended
select * from t1 where exists (select col2 from t2 where t2.col1=t1.col1 and t2.col2=t1.col2);
id select_type table type possible_keys key key_len ref rows filtered Extra
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 100.00
Warnings:
Note 1276 Field or reference 'test.t1.col1' of SELECT #2 was resolved in SELECT #1
Note 1276 Field or reference 'test.t1.col2' of SELECT #2 was resolved in SELECT #1
Note 1003 select `test`.`t1`.`col1` AS `col1`,`test`.`t1`.`col2` AS `col2`,`test`.`t1`.`col3` AS `col3` from `test`.`t1` semi join (`test`.`t2`) where 1
select * from t1 where exists (select col2 from t2 where t2.col1=t1.col1 and t2.col2=t1.col2);
col1 col2 col3
1 2 3
2 3 4
4 5 6
drop table t1,t2;
#
# MDEV-3879: Exists2In: Wrong result (extra row) and unexpected
# warning with exists_to_in=on and a NOT EXISTS subquery