diff --git a/mysql-test/main/cte_nonrecursive.result b/mysql-test/main/cte_nonrecursive.result index 7ad246941ae..4c0aa2e8cf3 100644 --- a/mysql-test/main/cte_nonrecursive.result +++ b/mysql-test/main/cte_nonrecursive.result @@ -183,7 +183,7 @@ where c in (select c from (select count(*) as c from t1 where b >= 'c' group by a) as t); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where -1 PRIMARY ref key0 key0 8 test.t2.c 2 Using where; FirstMatch(t2) +1 PRIMARY eq_ref distinct_key distinct_key 8 test.t2.c 1 Using where 3 DERIVED t1 ALL NULL NULL NULL NULL 8 Using where; Using temporary; Using filesort # two references to t specified by a query # selecting a field: both in main query @@ -369,7 +369,7 @@ select c as a from t2 where c < 4) select * from t2,t where t2.c=t.a; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where -1 PRIMARY ref key0 key0 5 test.t2.c 1 +1 PRIMARY eq_ref distinct_key distinct_key 5 test.t2.c 1 2 DERIVED t1 ALL NULL NULL NULL NULL 8 Using where 3 UNION t2 ALL NULL NULL NULL NULL 4 Using where NULL UNION RESULT ALL NULL NULL NULL NULL NULL @@ -381,7 +381,7 @@ select c as a from t2 where c < 4) as t where t2.c=t.a; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 4 Using where -1 PRIMARY ref key0 key0 5 test.t2.c 1 +1 PRIMARY eq_ref distinct_key distinct_key 5 test.t2.c 1 2 DERIVED t1 ALL NULL NULL NULL NULL 8 Using where 3 UNION t2 ALL NULL NULL NULL NULL 4 Using where NULL UNION RESULT ALL NULL NULL NULL NULL NULL diff --git a/mysql-test/main/derived.result b/mysql-test/main/derived.result index 6f6fccf35d9..b3acbdfce16 100644 --- a/mysql-test/main/derived.result +++ b/mysql-test/main/derived.result @@ -370,6 +370,15 @@ a 2 3 3 +set @save2_derived_optimizer_switch_bug=@@optimizer_switch; +set @@optimizer_switch=default; +select * from (select * from t1 union distinct select * from t2 union all select * from t3) X; +a +1 +2 +3 +3 +set @@optimizer_switch=@save2_derived_optimizer_switch_bug; drop table t1, t2, t3; create table t1 (a int); create table t2 (a int); @@ -1290,7 +1299,7 @@ id select_type table type possible_keys key key_len ref rows r_rows filtered r_f analyze select * from t1 , ( (select t2.a from t2 order by c) union (select t2.a from t2 order by c))q where t1.a=q.a; id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 Using where -1 PRIMARY ref key0 key0 5 test.t1.a 1 1.00 100.00 100.00 +1 PRIMARY eq_ref distinct_key distinct_key 5 test.t1.a 1 1.00 100.00 100.00 2 DERIVED t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 3 UNION t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 NULL UNION RESULT ALL NULL NULL NULL NULL NULL 6.00 NULL NULL @@ -1333,3 +1342,66 @@ DROP TABLE t1; # # End of 10.3 tests # +# +# Test of "Derived tables and union can now create distinct keys" +# +create table t1 (a int); +insert into t1 values (100),(100); +create table duplicates_tbl (a int); +insert into duplicates_tbl select seq/100 from seq_1_to_10000; +explain +select +t1.a IN ( SELECT COUNT(*) +from (select a +from duplicates_tbl +limit 10000 +) T +where T.a=5 +) as 'A' +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 +2 MATERIALIZED ALL NULL NULL NULL NULL 10000 Using where +3 DERIVED duplicates_tbl ALL NULL NULL NULL NULL 10000 +select +t1.a IN ( SELECT COUNT(*) +from (select a +from duplicates_tbl +limit 10000 +) T +where T.a=5 +) as 'A' +from t1; +A +1 +1 +explain +select +t1.a = all ( SELECT COUNT(*) +from (select a +from duplicates_tbl +limit 10000 +) T +where T.a=5 +) as 'A' +from t1; +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 2 +2 DEPENDENT SUBQUERY ALL NULL NULL NULL NULL 10000 Using where +3 DERIVED duplicates_tbl ALL NULL NULL NULL NULL 10000 +select +t1.a = all ( SELECT COUNT(*) +from (select a +from duplicates_tbl +limit 10000 +) T +where T.a=5 +) as 'A' +from t1; +A +1 +1 +drop table t1, duplicates_tbl; +# +# End of 11.0 tests +# diff --git a/mysql-test/main/derived.test b/mysql-test/main/derived.test index b256637ce26..8c76fe50fe7 100644 --- a/mysql-test/main/derived.test +++ b/mysql-test/main/derived.test @@ -260,6 +260,10 @@ insert into t2 values(2),(2); insert into t3 values(3),(3); select * from t1 union distinct select * from t2 union all select * from t3; select * from (select * from t1 union distinct select * from t2 union all select * from t3) X; +set @save2_derived_optimizer_switch_bug=@@optimizer_switch; +set @@optimizer_switch=default; +select * from (select * from t1 union distinct select * from t2 union all select * from t3) X; +set @@optimizer_switch=@save2_derived_optimizer_switch_bug; drop table t1, t2, t3; # @@ -1128,7 +1132,6 @@ select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a fr drop table t1,t2,t3; - --echo # --echo # MDEV-16549: Server crashes in Item_field::fix_fields on query with --echo # view and subquery, Assertion `context' failed, Assertion `field' failed @@ -1147,3 +1150,61 @@ DROP TABLE t1; --echo # --echo # End of 10.3 tests --echo # + +--echo # +--echo # Test of "Derived tables and union can now create distinct keys" +--echo # + +create table t1 (a int); +insert into t1 values (100),(100); + +create table duplicates_tbl (a int); +insert into duplicates_tbl select seq/100 from seq_1_to_10000; + +explain +select + t1.a IN ( SELECT COUNT(*) + from (select a + from duplicates_tbl + limit 10000 + ) T + where T.a=5 + ) as 'A' +from t1; + +select + t1.a IN ( SELECT COUNT(*) + from (select a + from duplicates_tbl + limit 10000 + ) T + where T.a=5 + ) as 'A' +from t1; + +explain +select + t1.a = all ( SELECT COUNT(*) + from (select a + from duplicates_tbl + limit 10000 + ) T + where T.a=5 + ) as 'A' +from t1; + +select + t1.a = all ( SELECT COUNT(*) + from (select a + from duplicates_tbl + limit 10000 + ) T + where T.a=5 + ) as 'A' +from t1; + +drop table t1, duplicates_tbl; + +--echo # +--echo # End of 11.0 tests +--echo # diff --git a/mysql-test/main/derived_cond_pushdown.result b/mysql-test/main/derived_cond_pushdown.result index 9be0a96ef77..b60359f074c 100644 --- a/mysql-test/main/derived_cond_pushdown.result +++ b/mysql-test/main/derived_cond_pushdown.result @@ -10433,7 +10433,7 @@ WHERE d_tab.e>1 ; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where -1 PRIMARY ref key0 key0 10 test.t1.a,test.t1.b 2 FirstMatch(t1) +1 PRIMARY eq_ref distinct_key distinct_key 10 test.t1.a,test.t1.b 1 3 DERIVED t2 ALL NULL NULL NULL NULL 5 Using where; Using temporary; Using filesort EXPLAIN FORMAT=JSON SELECT * FROM t1 WHERE (t1.a,t1.b) IN @@ -10465,15 +10465,14 @@ EXPLAIN { "table": { "table_name": "", - "access_type": "ref", - "possible_keys": ["key0"], - "key": "key0", + "access_type": "eq_ref", + "possible_keys": ["distinct_key"], + "key": "distinct_key", "key_length": "10", "used_key_parts": ["e", "max_f"], "ref": ["test.t1.a", "test.t1.b"], - "rows": 2, + "rows": 1, "filtered": 100, - "first_match": "t1", "materialized": { "query_block": { "select_id": 3, @@ -10532,7 +10531,7 @@ WHERE d_tab.max_f<25 ; id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 5 Using where -1 PRIMARY ref key0 key0 10 test.t1.a,test.t1.b 2 FirstMatch(t1) +1 PRIMARY eq_ref distinct_key distinct_key 10 test.t1.a,test.t1.b 1 3 DERIVED t2 ALL NULL NULL NULL NULL 5 Using temporary; Using filesort EXPLAIN FORMAT=JSON SELECT * FROM t1 WHERE (t1.a,t1.b) IN @@ -10564,15 +10563,14 @@ EXPLAIN { "table": { "table_name": "", - "access_type": "ref", - "possible_keys": ["key0"], - "key": "key0", + "access_type": "eq_ref", + "possible_keys": ["distinct_key"], + "key": "distinct_key", "key_length": "10", "used_key_parts": ["e", "max_f"], "ref": ["test.t1.a", "test.t1.b"], - "rows": 2, + "rows": 1, "filtered": 100, - "first_match": "t1", "materialized": { "query_block": { "select_id": 3, @@ -13399,7 +13397,7 @@ where ((d1.a<4) and (d1.c<200))); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 9 Using where -1 PRIMARY ref key0 key0 5 test.t2.b 2 Using where +1 PRIMARY ref key1 key1 5 test.t2.b 2 Using where 2 DERIVED t1 ALL NULL NULL NULL NULL 18 Using where; Using temporary; Using filesort 3 INTERSECT t1 ALL NULL NULL NULL NULL 18 Using where; Using temporary; Using filesort NULL INTERSECT RESULT ALL NULL NULL NULL NULL NULL @@ -13432,8 +13430,8 @@ EXPLAIN "table": { "table_name": "", "access_type": "ref", - "possible_keys": ["key0"], - "key": "key0", + "possible_keys": ["key1"], + "key": "key1", "key_length": "5", "used_key_parts": ["b"], "ref": ["test.t2.b"], @@ -14446,7 +14444,7 @@ where ((d1.a>4) and (d1.c>500))); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t2 ALL NULL NULL NULL NULL 9 Using where -1 PRIMARY ref key0 key0 5 test.t2.b 2 Using where +1 PRIMARY ref key1 key1 5 test.t2.b 2 Using where 2 DERIVED t1 ALL NULL NULL NULL NULL 18 Using where; Using temporary; Using filesort 3 EXCEPT t1 ALL NULL NULL NULL NULL 18 Using where; Using temporary; Using filesort NULL EXCEPT RESULT ALL NULL NULL NULL NULL NULL @@ -14479,8 +14477,8 @@ EXPLAIN "table": { "table_name": "", "access_type": "ref", - "possible_keys": ["key0"], - "key": "key0", + "possible_keys": ["key1"], + "key": "key1", "key_length": "5", "used_key_parts": ["b"], "ref": ["test.t2.b"], @@ -19543,7 +19541,7 @@ on (t1.id = t2.ro_id AND t2.flag = 1) group by t1.id) dt); id select_type table type possible_keys key key_len ref rows filtered Extra 1 PRIMARY t1 ALL PRIMARY NULL NULL NULL 20 100.00 -1 PRIMARY ref key0 key0 4 test.t1.id 2 100.00 FirstMatch(t1) +1 PRIMARY ref key1 key1 4 test.t1.id 2 100.00 FirstMatch(t1) 3 DERIVED t1 ALL PRIMARY NULL NULL NULL 20 100.00 Using temporary; Using filesort 3 DERIVED t2 ref ro_id ro_id 4 test.t1.id 1 100.00 Using where Warnings: diff --git a/mysql-test/main/derived_opt.result b/mysql-test/main/derived_opt.result index 4033f381458..051360ffe8c 100644 --- a/mysql-test/main/derived_opt.result +++ b/mysql-test/main/derived_opt.result @@ -323,10 +323,9 @@ JOIN t1 AS tc ON (tb.pk = tc.pk) JOIN t4 AS td ON tc.a = td.a) tu) limit 10; id select_type table type possible_keys key key_len ref rows Extra -1 PRIMARY ALL distinct_key NULL NULL NULL # +1 PRIMARY ALL NULL NULL NULL NULL # 1 PRIMARY tx eq_ref PRIMARY PRIMARY 4 tu.pk # Using index 1 PRIMARY ty eq_ref PRIMARY PRIMARY 4 tu.pk # Using index -2 MATERIALIZED ALL NULL NULL NULL NULL # 3 DERIVED td system PRIMARY NULL NULL NULL # Using temporary 3 DERIVED tc ref PRIMARY,a a 3 const # 3 DERIVED ta eq_ref PRIMARY PRIMARY 4 test.tc.pk # Using index diff --git a/mysql-test/main/myisam_explain_non_select_all.result b/mysql-test/main/myisam_explain_non_select_all.result index 9b98dc77eb7..f14676bb394 100644 --- a/mysql-test/main/myisam_explain_non_select_all.result +++ b/mysql-test/main/myisam_explain_non_select_all.result @@ -3069,14 +3069,14 @@ Warning 1287 ' INTO ;' is deprecated and will be EXPLAIN UPDATE t1, t2 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 Using where -1 PRIMARY ref key0 key0 5 test.t1.a 2 FirstMatch(t1) +1 PRIMARY eq_ref distinct_key distinct_key 5 test.t1.a 1 1 PRIMARY t2 ALL NULL NULL NULL NULL 3 3 DERIVED t2 ALL NULL NULL NULL NULL 3 Using filesort FLUSH STATUS; @@ -3128,7 +3128,7 @@ FLUSH TABLES; EXPLAIN EXTENDED UPDATE t1, t2 SET a = 10 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x); 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 -1 PRIMARY ref key0 key0 5 test.t1.a 2 100.00 FirstMatch(t1) +1 PRIMARY eq_ref distinct_key distinct_key 5 test.t1.a 1 100.00 1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort # Status of EXPLAIN EXTENDED query @@ -3139,11 +3139,11 @@ FLUSH TABLES; EXPLAIN EXTENDED SELECT * FROM t1, t2 WHERE a IN (SELECT * FROM (SELECT b FROM t2 ORDER BY b LIMIT 2,2) x); 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 -1 PRIMARY ref key0 key0 5 test.t1.a 2 100.00 FirstMatch(t1) +1 PRIMARY eq_ref distinct_key distinct_key 5 test.t1.a 1 100.00 1 PRIMARY t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) 3 DERIVED t2 ALL NULL NULL NULL NULL 3 100.00 Using filesort Warnings: -Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`b` AS `b` from `test`.`t1` semi join ((/* select#3 */ select `test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`b` limit 2,2) `x`) join `test`.`t2` where `x`.`b` = `test`.`t1`.`a` +Note 1003 /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t2`.`b` AS `b` from (/* select#3 */ select `test`.`t2`.`b` AS `b` from `test`.`t2` order by `test`.`t2`.`b` limit 2,2) `x` join `test`.`t1` join `test`.`t2` where `x`.`b` = `test`.`t1`.`a` # Status of EXPLAIN EXTENDED "equivalent" SELECT query execution Variable_name Value Handler_read_key 4 @@ -3173,7 +3173,7 @@ Warning 1287 '