Commit a21f5a4b authored by Varun Gupta's avatar Varun Gupta

Tests moved to one file

parent 8c79e090
This diff is collapsed.
......@@ -845,3 +845,446 @@ set use_sort_nest= 0;
eval $query;
drop table t0,t1,t2,t3,t4;
CREATE TABLE t1 (a int, b int, c int, KEY a_b (a,b), KEY a_c (a,c));
insert into t1 values (0,1,0), (0,2,0), (0,3,0), (0,4,0), (0,5,0), (0,6,0);
insert into t1 values (1,7,1), (1,8,1), (1,9,1), (1,10,1), (1,11,1), (1,12,1);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
--echo #
--echo # index a_b should be used, no need for filesort
--echo #
let $query= select a,b,c from t1 where a=1 and c=2 order by b limit 5;
set use_sort_nest= 1;
eval $query;
eval explain $query;
set use_sort_nest= 0;
eval explain $query;
drop table t1;
--echo #
--echo # Tests where Index(scan, ref or range access) satisfies the ORDERING
--echo #
CREATE TABLE t1 (a int, b int, c int, KEY a_b (a,b), KEY a_c (a,c));
insert into t1 values (0,1,0), (0,2,0), (0,3,0), (0,4,0), (0,5,0), (0,6,0);
insert into t1 values (1,7,1), (1,8,1), (1,9,1), (1,10,1), (1,11,1), (1,12,1);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,1,2);
--echo #
--echo # index key a_b, no need for filesort
--echo # Also index condition pushdown is used with use_sort_nest=1
--echo #
let $query= select a,b from t1 where a=1 and c=2 order by b limit 10;
set use_sort_nest=1;
eval $query;
eval explain $query;
set use_sort_nest=0;
eval explain $query;
drop table t1;
CREATE TABLE t1(
a int NOT NULL,
b char NULL,
PRIMARY KEY(a)
);
INSERT INTO t1 VALUES (1,'a'), (2,'b'), (3,'c'), (4,'d');
--echo #
--echo # Should use index condition
--echo #
let $query= SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a LIMIT 2;
set use_sort_nest= 1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
--echo #
--echo # Should not use index condition as ORDER by DESC is used
--echo #
let $query= SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a DESC LIMIT 2;
set use_sort_nest= 1;
eval EXPLAIN $query;
eval $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
drop table t1;
create table t1(a int, b int, c int, key(a), key a_b(a,b)); # 10 rows
insert into t1 values (0,1,0), (0,2,0), (0,3,0);
insert into t1 values (1,6,1), (1,7,1), (1,5,1);
insert into t1 values (2,8,2), (2,9,3), (2,10,4);
insert into t1 values (3,1,5);
create table t2(a int, b int, c int, key(b), key(c)); # 10 rows
insert into t2 select a, b, c from t1;
--echo #
--echo # Testing using of Indexes on first non-const table
--echo #
--echo #
--echo # Using range scan
--echo #
let $query=
SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.b > 8 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
set use_sort_nest= 1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
--echo #
--echo # Using ref access
--echo #
let $query=
SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.c >= 1 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
set use_sort_nest= 1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
drop table t1,t2;
--echo # TESTS with INDEX HINTS
set use_sort_nest=1;
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int,c int, key idx1(a), key idx2(a,b), key idx3(c));
insert into t1 select a,a,a from t0 where a <5; # 5 rows
--disable_result_log
analyze table t1 persistent for all;
--enable_result_log
--echo #
--echo # Index idx1 to be used for index scan
--echo #
let $query= SELECT * from t1 where b > 0 order by t1.a limit 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Index idx2 to be used for index scan(USE INDEX is used)
--echo #
let $query= SELECT * from t1 USE INDEX(idx2) WHERE b > 0 ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Index idx2 to be used for index scan(USE INDEX for ORDER BY is used)
--echo #
let $query=
SELECT * from t1 USE INDEX FOR ORDER BY(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Use Filesort as idx3 does not resolve ORDER BY clause
--echo #
let $query=
SELECT * from t1 USE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Using index idx2 as idx1 is ignored
--echo #
let $query=
SELECT * from t1 IGNORE INDEX(idx1)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Use index idx2 for sorting, it is forced here
--echo #
let $query=
SELECT * from t1 FORCE INDEX(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Use FILESORT as idx3 cannot resolve ORDER BY clause
--echo #
let $query=
SELECT * from t1 FORCE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
drop table t0,t1;
--echo #
--echo # SORT-NEST WITH SEMI JOINS
--echo #
CREATE TABLE t0(a int);
CREATE TABLE t1 (a int, b int, c int, key(a), key(b));
CREATE TABLE t2 (a int, b int, c int, key(a));
CREATE TABLE t3 (a int, b int, c int, key(a));
CREATE TABLE t4 (a int, b int, c int, key(a));
INSERT INTO t0 SELECT seq-1 FROM seq_1_to_10;
INSERT INTO t1 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_100;
INSERT INTO t2 SELECT a,a,a FROM t0;
INSERT INTO t3 SELECT a,a,a FROM t0 WHERE a < 5;
INSERT INTO t3 SELECT a,a,a FROM t0 WHERE a < 5;
INSERT INTO t4 SELECT a,a,a FROM t0 WHERE a < 5;
INSERT INTO t4 SELECT a,a,a FROM t0 WHERE a < 5;
--disable_result_log
ANALYZE TABLE t0 PERSISTENT FOR ALL;
ANALYZE TABLE t1 PERSISTENT FOR ALL;
ANALYZE TABLE t2 PERSISTENT FOR ALL;
ANALYZE TABLE t3 PERSISTENT FOR ALL;
ANALYZE TABLE t4 PERSISTENT FOR ALL;
--enable_result_log
--echo #
--echo # SJM scan inside the sort-nest
--echo #
let $query=
SELECT t1.a, t2.a, t1.b,t2.b
FROM t1, t2, t2 ot
WHERE t1.a=t2.a AND t2.c=ot.b AND t2.b < 4 AND
t1.b IN (SELECT it1.b FROM t3 it1,t4 it2
WHERE it1.a < 4 AND it1.a=it2.a)
ORDER BY t2.b DESC ,t1.b DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
--echo #
--echo # SJM Lookup with sort-nest, where SJM lookup table is outside the
--echo # sort-nest
--echo #
let $query=
SELECT t1.a, t2.a, t1.b,t2.b
FROM t1, t2, t2 ot
WHERE t1.a=t2.a AND t2.b=ot.a AND t2.b < 4 AND
t1.b IN (SELECT it1.b FROM t3 it1,t4 it2
WHERE it1.a < 4 AND it1.a=it2.a)
ORDER BY t2.b DESC ,t1.b DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
drop table t0,t1,t2,t3,t4;
--echo #
--echo # Firstmatch strategy
--echo #
CREATE TABLE t0(a int);
CREATE TABLE t1 (a int, b int, c int);
CREATE TABLE t2 (a int, b int, c int, key(a));
CREATE TABLE t3 (a int, b int, c int, key(b));
CREATE TABLE t4 (a int, b int, c int, key(a));
INSERT INTO t0 SELECT seq-1 FROM seq_1_to_10;
INSERT INTO t1 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_100;
INSERT INTO t2 SELECT a,a,a FROM t0;
INSERT INTO t3 SELECT a,a,a FROM t0 where a < 5;
INSERT INTO t3 SELECT a,a,a FROM t0 where a < 5;
INSERT INTO t4 SELECT a,a,a FROM t0 where a < 5;
INSERT INTO t4 SELECT a,a,a FROM t0 where a < 5;
--disable_result_log
ANALYZE TABLE t0 PERSISTENT FOR ALL;
ANALYZE TABLE t1 PERSISTENT FOR ALL;
ANALYZE TABLE t2 PERSISTENT FOR ALL;
ANALYZE TABLE t3 PERSISTENT FOR ALL;
ANALYZE TABLE t4 PERSISTENT FOR ALL;
--enable_result_log
let $query=
SELECT t1.a, t2.a, t1.b,t2.b
FROM t1, t2
WHERE t1.a=t2.a AND
t1.b IN (SELECT t3.b FROM t3,t4 WHERE t3.a <= 5 and t3.c = t2.b)
ORDER BY t2.b DESC, t1.b DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval $query;
set use_sort_nest=0;
eval EXPLAIN $query;
eval $query;
--echo #
--echo # Duplicate weedout
--echo #
set @save_optimizer_switch= @@optimizer_switch;
set optimizer_switch='firstmatch=off';
let $query=
SELECT t1.a, t2.a, t1.b,t2.b
FROM t1, t2
WHERE t1.a=t2.a AND
t1.b IN (SELECT t3.b FROM t3,t4 WHERE t3.a <= 5 and t3.c = t2.b)
ORDER BY t2.b DESC, t1.b DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval $query;
set use_sort_nest=0;
eval EXPLAIN $query;
eval $query;
set @@optimizer_switch= @save_optimizer_switch;
drop table t0,t1,t2,t3,t4;
--echo
--echo # NON-MERGED SEMI JOINS
--echo
create table t0 (a int);
INSERT INTO t0 SELECT seq-1 FROM seq_1_to_10;
create table t1 (a int, b int);
insert into t1 SELECT a,a from t0 where a <5;
create table t2 as SELECT * from t1 where a < 5;
create table t3(a int, b int);
INSERT INTO t3 SELECT seq-1, seq-1 FROM seq_1_to_100;
--echo <subquery2> outside the sort-nest
let $query=
SELECT * from t2,t1
WHERE t2.b=t1.b
AND
t1.a IN (SELECT max(t3.a) FROM t3 GROUP BY t3.b)
ORDER BY t2.a DESC,t1.a DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
--echo <subquery2> inside the sort-nest
let $query=
SELECT * FROM t3,t2
WHERE t3.b=t2.b AND
t3.a IN (SELECT max(t1.a) FROM t1 GROUP BY t1.b)
ORDER BY t3.a DESC,t2.a DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
DROP TABLE t0,t1,t2,t3;
......@@ -370,7 +370,7 @@ c_nationkey c_name o_orderDate o_totalprice c_acctbal n_name
18 Customer#000000082 1997-07-20 15082.82 9468.34 CHINA
10 Customer#000000055 1997-07-16 46380.69 4572.11 IRAN
#
# Sort-nest on table (lineitem, customer)
# Sort-nest on table (lineitem, orders)
#
set use_sort_nest=1;
explain SELECT
......@@ -385,12 +385,13 @@ AND
l_orderkey between 5500 AND 6000
AND
l_shipDATE >= '1997-01-01' and l_shipDATE <= '1998-08-10'
ORDER BY l_extendedprice DESC, c_acctbal DESC
ORDER BY l_extendedprice DESC, o_orderDATE DESC
LIMIT 20;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE lineitem range PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey_quantity 4 NULL 330 Using index condition; Using where; Using temporary; Using filesort
1 SIMPLE orders eq_ref PRIMARY,i_o_custkey PRIMARY 4 dbt3.lineitem.l_orderkey 1 Using where
1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3.orders.o_custkey 1
1 SIMPLE lineitem range PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey_quantity 4 NULL 338 Using index condition; Using where
1 SIMPLE orders eq_ref PRIMARY,i_o_custkey PRIMARY 4 dbt3.lineitem.l_orderkey 1
1 SIMPLE <sort-nest> ALL NULL NULL NULL NULL 20 Using filesort
1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 sort-nest.o_custkey 1
SELECT
c_acctbal, c_name, o_orderDate, l_extendedprice, l_orderkey
FROM
......@@ -403,7 +404,7 @@ AND
l_orderkey between 5500 AND 6000
AND
l_shipDATE >= '1997-01-01' and l_shipDATE <= '1998-08-10'
ORDER BY l_extendedprice DESC, c_acctbal DESC
ORDER BY l_extendedprice DESC, o_orderDATE DESC
LIMIT 20;
c_acctbal c_name o_orderDate l_extendedprice l_orderkey
1842.49 Customer#000000124 1997-11-06 54759.5 5857
......@@ -439,10 +440,10 @@ AND
l_orderkey between 5500 AND 6000
AND
l_shipDATE >= '1997-01-01' and l_shipDATE <= '1998-08-10'
ORDER BY l_extendedprice DESC, c_acctbal DESC
ORDER BY l_extendedprice DESC, o_orderDATE DESC
LIMIT 20;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE lineitem range PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey_quantity 4 NULL 330 Using index condition; Using where; Using temporary; Using filesort
1 SIMPLE lineitem range PRIMARY,i_l_shipdate,i_l_orderkey,i_l_orderkey_quantity i_l_orderkey_quantity 4 NULL 338 Using index condition; Using where; Using temporary; Using filesort
1 SIMPLE orders eq_ref PRIMARY,i_o_custkey PRIMARY 4 dbt3.lineitem.l_orderkey 1 Using where
1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3.orders.o_custkey 1
SELECT
......@@ -457,7 +458,7 @@ AND
l_orderkey between 5500 AND 6000
AND
l_shipDATE >= '1997-01-01' and l_shipDATE <= '1998-08-10'
ORDER BY l_extendedprice DESC, c_acctbal DESC
ORDER BY l_extendedprice DESC, o_orderDATE DESC
LIMIT 20;
c_acctbal c_name o_orderDate l_extendedprice l_orderkey
1842.49 Customer#000000124 1997-11-06 54759.5 5857
......
......@@ -152,7 +152,7 @@ eval $query;
--echo #
--echo # Sort-nest on table (lineitem, customer)
--echo # Sort-nest on table (lineitem, orders)
--echo #
let $query= SELECT
......@@ -167,7 +167,7 @@ let $query= SELECT
l_orderkey between 5500 AND 6000
AND
l_shipDATE >= '1997-01-01' and l_shipDATE <= '1998-08-10'
ORDER BY l_extendedprice DESC, c_acctbal DESC
ORDER BY l_extendedprice DESC, o_orderDATE DESC
LIMIT 20;
......
CREATE TABLE t1 (a int, b int, c int, KEY a_b (a,b), KEY a_c (a,c));
insert into t1 values (0,1,0), (0,2,0), (0,3,0), (0,4,0), (0,5,0), (0,6,0);
insert into t1 values (1,7,1), (1,8,1), (1,9,1), (1,10,1), (1,11,1), (1,12,1);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
#
# index a_b should be used, no need for filesort
#
set use_sort_nest= 1;
select a,b,c from t1 where a=1 and c=2 order by b limit 5;
a b c
1 7 2
1 7 2
1 8 2
1 8 2
1 9 2
explain select a,b,c from t1 where a=1 and c=2 order by b limit 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a_b,a_c a_b 5 NULL 10 Using index condition; Using where
set use_sort_nest= 0;
explain select a,b,c from t1 where a=1 and c=2 order by b limit 5;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a_b,a_c a_b 5 NULL 18 Using where
drop table t1;
#
# Tests where Index(scan, ref or range access) satisfies the ORDERING
#
CREATE TABLE t1 (a int, b int, c int, KEY a_b (a,b), KEY a_c (a,c));
insert into t1 values (0,1,0), (0,2,0), (0,3,0), (0,4,0), (0,5,0), (0,6,0);
insert into t1 values (1,7,1), (1,8,1), (1,9,1), (1,10,1), (1,11,1), (1,12,1);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,1,2);
# index key a_b, no need for filesort
set optimizer_trace=1;
set use_sort_nest=1;
select a,b,c from t1 where a=1 and c=2 order by b limit 10;
a b c
1 1 2
1 7 2
1 7 2
1 8 2
1 8 2
1 9 2
1 9 2
1 10 2
1 10 2
1 11 2
explain select a,b,c from t1 where a=1 and c=2 order by b limit 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a_b,a_c a_b 5 NULL 19 Using index condition; Using where
set use_sort_nest=0;
explain select a,b,c from t1 where a=1 and c=2 order by b limit 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a_b,a_c a_b 5 NULL 19 Using where
drop table t1;
CREATE TABLE t1(
a int NOT NULL,
b char NULL,
PRIMARY KEY(a)
);
INSERT INTO t1 VALUES (1,'a'), (2,'b'), (3,'c'), (4,'d');
#
# Should use index condition
#
set use_sort_nest= 1;
SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a LIMIT 2;
a b
1 a
2 b
EXPLAIN SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 2 Using index condition
set use_sort_nest= 0;
EXPLAIN SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 2 Using index condition
#
# Should not use index condition as ORDER by DESC is used
#
set use_sort_nest= 1;
EXPLAIN SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a DESC LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 2 Using where
SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a DESC LIMIT 2;
a b
2 b
1 a
set use_sort_nest= 0;
EXPLAIN SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a DESC LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range PRIMARY PRIMARY 4 NULL 2 Using where
drop table t1;
create table t1(a int, b int, c int, key(a), key a_b(a,b));
insert into t1 values (0,1,0), (0,2,0), (0,3,0);
insert into t1 values (1,6,1), (1,7,1), (1,5,1);
insert into t1 values (2,8,2), (2,9,3), (2,10,4);
insert into t1 values (3,1,5);
create table t2(a int, b int, c int, key(b), key(c));
insert into t2 select a, b, c from t1;
#
# Testing using of Indexes on first non-const table
#
#
# Using range scan
#
set use_sort_nest= 1;
SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.b > 8 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
a b c a b c
2 9 3 2 9 3
2 10 4 2 10 4
EXPLAIN SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.b > 8 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a,a_b a_b 10 NULL 2 Using index condition
1 SIMPLE t2 ref b b 5 test.t1.b 1
set use_sort_nest= 0;
EXPLAIN SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.b > 8 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a,a_b a_b 10 NULL 2 Using index condition
1 SIMPLE t2 ref b b 5 test.t1.b 1
#
# Using ref access
#
set use_sort_nest= 1;
SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.c >= 1 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
a b c a b c
2 8 2 2 8 2
2 9 3 2 9 3
2 10 4 2 10 4
EXPLAIN SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.c >= 1 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref a,a_b a_b 5 const 3 Using index condition; Using where
1 SIMPLE t2 ref b,c b 5 test.t1.b 1 Using where
set use_sort_nest= 0;
EXPLAIN SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.c >= 1 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref a,a_b a_b 5 const 3 Using where
1 SIMPLE t2 ref b,c b 5 test.t1.b 1 Using where
drop table t1,t2;
# TESTS with INDEX HINTS
set use_sort_nest=1;
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int,c int, key idx1(a), key idx2(a,b), key idx3(c));
insert into t1 select a,a,a from t0 where a <5;
analyze table t1 persistent for all;
#
# Index idx1 to be used for index scan
#
set use_sort_nest=1;
SELECT * from t1 where b > 0 order by t1.a limit 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 where b > 0 order by t1.a limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx1 5 NULL 2 Using where
set use_sort_nest=0;
EXPLAIN SELECT * from t1 where b > 0 order by t1.a limit 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx1 5 NULL 2 Using where
#
# Index idx2 to be used for index scan(USE INDEX is used)
#
set use_sort_nest=1;
SELECT * from t1 USE INDEX(idx2) WHERE b > 0 ORDER BY t1.a LIMIT 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 USE INDEX(idx2) WHERE b > 0 ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
set use_sort_nest=0;
EXPLAIN SELECT * from t1 USE INDEX(idx2) WHERE b > 0 ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
#
# Index idx2 to be used for index scan(USE INDEX for ORDER BY is used)
#
set use_sort_nest=1;
SELECT * from t1 USE INDEX FOR ORDER BY(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 USE INDEX FOR ORDER BY(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
set use_sort_nest=0;
EXPLAIN SELECT * from t1 USE INDEX FOR ORDER BY(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
#
# Use Filesort as idx3 does not resolve ORDER BY clause
#
set use_sort_nest=1;
SELECT * from t1 USE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 USE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where; Using filesort
set use_sort_nest=0;
EXPLAIN SELECT * from t1 USE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where; Using filesort
#
# Using index idx2 as idx1 is ignored
#
set use_sort_nest=1;
SELECT * from t1 IGNORE INDEX(idx1)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 IGNORE INDEX(idx1)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
set use_sort_nest=0;
EXPLAIN SELECT * from t1 IGNORE INDEX(idx1)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
#
# Use index idx2 for sorting, it is forced here
#
set use_sort_nest=1;
SELECT * from t1 FORCE INDEX(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 FORCE INDEX(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
set use_sort_nest=0;
EXPLAIN SELECT * from t1 FORCE INDEX(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL idx2 10 NULL 2 Using where
#
# Use FILESORT as idx3 cannot resolve ORDER BY clause
#
set use_sort_nest=1;
SELECT * from t1 FORCE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
a b c
1 1 1
2 2 2
EXPLAIN SELECT * from t1 FORCE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where; Using filesort
set use_sort_nest=0;
EXPLAIN SELECT * from t1 FORCE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using where; Using filesort
drop table t0,t1;
CREATE TABLE t1 (a int, b int, c int, KEY a_b (a,b), KEY a_c (a,c));
insert into t1 values (0,1,0), (0,2,0), (0,3,0), (0,4,0), (0,5,0), (0,6,0);
insert into t1 values (1,7,1), (1,8,1), (1,9,1), (1,10,1), (1,11,1), (1,12,1);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
--echo #
--echo # index a_b should be used, no need for filesort
--echo #
let $query= select a,b,c from t1 where a=1 and c=2 order by b limit 5;
set use_sort_nest= 1;
eval $query;
eval explain $query;
set use_sort_nest= 0;
eval explain $query;
drop table t1;
--echo #
--echo # Tests where Index(scan, ref or range access) satisfies the ORDERING
--echo #
CREATE TABLE t1 (a int, b int, c int, KEY a_b (a,b), KEY a_c (a,c));
insert into t1 values (0,1,0), (0,2,0), (0,3,0), (0,4,0), (0,5,0), (0,6,0);
insert into t1 values (1,7,1), (1,8,1), (1,9,1), (1,10,1), (1,11,1), (1,12,1);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,7,2), (1,8,2), (1,9,2), (1,10,2), (1,11,2), (1,12,2);
insert into t1 values (1,1,2);
--echo # index key a_b, no need for filesort
let $query= select a,b,c from t1 where a=1 and c=2 order by b limit 10;
set optimizer_trace=1;
set use_sort_nest=1;
eval $query;
eval explain $query;
set use_sort_nest=0;
eval explain $query;
drop table t1;
CREATE TABLE t1(
a int NOT NULL,
b char NULL,
PRIMARY KEY(a)
);
INSERT INTO t1 VALUES (1,'a'), (2,'b'), (3,'c'), (4,'d');
--echo #
--echo # Should use index condition
--echo #
let $query= SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a LIMIT 2;
set use_sort_nest= 1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
--echo #
--echo # Should not use index condition as ORDER by DESC is used
--echo #
let $query= SELECT * FROM t1 WHERE a BETWEEN 1 and 2 ORDER BY a DESC LIMIT 2;
set use_sort_nest= 1;
eval EXPLAIN $query;
eval $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
drop table t1;
create table t1(a int, b int, c int, key(a), key a_b(a,b)); # 10 rows
insert into t1 values (0,1,0), (0,2,0), (0,3,0);
insert into t1 values (1,6,1), (1,7,1), (1,5,1);
insert into t1 values (2,8,2), (2,9,3), (2,10,4);
insert into t1 values (3,1,5);
create table t2(a int, b int, c int, key(b), key(c)); # 10 rows
insert into t2 select a, b, c from t1;
--echo #
--echo # Testing using of Indexes on first non-const table
--echo #
--echo #
--echo # Using range scan
--echo #
let $query=
SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.b > 8 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
set use_sort_nest= 1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
--echo #
--echo # Using ref access
--echo #
let $query=
SELECT * FROM t1,t2
WHERE
t1.a=2 AND t2.c >= 1 AND
t1.b=t2.b
ORDER BY t1.b LIMIT 10;
set use_sort_nest= 1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest= 0;
eval EXPLAIN $query;
drop table t1,t2;
--echo # TESTS with INDEX HINTS
set use_sort_nest=1;
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int,c int, key idx1(a), key idx2(a,b), key idx3(c));
insert into t1 select a,a,a from t0 where a <5; # 5 rows
--disable_result_log
analyze table t1 persistent for all;
--enable_result_log
--echo #
--echo # Index idx1 to be used for index scan
--echo #
let $query= SELECT * from t1 where b > 0 order by t1.a limit 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Index idx2 to be used for index scan(USE INDEX is used)
--echo #
let $query= SELECT * from t1 USE INDEX(idx2) WHERE b > 0 ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Index idx2 to be used for index scan(USE INDEX for ORDER BY is used)
--echo #
let $query=
SELECT * from t1 USE INDEX FOR ORDER BY(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Use Filesort as idx3 does not resolve ORDER BY clause
--echo #
let $query=
SELECT * from t1 USE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Using index idx2 as idx1 is ignored
--echo #
let $query=
SELECT * from t1 IGNORE INDEX(idx1)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Use index idx2 for sorting, it is forced here
--echo #
let $query=
SELECT * from t1 FORCE INDEX(idx2)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
--echo #
--echo # Use FILESORT as idx3 cannot resolve ORDER BY clause
--echo #
let $query=
SELECT * from t1 FORCE INDEX FOR ORDER BY(idx3)
WHERE b > 0
ORDER BY t1.a LIMIT 2;
set use_sort_nest=1;
eval $query;
eval EXPLAIN $query;
set use_sort_nest=0;
eval EXPLAIN $query;
drop table t0,t1;
This diff is collapsed.
--source include/have_sequence.inc
--echo #
--echo # SORT-NEST WITH SEMI JOINS
--echo #
--echo
--echo # MERGED SEMI-JOINS
--echo
--echo # SEMI JOIN MATERIALIZATION SCAN with SORT-NEST
CREATE TABLE t0(a int);
CREATE TABLE t1 (a int, b int, c int);
CREATE TABLE t2 (a int, b int, c int);
CREATE TABLE t3 (a int, b int, c int, key(a));
CREATE TABLE t4 (a int, b int, c int, key(a));
INSERT INTO t0 SELECT seq-1 FROM seq_1_to_10;
INSERT INTO t1 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_100;
INSERT INTO t2 SELECT a,a,a FROM t0;
INSERT INTO t3 SELECT a,a,a FROM t0;
INSERT INTO t4 SELECT a,a,a FROM t0;
--disable_result_log
ANALYZE TABLE t0 PERSISTENT FOR ALL;
ANALYZE TABLE t1 PERSISTENT FOR ALL;
ANALYZE TABLE t2 PERSISTENT FOR ALL;
ANALYZE TABLE t3 PERSISTENT FOR ALL;
ANALYZE TABLE t4 PERSISTENT FOR ALL;
--enable_result_log
--echo # SJM scan inside the sort-nest
--echo # sort-nest includes (t2, <subquery2>)
let $query= SELECT t1.a, t2.a, t1.b,t2.b
FROM t1, t2
WHERE t1.a=t2.a AND
t1.b IN (SELECT t3.b FROM t3,t4
WHERE t3.a < 3 AND t3.a=t4.a)
ORDER BY t1.b DESC ,t2.b DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
--echo #
--echo # SJM scan table is the first table inside the sort-nest
--echo #
alter table t2 add key(b);
let $query= SELECT t1.a, t2.a, t1.b,t2.b
FROM t1, t2
WHERE t1.a=t2.a AND t2.b < 5 AND
t1.b IN (SELECT t3.b FROM t3,t4
WHERE t3.a < 3 AND t3.a=t4.a)
ORDER BY t2.b DESC, t1.b DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval $query;
set use_sort_nest= 0;
eval $query;
DROP TABLE t0, t1, t2, t3, t4;
--echo #
--echo # SJM Lookup with sort-nest, where SJM lookup table is outside the
--echo # sort-nest
--echo #
create table t1 (a int, b int, c int, key(a));
create table t2 (a int, b int, c int, key(c));
create table t3 (a int, b int, c int, key(a));
create table t4 (a int, b int, c int);
INSERT INTO t1 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_10;
INSERT INTO t2 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_100;
INSERT INTO t3 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_1000;
INSERT INTO t4 SELECT seq-1, seq-1, seq-1 FROM seq_1_to_100;
--disable_result_log
ANALYZE TABLE t1 PERSISTENT FOR ALL;
ANALYZE TABLE t2 PERSISTENT FOR ALL;
ANALYZE TABLE t3 PERSISTENT FOR ALL;
ANALYZE TABLE t4 PERSISTENT FOR ALL;
--enable_result_log
let $query= SELECT t1.a, t2.a, t2.b
FROM t1, t2
WHERE t2.a in (SELECT t3.b from t3)
AND t1.a= t2.b
AND t1.a < 5
ORDER BY t1.b DESC, t2.a DESC
LIMIT 5;
set use_sort_nest= 1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest= 0;
eval $query;
DROP TABLE t1, t2, t3, t4;
--echo #
--echo # Firstmatch strategy
--echo #
set @save_optimizer_switch=@@optimizer_switch;
create table t0(a int);
insert t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int, b int, c int, key(a));
insert t1 SELECT a,a,a from t0;
create table t2 as SELECT * from t1;
create table t3 as SELECT * from t1;
let $query= SELECT * FROM t1, t2
WHERE t1.a=t2.a AND
t1.b IN (SELECT b FROM t3 WHERE t3.c<=t2.c)
ORDER BY t2.c DESC, t1.c DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
set optimizer_switch='firstmatch=off';
--echo #
--echo # Duplicate Weedout strategy
--echo #
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
set optimizer_switch=@save_optimizer_switch;
DROP TABLE t0,t1,t2,t3;
--echo
--echo # NON-MERGED SEMI JOINS
--echo
create table t0 (a int);
INSERT INTO t0 SELECT seq-1 FROM seq_1_to_10;
create table t1 (a int, b int);
insert into t1 SELECT a,a from t0 where a <5;
create table t2 as SELECT * from t1 where a < 5;
create table t3(a int, b int);
INSERT INTO t3 SELECT seq-1, seq-1 FROM seq_1_to_100;
--echo <subquery2> outside the sort-nest
let $query= SELECT * from t2,t1
WHERE t2.b=t1.b
AND
t1.a IN (SELECT max(t3.a) FROM t3 GROUP BY t3.b)
ORDER BY t2.a DESC,t1.a DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
--echo <subquery2> inside the sort-nest
let $query= SELECT * FROM t3,t2
WHERE t3.b=t2.b AND
t3.a IN (SELECT max(t1.a) FROM t1 GROUP BY t1.b)
ORDER BY t3.a DESC,t2.a DESC
LIMIT 5;
set use_sort_nest=1;
eval EXPLAIN $query;
eval EXPLAIN FORMAT=JSON $query;
eval $query;
set use_sort_nest=0;
eval $query;
DROP TABLE t1,t2,t3,t0;
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment