Skip to content

Commit

Permalink
Merge ../10.1-mdev6657 into bb-10.1-orderby-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
spetrunia committed Sep 24, 2014
2 parents c7d45c2 + 8707f17 commit 15ad0d0
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 23 deletions.
4 changes: 2 additions & 2 deletions mysql-test/r/myisam_explain_non_select_all.result
Expand Up @@ -674,14 +674,14 @@ FLUSH STATUS;
FLUSH TABLES;
EXPLAIN EXTENDED SELECT * FROM t1 WHERE t1.a > 0 ORDER BY t1.a;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE t1 index PRIMARY PRIMARY 4 NULL 3 100.00 Using where; Using index
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 3 100.00 Using where; Using index
Warnings:
Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where (`test`.`t1`.`a` > 0) order by `test`.`t1`.`a`
# Status of EXPLAIN EXTENDED "equivalent" SELECT query execution
Variable_name Value
# Status of "equivalent" SELECT query execution:
Variable_name Value
Handler_read_first 1
Handler_read_key 1
Handler_read_next 3
# Status of testing query execution:
Variable_name Value
Expand Down
6 changes: 3 additions & 3 deletions mysql-test/r/order_by.result
Expand Up @@ -297,7 +297,7 @@ create table t1 (a int not null, b int, c varchar(10), key (a, b, c));
insert into t1 values (1, NULL, NULL), (1, NULL, 'b'), (1, 1, NULL), (1, 1, 'b'), (1, 1, 'b'), (2, 1, 'a'), (2, 1, 'b'), (2, 2, 'a'), (2, 2, 'b'), (2, 3, 'c'),(1,3,'b');
explain select * from t1 where (a = 1 and b is null and c = 'b') or (a > 2) order by a desc;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index a a 22 NULL 11 Using where; Using index
1 SIMPLE t1 range a a 22 NULL 2 Using where; Using index
select * from t1 where (a = 1 and b is null and c = 'b') or (a > 2) order by a desc;
a b c
1 NULL b
Expand Down Expand Up @@ -2569,7 +2569,7 @@ SELECT * FROM t1 r JOIN t1 s ON r.a = s.a
WHERE s.a IN (2,9) OR s.a < 100 AND s.a != 0
ORDER BY 1 LIMIT 10;
id select_type table type possible_keys key key_len ref rows filtered Extra
1 SIMPLE r index PRIMARY PRIMARY 4 NULL 10 100.00 Using where; Using index
1 SIMPLE r range PRIMARY PRIMARY 4 NULL 12 100.00 Using where; Using index
1 SIMPLE s eq_ref PRIMARY PRIMARY 4 test.r.a 1 100.00 Using index
Warnings:
Note 1003 select `test`.`r`.`a` AS `a`,`test`.`s`.`a` AS `a` from `test`.`t1` `r` join `test`.`t1` `s` where ((`test`.`s`.`a` = `test`.`r`.`a`) and ((`test`.`r`.`a` in (2,9)) or ((`test`.`r`.`a` < 100) and (`test`.`r`.`a` <> 0)))) order by 1 limit 10
Expand Down Expand Up @@ -2600,7 +2600,7 @@ CREATE TABLE t1 (a INT,KEY (a));
INSERT INTO t1 VALUES (1),(2),(3),(4),(5),(6),(7),(8),(9),(10);
EXPLAIN SELECT DISTINCT a,1 FROM t1 WHERE a <> 1 ORDER BY a DESC;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index a a 5 NULL 10 Using where; Using index; Using filesort
1 SIMPLE t1 range a a 5 NULL 10 Using where; Using index
SELECT DISTINCT a,1 FROM t1 WHERE a <> 1 ORDER BY a DESC;
a 1
10 1
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/show_explain.result
Expand Up @@ -1036,15 +1036,15 @@ explain
SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 system NULL NULL NULL NULL 1
1 SIMPLE t1 index b b 6 NULL 107 Using where; Using index
1 SIMPLE t1 range b b 6 NULL 107 Using where; Using index
1 SIMPLE t3 ref PRIMARY PRIMARY 5 test.t1.b 1 Using index
set @show_explain_probe_select_id=1;
set debug_dbug='+d,show_explain_probe_do_select';
SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2;
show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t2 system NULL NULL NULL NULL 1 Using filesort
1 SIMPLE t1 index b b 6 NULL 107 Using where; Using index
1 SIMPLE t1 range b b 6 NULL 107 Using where; Using index
1 SIMPLE t3 ref PRIMARY PRIMARY 5 test.t1.b 1 Using index
Warnings:
Note 1003 SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect.result
Expand Up @@ -6974,7 +6974,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down Expand Up @@ -7008,7 +7008,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/subselect_mat.result
Expand Up @@ -103,7 +103,7 @@ explain extended
select * from t1i where a1 in (select max(b1) from t2i where b1 > '0' group by b1);
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1i index NULL # 18 # 3 100.00 #
2 MATERIALIZED t2i index it2i1,it2i3 # 9 # 5 100.00 #
2 MATERIALIZED t2i range it2i1,it2i3 # 9 # 5 100.00 #
Warnings:
Note 1003 select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from `test`.`t1i` where <expr_cache><`test`.`t1i`.`a1`>(<in_optimizer>(`test`.`t1i`.`a1`,`test`.`t1i`.`a1` in ( <materialize> (select max(`test`.`t2i`.`b1`) from `test`.`t2i` where (`test`.`t2i`.`b1` > '0') group by `test`.`t2i`.`b1` ), <primary_index_lookup>(`test`.`t1i`.`a1` in <temporary table> on distinct_key where ((`test`.`t1i`.`a1` = `<subquery2>`.`max(b1)`))))))
select * from t1i where a1 in (select max(b1) from t2i where b1 > '0' group by b1);
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_exists_to_in.result
Expand Up @@ -6974,7 +6974,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down Expand Up @@ -7008,7 +7008,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_mat.result
Expand Up @@ -6968,7 +6968,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down Expand Up @@ -7001,7 +7001,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_opts.result
Expand Up @@ -6965,7 +6965,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down Expand Up @@ -6999,7 +6999,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_scache.result
Expand Up @@ -6980,7 +6980,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down Expand Up @@ -7014,7 +7014,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down
4 changes: 2 additions & 2 deletions mysql-test/r/subselect_no_semijoin.result
Expand Up @@ -6965,7 +6965,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down Expand Up @@ -6999,7 +6999,7 @@ INSERT INTO t2 VALUES (45),(17),(20);
EXPLAIN SELECT * FROM t1 WHERE EXISTS ( SELECT a FROM t1, t2 WHERE b = a GROUP BY a HAVING a <> 1 ) ;
id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE
2 SUBQUERY t1 index a a 5 NULL 2 Using where; Using index
2 SUBQUERY t1 range a a 5 NULL 2 Using where; Using index
2 SUBQUERY t2 ref b b 5 test.t1.a 2 Using index
DROP TABLE t1,t2;
#
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/r/subselect_sj_mat.result
Expand Up @@ -107,7 +107,7 @@ select * from t1i where a1 in (select max(b1) from t2i where b1 > '0' group by b
id select_type table type possible_keys key key_len ref rows filtered Extra
1 PRIMARY t1i index it1i1,it1i3 # 18 # 3 100.00 #
1 PRIMARY <subquery2> eq_ref distinct_key # 8 # 1 100.00 #
2 MATERIALIZED t2i index it2i1,it2i3 # 9 # 5 100.00 #
2 MATERIALIZED t2i range it2i1,it2i3 # 9 # 5 100.00 #
Warnings:
Note 1003 select `test`.`t1i`.`a1` AS `a1`,`test`.`t1i`.`a2` AS `a2` from <materialize> (select max(`test`.`t2i`.`b1`) from `test`.`t2i` where (`test`.`t2i`.`b1` > '0') group by `test`.`t2i`.`b1`) join `test`.`t1i` where (`<subquery2>`.`max(b1)` = `test`.`t1i`.`a1`)
select * from t1i where a1 in (select max(b1) from t2i where b1 > '0' group by b1);
Expand Down
2 changes: 1 addition & 1 deletion sql/opt_range.cc
Expand Up @@ -3064,7 +3064,7 @@ int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
param.alloced_sel_args= 0;

/* Calculate cost of full index read for the shortest covering index */
if (!head->covering_keys.is_clear_all())
if (!force_quick_range && !head->covering_keys.is_clear_all())
{
int key_for_use= find_shortest_key(head, &head->covering_keys);
double key_read_time= head->file->keyread_time(key_for_use, 1, records) +
Expand Down
8 changes: 7 additions & 1 deletion sql/sql_select.cc
Expand Up @@ -20179,7 +20179,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
(tab->join->select_options &
OPTION_FOUND_ROWS) ?
HA_POS_ERROR :
tab->join->unit->select_limit_cnt,0,
tab->join->unit->select_limit_cnt,TRUE,
TRUE, FALSE) <= 0;
if (res)
{
Expand Down Expand Up @@ -20324,6 +20324,12 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
*/
if (!table->covering_keys.is_set(best_key))
table->disable_keyread();
else
{
if (!table->key_read)
table->enable_keyread();
}

if (!quick_created)
{
if (select) // Throw any existing quick select
Expand Down
47 changes: 47 additions & 0 deletions storage/tokudb/mysql-test/tokudb_mariadb/r/mdev6657.result
@@ -0,0 +1,47 @@
drop table if exists t1,t2,t3;
create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t2(a int);
insert into t2 select A.a + B.a* 10 + C.a * 100 from t1 A, t1 B, t1 C;
CREATE TABLE t3 (
col1 int(10) unsigned NOT NULL DEFAULT '0',
col2 mediumint(8) unsigned NOT NULL DEFAULT '0',
col3 smallint(5) NOT NULL DEFAULT '1',
filler varchar(255) DEFAULT NULL,
PRIMARY KEY (col1,col2,col3),
KEY key1 (col1,col2) USING BTREE
) ENGINE=TokuDB DEFAULT CHARSET=latin1 PACK_KEYS=1 COMPRESSION=TOKUDB_LZMA;
insert into t3 select 1300000000+a, 12345, 7890, 'data' from t2;
insert into t3 select 1400000000+a, 12345, 7890, 'data' from t2;
insert into t3 select 1410799999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1410899999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1410999999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411099999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411199999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411299999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411399999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411499999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411599999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411699999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411899999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411999999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1412099999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1412199999+a, 12345, 7890, 'data' from t2;
# The following must use range(PRIMARY):
explain
select col1,col2,col3
from t3
where col1 <= 1410799999
order by col1 desc,col2 desc,col3 desc limit 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 range PRIMARY,key1 PRIMARY 4 NULL 2001 Using where; Using index
# The same query but the constant is bigger.
# The query should use range(PRIMARY), not full index scan:
explain
select col1,col2,col3
from t3
where col1 <= 1412199999
order by col1 desc, col2 desc, col3 desc limit 1;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t3 range PRIMARY,key1 PRIMARY 4 NULL 15001 Using where; Using index
drop table t1,t2,t3;
53 changes: 53 additions & 0 deletions storage/tokudb/mysql-test/tokudb_mariadb/t/mdev6657.test
@@ -0,0 +1,53 @@
--disable_warnings
drop table if exists t1,t2,t3;
--enable_warnings

create table t1(a int);
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t2(a int);
insert into t2 select A.a + B.a* 10 + C.a * 100 from t1 A, t1 B, t1 C;

CREATE TABLE t3 (
col1 int(10) unsigned NOT NULL DEFAULT '0',
col2 mediumint(8) unsigned NOT NULL DEFAULT '0',
col3 smallint(5) NOT NULL DEFAULT '1',
filler varchar(255) DEFAULT NULL,
PRIMARY KEY (col1,col2,col3),
KEY key1 (col1,col2) USING BTREE
) ENGINE=TokuDB DEFAULT CHARSET=latin1 PACK_KEYS=1 COMPRESSION=TOKUDB_LZMA;

insert into t3 select 1300000000+a, 12345, 7890, 'data' from t2;
insert into t3 select 1400000000+a, 12345, 7890, 'data' from t2;
insert into t3 select 1410799999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1410899999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1410999999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411099999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411199999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411299999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411399999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411499999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411599999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411699999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411899999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1411999999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1412099999+a, 12345, 7890, 'data' from t2;
insert into t3 select 1412199999+a, 12345, 7890, 'data' from t2;

--echo # The following must use range(PRIMARY):
explain
select col1,col2,col3
from t3
where col1 <= 1410799999
order by col1 desc,col2 desc,col3 desc limit 1;

--echo # The same query but the constant is bigger.
--echo # The query should use range(PRIMARY), not full index scan:
explain
select col1,col2,col3
from t3
where col1 <= 1412199999
order by col1 desc, col2 desc, col3 desc limit 1;

drop table t1,t2,t3;

0 comments on commit 15ad0d0

Please sign in to comment.