move test folder
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,394 @@
|
||||
# owner: guoping.wgp
|
||||
# owner group: sql1
|
||||
# tags: optimizer
|
||||
# description:
|
||||
#
|
||||
|
||||
--disable_query_log
|
||||
set @@session.explicit_defaults_for_timestamp=off;
|
||||
--enable_query_log
|
||||
|
||||
--disable_warnings
|
||||
drop database if exists hualong;
|
||||
--enable_warnings
|
||||
create database hualong;
|
||||
use hualong;
|
||||
|
||||
create table t1 (pk1 int primary key, a int, b int, c int, d int,
|
||||
key idx_a(a),
|
||||
key idx_b(b),
|
||||
key idx_c(c),
|
||||
key idx_a_b(a, b),
|
||||
key idx_b_c(b, c),
|
||||
key idx_c_a(c, a),
|
||||
key idx_a_b_c(a, b, c),
|
||||
key idx_b_c_a(b, c, a),
|
||||
key idx_c_a_b(c, a, b));
|
||||
|
||||
create table t2 (pk2 int primary key, x int, y int, z int,
|
||||
key idx_x_y_z(x, y, z));
|
||||
|
||||
create table t3 (pk1 int primary key, a int, b int, c int, d int,
|
||||
key idx_a(a),
|
||||
key idx_b(b),
|
||||
key idx_c(c),
|
||||
key idx_a_b(a, b),
|
||||
key idx_b_c(b, c),
|
||||
key idx_c_a(c, a),
|
||||
key idx_a_b_c(a, b, c) storing (d),
|
||||
key idx_b_c_a(b, c, a) storing (d),
|
||||
key idx_c_a_b(c, a, b) storing (d));
|
||||
|
||||
create table t4 (
|
||||
a1 char(64), a2 char(64), b char(16), c char(16) not null, d char(16), dummy char(248) default ' ',
|
||||
index idx_t1_0 (a1),
|
||||
index idx_t1_1 (a1,a2,b,c),
|
||||
index idx_t1_2 (a1,a2,b));
|
||||
create table t5 (pk int primary key, v1 int, v2 int, index idx_v1(v1), index idx_v2(v2));
|
||||
|
||||
## test equal prefix
|
||||
create table t6 (pk int primary key, a int, b int, c int, d int, e int,
|
||||
index idx_b_c(b, c),
|
||||
index idx_b_a_c(b, a, c),
|
||||
index idx_b_e_d_c_a(b, e, d, c, a));
|
||||
|
||||
create table t7(a int, b int, c int, d int, e int, f int, key k1(a,c,b), key k2(a,b,c,d,e));
|
||||
|
||||
create table t8(a int primary key, b int, c int, d int, e int, f int, index k1(b,c,e), index k2(c,d,e,f));
|
||||
|
||||
create table tmp (pk int primary key, c1 int, c2 int, c3 int, c4 int, c5 int,
|
||||
index idx_c1_c2(c1, c2),
|
||||
index idx_c1_c2_c3(c1, c2 ,c3),
|
||||
index idx_c1_c2_c3_c4(c1, c2, c3, c4));
|
||||
|
||||
create table t9 (c1 int primary key, c2 int, c3 int, c4 varchar(100), c5 int,
|
||||
index idx_c2_c5 (c2, c5),
|
||||
index idx_c2_c3_c5 (c2, c3, c5),
|
||||
index idx_c4_c5 (c4, c5),
|
||||
index idx_c4_c3_c5 (c4, c3, c5));
|
||||
|
||||
--real_sleep 5
|
||||
|
||||
##no range, no index back no intersting order
|
||||
explain select b from t1;
|
||||
explain select b,c from t1;
|
||||
explain select * from t1;
|
||||
|
||||
##group by
|
||||
explain select b from t1 group by b;
|
||||
explain select b, c from t1 group by b,c;
|
||||
explain select a, b, c from t1 group by b,c;
|
||||
explain select b, c from t1 group by c,b;
|
||||
explain select a, b, c from t1 group by c,b;
|
||||
explain select * from t1 where b = 100 group by c,b limit 100;
|
||||
|
||||
#distinct
|
||||
explain select distinct(b) from t1;
|
||||
explain select distinct(b),c from t1;
|
||||
explain select distinct(b),c from t1 where a > 100;
|
||||
explain select distinct(c),b from t1;
|
||||
explain select distinct(c),b, d from t1 where b > 200 limit 100;
|
||||
|
||||
#order by
|
||||
explain select b from t1 order by b;
|
||||
explain select b, c from t1 order by b, c;
|
||||
explain select a, b, c from t1 order by b, c, a;
|
||||
explain select a, b, c, d from t1 where b in (100, 200) order by b, c, a limit 100;
|
||||
|
||||
##range
|
||||
explain select b from t1 where b = 100;
|
||||
explain select b, c from t1 where b = 100;
|
||||
explain select b, c, a from t1 where b = 100;
|
||||
explain select * from t1 where b = 100 limit 100;
|
||||
explain select b, c from t1 where b = 100 and c = 200;
|
||||
explain select b, c from t1 where b = 100 or c = 200;
|
||||
explain select b, c from t1 where b + c = 100;
|
||||
|
||||
##in condition
|
||||
explain select b from t1 where b in (1, 2, 3);
|
||||
explain select b, c from t1 where b in (1, 2, 3);
|
||||
explain select b, c, a from t1 where b in (1, 2, 3);
|
||||
explain select * from t1 where b in (1, 2, 3) limit 100;
|
||||
|
||||
##open range
|
||||
explain select b from t1 where b < 100;
|
||||
explain select b, c from t1 where b < 100;
|
||||
explain select b, c, a from t1 where b < 100;
|
||||
explain select * from t1 where b < 100 limit 100;
|
||||
|
||||
##close interval
|
||||
explain select b from t1 where b > 0 and b < 100;
|
||||
explain select b, c from t1 where b > 0 and b < 100;
|
||||
explain select b, c, a from t1 where b > 0 and b < 100;
|
||||
explain select * from t1 where b > 0 and b < 100 limit 100;
|
||||
|
||||
explain select b, c from t1 where b = 100 and c > 0 and c < 100;
|
||||
explain select * from t1 where b = 100 and c > 0 and c < 100 limit 100;
|
||||
|
||||
##
|
||||
explain select a, b, c from t1 where b = 100 and a > 0 and a < 100 and c > 100;
|
||||
explain select a, b, c from t1 where b = 100 and a > 0 and a < 100 and c = 200;
|
||||
explain select a, b, c from t1 where b = 100 or c = 100 or a = 100 group by c, b;
|
||||
explain select a, b, c from t1 where b in (100, 200, 300) or c in (100, 200, 300) or a in (100, 200, 300) group by c, b;
|
||||
explain select a, b, c from t1 where (b > 100 or b < 200) or (c > 100 or c < 200) or (c > 100 or c < 200) group by c, b;
|
||||
explain select a, b, c, d from t1 where b = 100 and a > 0 and a < 100 and c = 200 limit 100;
|
||||
|
||||
## join
|
||||
explain select * from t1 join t2 on t1.pk1 = t2.pk2;
|
||||
explain select a, b, c from t1 join t2 on t1.b = t2.x;
|
||||
explain select a, b, c from t1 join t2 where t1.b = t2.x;
|
||||
explain select a, b, c from t1 join t2 on t1.b = t2.x and t1.c = t2.y;
|
||||
explain select a, b, c from t1 join t2 where t1.b = t2.x and t1.c = t2.y;
|
||||
explain select a, b, c from t1 join t2 on t1.b = t2.x and t1.c = t2.y and t1.a = t2.z;
|
||||
explain select a, b, c from t1 join t2 where t1.b = t2.x and t1.c = t2.y and t1.a = t2.z;
|
||||
explain select t.a, t.b, t.c from t1 as t join t1 as tt join t1 as ttt on t.b = tt.c and t.c = tt.a and ttt.c > 100;
|
||||
explain select a, b, c from t1 left join t2 on t1.b = t2.x order by b, c;
|
||||
explain select a, b, c from t2 left join t1 on t1.b = t2.x order by b, c;
|
||||
|
||||
##set
|
||||
explain select a, b, c from t1 union select x, y, z from t2;
|
||||
explain select a, b, c from t1 order by c, b, a union select x, y, z from t2;
|
||||
explain select b, c, a from t1 where b > 100 union select x, y, z from t2 where x > 100;
|
||||
explain select b, c, a from t1 where b > 100 and b < 200 union select x, y, z from t2 where x > 100 and x < 200;
|
||||
|
||||
###condition with distinct and order by group by
|
||||
explain select a, b, c from t1 where b = 100 group by c;
|
||||
explain select a, b, c from t1 where b > 100 and b < 200 group by c, b;
|
||||
explain select a, b, c from t1 where b > 100 and b < 200 group by c, b having sum(c) > 1000;
|
||||
explain select distinct a, b, c from t1 where b = 100 order by b;
|
||||
explain select distinct a, b, c from t1 where (b > 100 and b < 200) or (a < 200 and a > 100) order by b desc;
|
||||
explain select distinct a, b, c from t1 where b = 100 or c = 200 order by b desc, c desc;
|
||||
explain select /*+index(t1 idx_b_c)*/ distinct a, b, c from t1 where b = 100 or c = 200 order by b desc, c desc;
|
||||
explain select /*+index(t1 idx_b_c_a)*/ distinct a, b, c from t1 where b = 100 or c = 200 order by b desc, c desc;
|
||||
explain select distinct a, b, c from t1 where b in (100, 200) or c in (200, 400) order by b desc, c desc;
|
||||
explain select distinct b, c, a from t1 where b > 100 and b < 200 and c > 100 and a > 100 order by b, c;
|
||||
explain select distinct a, b, c from t1 group by c, b order by c;
|
||||
explain select distinct a, b, c from t1 join t2 where t1.b = t2.x and t1.c = t2.y group by c, b order by b, c;
|
||||
|
||||
## join with group by
|
||||
explain select a, b, c from t1 join t2 on t1.b != t2.x group by c, b;
|
||||
explain select a, b, c from t1 join t2 on t1.b = t2.x and t1.c = t2.y group by c, b;
|
||||
explain select a, b, c from t1 join t2 on t1.b = t2.x order by b, c;
|
||||
|
||||
|
||||
## semi join
|
||||
explain select a,b,c from t3 where d in (select pk2 from t2) group by a,b;
|
||||
explain select a,b,c from t3 where d not in (select pk2 from t2) group by a,b;
|
||||
explain select a,b,c from t3 where d in (select pk2 from t2) order by a,b;
|
||||
explain select a,b,c from t3 where d not in (select pk2 from t2) order by a,b;
|
||||
explain select distinct a,b,c from t3 where d in (select pk2 from t2);
|
||||
explain select distinct a,b,c from t3 where d not in (select pk2 from t2);
|
||||
explain select distinct a,b,c from t3 where d in (select pk2 from t2) union select x,y,z from t2;
|
||||
|
||||
explain select distinct a,b,c from t3 where d > any (select pk2 from t2);
|
||||
|
||||
explain select a, b, c from t1 where b = 100 and c < 100 and c > 100 order by c desc limit 0, 100;
|
||||
explain select a, b, c from t1 where b = 100 or (b = 200 and c = 300) order by c desc limit 0, 100;
|
||||
explain select a, b, c from t1 where (b = 200 and c = 300) or (b = 100) order by c desc limit 0, 100;
|
||||
|
||||
explain select a1, a2, b, min(c), max(c) from t4 group by a1, a2, b;
|
||||
explain select v1, pk from t5 where pk = 100 and v1 = 100 order by v1 desc;
|
||||
explain select v1, pk from t5 where pk = 100 and v1 = 200 order by v1 desc;
|
||||
|
||||
|
||||
### test of equal prefix (order by)
|
||||
explain select a from t6 where b = 1 order by c desc limit 9;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 order by c desc limit 9;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 order by c desc limit 9;
|
||||
|
||||
explain select b, c from t6 where a = 1 order by b, c;
|
||||
explain select /*+index(t6 idx_b_c)*/ b, c from t6 where a = 1 order by b, c;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ b, c from t6 where a = 1 order by b, c;
|
||||
|
||||
explain select a from t6 where b = 1 and a = 2 order by c;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 and a = 2 order by c;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 and a = 2 order by c;
|
||||
|
||||
explain select a from t6 where b = 1 or b = 2 order by a;
|
||||
|
||||
explain select a from t6 where b = 1 and c = 2 order by a;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 and c = 2 order by a;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 and c = 2 order by a;
|
||||
|
||||
explain select a from t6 where b = 1 order by a, c desc;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 order by a, c desc;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 order by a, c desc;
|
||||
|
||||
explain select a from t6 where b > 1 order by c desc;
|
||||
|
||||
explain select a from t6 where b = 1 and d = 2 order by e, c, a;
|
||||
|
||||
explain select a from t6 where e = 1 and c = 2 order by b, d, a;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where e = 1 and c = 2 order by b, d, a;
|
||||
explain select /*+index(t6 idx_b_e_d_c_a)*/ a from t6 where e = 1 and c = 2 order by b, d, a;
|
||||
|
||||
explain select a from t6 where b = 1 and a > 2 order by c;
|
||||
|
||||
### test of equal preifix (group by)
|
||||
explain select a from t6 where b = 1 group by c limit 9;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 group by c limit 9;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 group by c limit 9;
|
||||
|
||||
explain select b, c from t6 where a = 1 group by b, c;
|
||||
explain select /*+index(t6 idx_b_c)*/ b, c from t6 where a = 1 group by b, c;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ b, c from t6 where a = 1 group by b, c;
|
||||
|
||||
explain select a from t6 where b = 1 and a = 2 group by c;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 and a = 2 group by c;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 and a = 2 group by c;
|
||||
|
||||
explain select a from t6 where b = 1 or b = 2 group by a;
|
||||
|
||||
explain select a from t6 where b = 1 and c = 2 group by a;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 and c = 2 group by a;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 and c = 2 group by a;
|
||||
|
||||
explain select a from t6 where b = 1 group by a, c;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 group by a, c;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 group by a, c;
|
||||
|
||||
explain select a from t6 where b > 1 group by c desc;
|
||||
|
||||
explain select a from t6 where b = 1 and d = 2 group by e, c, a;
|
||||
|
||||
explain select a from t6 where e = 1 and c = 2 group by b, d, a;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where e = 1 and c = 2 group by b, d, a;
|
||||
explain select /*+index(t6 idx_b_e_d_c_a)*/ a from t6 where e = 1 and c = 2 group by b, d, a;
|
||||
|
||||
explain select a from t6 where b = 1 and a > 2 group by c;
|
||||
|
||||
|
||||
### test of equal prefix (distinct )
|
||||
explain select distinct c from t6 where b = 1 limit 9;
|
||||
explain select /*+index(t6 idx_b_c)*/ distinct c from t6 where b = 1 limit 9;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ distinct c from t6 where b = 1 limit 9;
|
||||
|
||||
explain select distinct b, c from t6 where a = 1;
|
||||
explain select /*+index(t6 idx_b_c)*/ distinct b, c from t6 where a = 1;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ distinct b, c from t6 where a = 1;
|
||||
|
||||
explain select distinct c from t6 where b = 1 and a = 2;
|
||||
explain select /*+index(t6 idx_b_c)*/ distinct c from t6 where b = 1 and a = 2;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ distinct c from t6 where b = 1 and a = 2;
|
||||
|
||||
explain select distinct a from t6 where b = 1 or b = 2;
|
||||
|
||||
explain select distinct a from t6 where b = 1 and c = 2;
|
||||
explain select /*+index(t6 idx_b_c)*/ distinct a from t6 where b = 1 and c = 2;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ distinct a from t6 where b = 1 and c = 2;
|
||||
|
||||
explain select distinct a, c from t6 where b = 1;
|
||||
explain select /*+index(t6 idx_b_c)*/ distinct a, c from t6 where b = 1;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ distinct a, c from t6 where b = 1;
|
||||
|
||||
explain select distinct c from t6 where b > 1;
|
||||
|
||||
explain select distinct e, c, a from t6 where b = 1 and d = 2;
|
||||
|
||||
explain select distinct b, d, a from t6 where e = 1 and c = 2;
|
||||
explain select /*+index(t6 idx_b_c)*/ distinct b, d, a from t6 where e = 1 and c = 2;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ distinct b, d, a from t6 where e = 1 and c = 2;
|
||||
explain select /*+index(t6 idx_b_e_d_c_a)*/ distinct b, d, a from t6 where e = 1 and c = 2;
|
||||
|
||||
explain select distinct c from t6 where b = 1 and a > 2;
|
||||
|
||||
## union
|
||||
explain select c from t6 where b = 1 limit 9 union select pk from tmp;
|
||||
explain select /*+index(t6 idx_b_c)*/ c from t6 where b = 1 limit 9 union select pk from tmp;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ c from t6 where b = 1 limit 9 union select pk from tmp;
|
||||
|
||||
explain select b, c from t6 where a = 1 union select c1, c2 from tmp;
|
||||
explain select /*+index(t6 idx_b_c)*/ b, c from t6 where a = 1 union select c1, c2 from tmp;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ b, c from t6 where a = 1 union select c1, c2 from tmp;
|
||||
|
||||
explain select c from t6 where b = 1 and a = 2 union select pk from tmp;
|
||||
explain select /*+index(t6 idx_b_c)*/ c from t6 where b = 1 and a = 2 union select pk from tmp;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ c from t6 where b = 1 and a = 2 union select pk from tmp;
|
||||
|
||||
explain select a from t6 where b = 1 or b = 2 union select pk from tmp;
|
||||
|
||||
explain select a from t6 where b = 1 and c = 2 union select pk from tmp;
|
||||
explain select /*+index(t6 idx_b_c)*/ a from t6 where b = 1 and c = 2 union select pk from tmp;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a from t6 where b = 1 and c = 2 union select pk from tmp;
|
||||
|
||||
explain select a, c from t6 where b = 1 union select c1, c2 from tmp;
|
||||
explain select /*+index(t6 idx_b_c)*/ a, c from t6 where b = 1 union select c1, c2 from tmp;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ a, c from t6 where b = 1 union select c1, c2 from tmp;
|
||||
|
||||
explain select c from t6 where b > 1 union select pk from tmp;
|
||||
|
||||
explain select e, c, a from t6 where b = 1 and d = 2 union select c1, c2, c3 from tmp;
|
||||
|
||||
explain select b, d, a from t6 where e = 1 and c = 2 union select c1, c2, c3 from tmp;
|
||||
explain select /*+index(t6 idx_b_c)*/ b, d, a from t6 where e = 1 and c = 2 union select c1, c2, c3 from tmp;
|
||||
explain select /*+index(t6 idx_b_a_c)*/ b, d, a from t6 where e = 1 and c = 2 union select c1, c2, c3 from tmp;
|
||||
explain select /*+index(t6 idx_b_e_d_c_a)*/ b, d, a from t6 where e = 1 and c = 2 union select c1, c2, c3 from tmp;
|
||||
|
||||
explain select c from t6 where b = 1 and a > 2 union select pk from tmp;
|
||||
|
||||
explain select * from t7 where a >= 0 and a <= 5 and b = 1;
|
||||
|
||||
explain select * from t8 where e = 1 order by b;
|
||||
explain select * from t8 where e = 1 or f = 1 order by b;
|
||||
explain select * from t8 where d = 1 order by b;
|
||||
|
||||
|
||||
|
||||
## part expr 决定路径
|
||||
explain select avg(c1) over (partition by c2) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c2, c3) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c3) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c3) from t9 where c2 = 2 order by c1;
|
||||
|
||||
## order expr 决定路径
|
||||
explain select avg(c1) over (order by c2, c3) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (order by c2, c3 desc) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (order by c2 desc, c3 desc) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (order by c3) from t9 where c2 = 2 order by c1;
|
||||
|
||||
## part + order expr 决定路径
|
||||
explain select avg(c1) over (partition by c2 order by c3) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c2 order by c3, c5) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c2 order by c3, c5 desc) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c3 order by c5) from t9 where c2 = 2 order by c1;
|
||||
|
||||
## win func 没有合适的路径
|
||||
explain select avg(c1) over (partition by c3, c5) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c3, c5) from t9 order by c4;
|
||||
|
||||
explain select avg(c1) over (order by c3, c5) from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (order by c3, c5) from t9 order by c4;
|
||||
|
||||
## 多个 window function
|
||||
# 创建一条新的可选路径
|
||||
create index idx_c3_c2_c5 on t9(c3, c2, c5);
|
||||
--source mysql_test/include/check_all_idx_ok.inc
|
||||
|
||||
explain select avg(c1) over (partition by c2 order by c3),
|
||||
max(c1) over (partition by c2, c3, c5)
|
||||
from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c2 order by c3),
|
||||
max(c1) over (partition by c3 order by c2, c5)
|
||||
from t9 order by c1;
|
||||
|
||||
explain select avg(c1) over (partition by c3 order by c5),
|
||||
max(c1) over (partition by c2, c3, c5)
|
||||
from t9 order by c3, c2, c5;
|
||||
|
||||
##complicate
|
||||
#
|
||||
set @@recyclebin = 'off';
|
||||
drop database hualong;
|
||||
|
||||
@ -0,0 +1,230 @@
|
||||
# owner: guoping.wgp
|
||||
# owner group: sql1
|
||||
# tags: optimizer
|
||||
# description:
|
||||
#
|
||||
|
||||
--disable_query_log
|
||||
set @@session.explicit_defaults_for_timestamp=off;
|
||||
--enable_query_log
|
||||
|
||||
--disable_warnings
|
||||
drop database if exists hualong;
|
||||
--enable_warnings
|
||||
|
||||
create database hualong;
|
||||
use hualong;
|
||||
|
||||
create table other(c1 int primary key, c2 int, c3 int,
|
||||
key idx_c2(c2),
|
||||
key idx_c3(c3),
|
||||
key idx_c2_c3(c2,c3),
|
||||
key idx_c3_c2(c3,c2));
|
||||
|
||||
## 这个表的特点在于(c2~c6)的所有列上都建有索引,只要跟c2~c6的列上的序能够利用,那么就能找到一个最佳的索引。
|
||||
|
||||
#CREATE TABLE `skyline` (
|
||||
# `tenant_id` varchar(8) NOT NULL ,
|
||||
# `id` varchar(32) NOT NULL ,
|
||||
# `v1` varchar(64) NOT NULL ,
|
||||
# `v2` varchar(24) NOT NULL,
|
||||
# `v3` varchar(64) NOT NULL,
|
||||
# `v4` varchar(32) NOT NULL,
|
||||
# `v5` varchar(32) NOT NULL,
|
||||
# `v6` varchar(32) NOT NULL,
|
||||
# `gmt_create` timestamp NOT NULL,
|
||||
# `gmt_modified` timestamp NOT NULL,
|
||||
# PRIMARY KEY (v1, tenant_id),
|
||||
# KEY `idx_tenant_id` (tenant_id),
|
||||
# KEY `idx_v2` (v2),
|
||||
# KEY `idx_v2_v3` (v2, v3),
|
||||
# KEY `idx_v3_v2` (v3, v2),
|
||||
# KEY `idx_v2_v3_v4` (v2, v3, v4),
|
||||
# KEY `idx_v3_v4_v2` (v3, v4, v2),
|
||||
# KEY `idx_v4_v2_v3` (v4, v2, v3),
|
||||
# KEY `idx_v2_v3_v4_v5` (v2, v3, v4, v5),
|
||||
# KEY `idx_v3_v4_v5_v2` (v3, v4, v5, v2),
|
||||
# KEY `idx_v4_v5_v2_v3` (v4, v5, v2, v3),
|
||||
# KEY `idx_v5_v2_v3_v4` (v5, v2, v3, v4),
|
||||
# KEY `idx_v2_v3_v4_v5_v6` (v2, v3, v4, v5, v6),
|
||||
# KEY `idx_v3_v4_v5_v6_v2` (v3, v4, v5, v6, v2),
|
||||
# KEY `idx_v4_v5_v6_v2_v3` (v4, v5, v6, v2, v3),
|
||||
# KEY `idx_v5_v6_v2_v3_v4` (v5, v6, v2, v3, v4),
|
||||
# KEY `idx_v6_v2_v3_v4_v5` (v6, v2, v3, v4, v5));
|
||||
#
|
||||
CREATE TABLE `skyline_int` (
|
||||
`tenant_id` varchar(8) NOT NULL ,
|
||||
`id` int NOT NULL,
|
||||
`v1` int NOT NULL,
|
||||
`v2` int NOT NULL,
|
||||
`v3` int NOT NULL,
|
||||
`v4` int NOT NULL,
|
||||
`v5` int NOT NULL,
|
||||
`v6` int NOT NULL,
|
||||
`gmt_create` timestamp NOT NULL,
|
||||
`gmt_modified` timestamp NOT NULL,
|
||||
PRIMARY KEY (v1, tenant_id),
|
||||
KEY `idx_tenant_id` (tenant_id),
|
||||
KEY `idx_v2` (v2),
|
||||
KEY `idx_v2_v3` (v2, v3),
|
||||
KEY `idx_v3_v2` (v3, v2),
|
||||
KEY `idx_v2_v3_v4` (v2, v3, v4),
|
||||
KEY `idx_v3_v4_v2` (v3, v4, v2),
|
||||
KEY `idx_v4_v2_v3` (v4, v2, v3),
|
||||
KEY `idx_v2_v3_v4_v5` (v2, v3, v4, v5),
|
||||
KEY `idx_v3_v4_v5_v2` (v3, v4, v5, v2),
|
||||
KEY `idx_v4_v5_v2_v3` (v4, v5, v2, v3),
|
||||
KEY `idx_v5_v2_v3_v4` (v5, v2, v3, v4),
|
||||
KEY `idx_v2_v3_v4_v5_v6` (v2, v3, v4, v5, v6),
|
||||
KEY `idx_v3_v4_v5_v6_v2` (v3, v4, v5, v6, v2),
|
||||
KEY `idx_v4_v5_v6_v2_v3` (v4, v5, v6, v2, v3),
|
||||
KEY `idx_v5_v6_v2_v3_v4` (v5, v6, v2, v3, v4),
|
||||
KEY `idx_v6_v2_v3_v4_v5` (v6, v2, v3, v4, v5));
|
||||
|
||||
--real_sleep 5
|
||||
|
||||
##这里的虽然group by之后的序不一定能给order by用,但是这里的group by interesting order会剪掉大部分的index
|
||||
##最好的索引是 idx_v3_v2
|
||||
explain select v1, tenant_id from skyline_int group by v3 order by v3;
|
||||
|
||||
## 能选的索引只有 idx_v3_v4_v2, idx_v3_v4_v5_v2 idx_v3_v4_v5_v6_v2, 代价最小应该是最短的
|
||||
explain select v1, tenant_id from skyline_int group by v3, v4 order by v3;
|
||||
explain select v1, tenant_id from skyline_int group by v4, v3 order by v3;
|
||||
|
||||
##不管group 顺序如何,都是选中同一个索引idx_v3_v4_v5_v2 或 idx_v3_v4_v5_v6_v2
|
||||
##如果需要v6列,那么会选第二个索引
|
||||
explain select v1, tenant_id from skyline_int group by v3, v4, v5 order by v3;
|
||||
explain select v1, tenant_id from skyline_int group by v3, v5, v4 order by v3;
|
||||
##no index back
|
||||
explain select v1, tenant_id, v6 from skyline_int group by v3, v5, v4 order by v3;
|
||||
explain select v1, tenant_id from skyline_int group by v4, v3, v5 order by v3;
|
||||
explain select v1, tenant_id from skyline_int group by v4, v5, v3 order by v3;
|
||||
explain select v1, tenant_id from skyline_int group by v5, v3, v4 order by v3;
|
||||
explain select v1, tenant_id from skyline_int group by v5, v4, v3 order by v3;
|
||||
|
||||
##有很多索引可以选,但是像idx_v3_v2 idx_v3_v4_v2 idx_v3_v4_v5_v2 idx_v3_v4_v5_v6_v2, 会剪掉大部分,只剩下最后一个
|
||||
explain select sum(v1), tenant_id from skyline_int group by v6, v5, v4, v3 having sum(v1) > 100 order by v3;
|
||||
|
||||
## 有group的情况下,最大的序会group和join的最大值,和order by无关
|
||||
--echo group by will decide which index to use
|
||||
## index idx_v2_v3_v4
|
||||
explain select max(v1), tenant_id from skyline_int join other on v4 = c1 and v3 = c2 and v2 = c3 group by v3, v2;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c1 and v2 = c3 group by v3, v4, v2;
|
||||
## index idx_v3_v4_v2
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c3 and v2 = c1 group by v3;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c3 and v2 = c1 group by v3, v4;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c3 and v2 = c1 group by v4, v3;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v4 = c3 and v3 = c2 and v2 = c1 group by v4, v3;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v4 = c3 and v3 = c2 and v2 = c1 group by v4, v3 order by c2, c3;
|
||||
## index idx_v4_v2_v3
|
||||
explain select max(v1), tenant_id from skyline_int join other on v4 = c3 and v3 = c1 and v2 = c2 group by v2, v4;
|
||||
|
||||
## index idx_v3_v4_v5_v2
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c3 and v5 = c1 group by v3;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v4 = c3 and v3 = c2 and v5 = c1 group by v3;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c3 and v5 = c1 group by v3, v4;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v5 = c1 and v4 = c3 and v3 = c2 group by v4, v3;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c2 and v4 = c3 and v5 = c1 group by v3, v4, v5;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v5 = c1 and v4 = c3 and v3 = c2 group by v5, v4, v3;
|
||||
|
||||
## idx_v3_v2
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c1 group by v3;
|
||||
## idx_v3_v4_v2
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c1 group by v4, v3;
|
||||
## idx_v3_v4_v5
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c1 group by v5, v4;
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c1 group by v5, v4 order by v4, v5;
|
||||
## idx_v3_v4_v5_v2
|
||||
explain select max(v1), tenant_id from skyline_int join other on v3 = c1 group by v5, v4, v3 order by v3, v4, v5;
|
||||
## idx_v3_v4_v5_v6_v2
|
||||
explain select max(v1), tenant_id, v6 from skyline_int join other on v3 = c1 group by v5, v4, v3 order by v3, v4, v5;
|
||||
|
||||
## idx_v3_v4_v5_v2
|
||||
explain select distinct(v3) from skyline_int join other on v3 = c1 order by v3, v4, v5;
|
||||
## idx_v4_v5_v2_v3
|
||||
explain select distinct(v4) from skyline_int join other on v4 = c1 order by v3, v4, v5;
|
||||
## idx_v4_v5_v6_v2_v3
|
||||
explain select distinct(v4) from skyline_int join other on v4 = c1 order by v3, v4, v5, v6;
|
||||
## idx_v4_v5_v2_v3
|
||||
explain select distinct v5, v4, v3 from skyline_int join other on v4 = c1 order by v3;
|
||||
explain select distinct v5, v4, v3 from other left join skyline_int on v4 = c1 order by v3;
|
||||
## idx_v4_v5_v6_v2_v3
|
||||
explain select distinct v4, v5, v6 from other left join skyline_int on v4 = c1 and v5 = c2 order by v4, v5, v6;
|
||||
|
||||
# test union
|
||||
explain select v3, v4, v5 from skyline_int union select c1, c2, c3 from other;
|
||||
explain select v4, v5, v6 from skyline_int intersect select v3, v4, v5 from skyline_int;
|
||||
explain select v4, v5, v6 from skyline_int union select v2, v3, v4 from skyline_int;
|
||||
|
||||
## condition
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 = 100 and v5 = 100 order by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 in (100, 200, 300) group by v3, v4, v5 order by v3, v4, v5;
|
||||
explain select distinct v3, v4, v5 from skyline_int where v3 in (100, 200, 300) order by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 > 100 group by v4, v3, v5;
|
||||
|
||||
explain select v4, v5, v6 from skyline_int where v3 = 100 group by v3, v5;
|
||||
explain select distinct v3, v5 from skyline_int where v4 = 100;
|
||||
explain select distinct v3, v5 from skyline_int where v4 > 100 and v4 < 200;
|
||||
explain select v3, v5 from skyline_int where v4 > 100 and v4 < 200 order by v4, v2;
|
||||
explain select v3, v5 from skyline_int where v4 > 100 and v4 < 200 order by v4, v2 limit 100;
|
||||
explain select v3, v5 from skyline_int where v5 = 100 group by v4, v2;
|
||||
explain select v3, v5 from skyline_int where v5 = 100 group by v4, v2 limit 100;
|
||||
explain select v3, v6 from skyline_int join other on skyline_int.v3 = other.c1 and skyline_int.v5 = other.c2 order by v3, v4;
|
||||
explain select v3, v6 from skyline_int join other on skyline_int.v3 = other.c1 and skyline_int.v5 = other.c2 order by v3;
|
||||
|
||||
## 还是要SORT
|
||||
explain select distinct v3, v5 from skyline_int join other where skyline_int.v4 = other.c1 order by v4, v5, v6;
|
||||
|
||||
#join的序没有被后面的运算符所用
|
||||
## should do merge group by
|
||||
explain select v3, v5 from skyline_int join other where skyline_int.v4 = other.c1 group by v4, v5;
|
||||
## 并没有使用hash distinct保序
|
||||
explain select distinct v4, v5 from skyline_int join other where skyline_int.v4 = other.c1;
|
||||
## 并没有用merge join 保序
|
||||
explain select v4, v5 from skyline_int join other where skyline_int.v4 = other.c1 order by v4, v5;
|
||||
## 没有用merge join 保序
|
||||
explain (select v4, v5, v6 from skyline_int join other on skyline_int.v4 = other.c1 ) union select c1, c2, c3 from other;
|
||||
|
||||
#group by的序能被 order by和distinct 所用
|
||||
##group by的序能被order by所用
|
||||
explain select v1, tenant_id from skyline_int group by v2, v3, v4 order by v2, v3, v4, v5;
|
||||
#group by的序其实能被 distinct所用
|
||||
explain select distinct v2, v3, v4, v5 from skyline_int group by v2, v3, v4;
|
||||
## group by的序能被union所用
|
||||
explain (select c1, c2, c3 from other group by c1, c2) union select c1, c2, c3 from other group by c1, c2;
|
||||
|
||||
##distinct的序能被order by所用
|
||||
explain select distinct v3, v4, v5 from skyline_int order by v3, v4, v5, v6;
|
||||
##distinct的序能被union所用
|
||||
explain select distinct v3, v4 from skyline_int union select distinct c2, c3 from other;
|
||||
|
||||
## order by会消除或者能被union所用
|
||||
explain select v3, v4 from skyline_int order by v3, v4 union select distinct c2, c3 from other order by v3, v4;
|
||||
explain (select v3, v4 from skyline_int order by v3, v4) union (select distinct c2, c3 from other order by c2, c3) order by 1,2;
|
||||
|
||||
## 强制走 nestloop,保左表的序,group by的序能被用
|
||||
explain select /*+use_nl(skyline_int other)*/tenant_id from skyline_int join other where skyline_int.v3 = other.c2 group by v3, v4;
|
||||
explain select /*+use_nl(skyline_int other)*/tenant_id from skyline_int join other where skyline_int.v3 = other.c2 group by v3, v4, v5;
|
||||
|
||||
explain select /*use_nl(skyline_int other)*/tenant_id from skyline_int join other where skyline_int.v3 = other.c2 and skyline_int.v4 = other.c3 group by v3, v4, v5;
|
||||
|
||||
## 强制走 hash join 右表的序能被利用
|
||||
explain select /*+use_hash(skyline_int other)*/ v3, v4 from skyline_int join other on skyline_int.v3 = other.c2 order by v3, v4, v5;
|
||||
explain select /*+use_hash(skyline_int other)*/ distinct v3, v4 from skyline_int join other on skyline_int.v3 = other.c2 order by v3, v4, v5;
|
||||
explain select /*+leading(other skyline_int) use_hash(other skyline_int)*/ distinct v3, v4 from skyline_int join other on skyline_int.v3 = other.c2 union select c2, c3 from other;
|
||||
|
||||
## test query range
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 > 100 group by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 >= 100 group by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 < 100 group by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 <= 100 group by v3, v4, v5;
|
||||
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 > 100 and v4 < 1000 group by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 >= 100 and v4 < 1000 group by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 > 100 and v4 <= 1000 group by v3, v4, v5;
|
||||
explain select v3, v4, v5 from skyline_int where v3 = 100 and v4 >= 100 and v4 <= 1000 group by v3, v4, v5;
|
||||
|
||||
set @@recyclebin = 'off';
|
||||
drop database hualong;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user