summaryrefslogtreecommitdiff
path: root/mysql-test/main/order_by_innodb.test
blob: f4c738263ae599c1cab2f82440db1bab4a8be6b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#
# ORDER BY handling (e.g. filesort) tests that require innodb
#
-- source include/have_innodb.inc

--disable_warnings
drop table if exists t0,t1,t2,t3;
--enable_warnings

--echo #
--echo # MDEV-6434: Wrong result (extra rows) with ORDER BY, multiple-column index, InnoDB
--echo #

CREATE TABLE t1 (a INT, b INT, c INT, d TEXT, KEY idx(a,b,c)) ENGINE=InnoDB;

INSERT INTO t1 (a,c) VALUES
(8, 9),(8, 10),(13, 15),(16, 17),(16, 18),(16, 19),(20, 21),
(20, 22),(20, 24),(20, 25),(20, 26),(20, 27),(20, 28);

SELECT  * FROM t1 WHERE a = 8 AND (b = 1 OR b IS NULL) ORDER BY c;

DROP TABLE t1;

--echo #
--echo # MDEV-9457: Poor query plan chosen for ORDER BY query by a recent 10.1
--echo #
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);

create table t1 (
  pk int primary key,
  key1 int,
  key2 int,
  col1 char(255),
  key(key1),
  key(key2)
) engine=innodb;

set @a=-1;
insert into t1 
select 
  @a:=@a+1, 
  @a, 
  @a, 
  repeat('abcd', 63) 
from t0 A, t0 B, t0 C, t0 D;

--echo # The following must NOT use 'index' on PK. 
--echo # It should use index_merge(key1,key2) + filesort
--replace_column 9 #
explain 
select * 
from t1
where key1<3 or key2<3
order by pk;

--replace_column 9 #
explain 
select * 
from t1
where key1<3 or key2<3;

drop table t0, t1;

--echo #
--echo # MDEV-18094: Query with order by limit picking index scan over filesort
--echo #

create table t0 (a int);
INSERT INTO t0 VALUES (0),(0),(0),(0),(2),(0),(0),(1),(1),(0);

CREATE TABLE t1 (
a int(11),
b int(11),
c int(11),
KEY a_c (a,c),
KEY a_b (a,b)
) ENGINE=InnoDB;
insert into t1 select A.a , B.a, C.a from t0 A, t0 B, t0 C;

--echo # should use ref access
explain select a,b,c from t1 where a=1 and c=2 order by b;

--echo # both should use range access
explain select a,b,c from t1 where a=1 and c=2 order by b limit 1000;
explain select a,b,c from t1 where a=1 and c=2 order by b limit 2000;
drop table t1,t0;

--echo # Start of 10.2 tests

--echo #
--echo # MDEV-14071: wrong results with orderby_uses_equalities=on
--echo # (duplicate of MDEV-13994)
--echo #

CREATE TABLE t1 (i int, j int, z int,PRIMARY KEY (i,j), KEY (z)) ENGINE=InnoDB;
CREATE TABLE t2 (i int, j int, PRIMARY KEY (i,j)) ENGINE=InnoDB;
CREATE TABLE t3 (j int, n varchar(5), PRIMARY KEY (j)) ENGINE=InnoDB;

INSERT INTO t1 VALUES
  (127,0,1),(188,0,1),(206,0,1),(218,0,1),(292,0,1),(338,0,1),(375,0,1),
  (381,0,1),(409,0,1),(466,0,1),(469,0,1),(498,0,1),(656,0,1);
INSERT INTO t1 VALUES
  (77,4,0),(86,7,0),(96,6,0),(96,7,0),(99,9,0),(99,10,0),(99,11,0),(104,4,0),
  (106,5,0),(148,6,0),(177,6,0),(181,5,0),(188,8,0),(218,8,0),(253,7,0),
  (268,4,0),(338,4,0),(409,7,0),(466,8,0),(469,8,0),(498,8,0),(656,8,0);

INSERT INTO t2 VALUES
  (127,7),(188,8),(188,9),(206,6),(218,8),(218,9),(292,7),(338,4),(338,5),
  (375,6),(381,5),(409,7),(409,8),(466,8),(466,9),(469,8),(469,9),(498,8),
  (498,9),(656,8),(656,9);
INSERT INTO t3 VALUES
  (4,'four'),(5,'five'),(6,'six'),(7,'seven'),(8,'eight'),(9,'nine');

let $q1=
SELECT i,n
FROM t1 INNER JOIN t2 USING (i,j) LEFT JOIN t3 USING (j)
WHERE i IN (SELECT i FROM t1 WHERE z=1) AND z=0 ORDER BY i;
let $q2=
SELECT i,n
FROM t1 x INNER JOIN t2 USING (i,j) LEFT JOIN t3 USING (j)
WHERE EXISTS (SELECT * FROM t1 WHERE i=x.i AND z=1) AND z=0 ORDER BY i;

SET @save_optimizer_switch=@@optimizer_switch;

SET optimizer_switch='orderby_uses_equalities=off';
eval $q1;
eval $q2;

SET optimizer_switch='orderby_uses_equalities=on';
eval $q1;
eval $q2;

set optimizer_switch= @save_optimizer_switch;

DROP TABLE t1,t2,t3;

--echo # End of 10.2 tests