summaryrefslogtreecommitdiff
path: root/mysql-test/main/order_by_innodb.result
blob: ad4acad3319dc4501ce6b1dec4851b6840f2e674 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#
# MDEV-6434: Wrong result (extra rows) with ORDER BY, multiple-column index, InnoDB
#
CREATE TABLE t1 (a INT, b INT, c INT, d TEXT, KEY idx(a,b,c)) ENGINE=InnoDB
STATS_PERSISTENT=0;
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;
a	b	c	d
8	NULL	9	NULL
8	NULL	10	NULL
DROP TABLE t1;
#
# MDEV-9457: Poor query plan chosen for ORDER BY query by a recent 10.1
#
create table t1 (
pk int primary key,
key1 int,
key2 int,
col1 char(255),
key(key1),
key(key2)
) engine=innodb stats_persistent=0;
insert into t1 
select seq,seq,seq,repeat('abcd', 63) from seq_0_to_9999;
# The following must NOT use 'index' on PK. 
# It should use index_merge(key1,key2) + filesort
explain 
select * 
from t1
where key1<3 or key2<3
order by pk;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	index_merge	key1,key2	key1,key2	5,5	NULL	#	Using sort_union(key1,key2); Using where; Using filesort
explain 
select * 
from t1
where key1<3 or key2<3;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	index_merge	key1,key2	key1,key2	5,5	NULL	#	Using sort_union(key1,key2); Using where
drop table t1;
#
# MDEV-18094: Query with order by limit picking index scan over filesort
#
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;
select count(*) from t1;
count(*)
1000
select count(*) from t1 where a=1;
count(*)
200
select count(*) from t1 where a=1 and c=2;
count(*)
20
# should use ref access
explain select a,b,c from t1 where a=1 and c=2 order by b;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a_c,a_b	a_c	10	const,const	20	Using where; Using filesort
# all should use range access
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	ref	a_c,a_b	a_c	10	const,const	20	Using where; Using filesort
explain select a,b,c from t1 where a=1 and c=2 order by b limit 300;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a_c,a_b	a_c	10	const,const	20	Using where; Using filesort
explain select a,b,c from t1 where a=1 and c=2 order by b limit 1000;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ref	a_c,a_b	a_c	10	const,const	20	Using where; Using filesort
drop table t1,t0;
# Start of 10.2 tests
#
# MDEV-14071: wrong results with orderby_uses_equalities=on
# (duplicate of MDEV-13994)
#
CREATE TABLE t1 (i int, j int, z int,PRIMARY KEY (i,j), KEY (z)) ENGINE=InnoDB
STATS_PERSISTENT=0;
CREATE TABLE t2 (i int, j int, PRIMARY KEY (i,j)) ENGINE=InnoDB
STATS_PERSISTENT=0;
CREATE TABLE t3 (j int, n varchar(5), PRIMARY KEY (j)) ENGINE=InnoDB
STATS_PERSISTENT=0;
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');
SET @save_optimizer_switch=@@optimizer_switch;
SET optimizer_switch='orderby_uses_equalities=off';
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;
i	n
188	eight
218	eight
338	four
409	seven
466	eight
469	eight
498	eight
656	eight
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;
i	n
188	eight
218	eight
338	four
409	seven
466	eight
469	eight
498	eight
656	eight
SET optimizer_switch='orderby_uses_equalities=on';
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;
i	n
188	eight
218	eight
338	four
409	seven
466	eight
469	eight
498	eight
656	eight
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;
i	n
188	eight
218	eight
338	four
409	seven
466	eight
469	eight
498	eight
656	eight
set optimizer_switch= @save_optimizer_switch;
DROP TABLE t1,t2,t3;
#
# MDEV-25858: Query results are incorrect when indexes are added
#
CREATE TABLE t1 (id int NOT NULL PRIMARY KEY) engine=innodb
STATS_PERSISTENT=0;
insert into t1 values (1),(2),(3);
CREATE TABLE t2 (
id int NOT NULL PRIMARY KEY,
id2 int NOT NULL,
d1 datetime,
d2 timestamp NOT NULL,
KEY id2 (id2)
) engine=innodb stats_persistent=0;
insert into t2 values
(1,2,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
(2,3,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
(3,3,'2019-03-06 00:00:00','2019-03-05 00:00:00');
select
t1.id,t2.id
from
t1 left join
t2 on t2.id2 = t1.id and
t2.id = (select dd.id
from t2 dd
where
dd.id2 = t1.id and
d1 > '2019-02-06 00:00:00'
                 order by
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
);
id	id
1	NULL
2	1
3	3
create index for_latest_sort on t2 (d1, d2, id);
select
t1.id,t2.id
from
t1 left join
t2 on t2.id2 = t1.id and
t2.id = (select dd.id
from t2 dd
where
dd.id2 = t1.id and
d1 > '2019-02-06 00:00:00'
                 order by
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
);
id	id
1	NULL
2	1
3	3
# Now, same as above but use a DESC index
CREATE TABLE t3 (
id int NOT NULL PRIMARY KEY,
id2 int NOT NULL,
d1 datetime,
d2 timestamp NOT NULL,
KEY id2 (id2)
) engine=innodb;
insert into t3 values
(1,2,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
(2,3,'2019-03-05 00:00:00','2019-03-06 00:00:00'),
(3,3,'2019-03-06 00:00:00','2019-03-05 00:00:00');
create index for_latest_sort on t3 (d1 desc, d2 desc, id desc);
select
t1.id,t3.id
from
t1 left join
t3 on t3.id2 = t1.id and
t3.id = (select dd.id
from t3 dd
where
dd.id2 = t1.id and
d1 > '2019-02-06 00:00:00'
                 order by
dd.d1 desc, dd.d2 desc, dd.id desc limit 1
);
id	id
1	NULL
2	1
3	3
#
# MDEV-27270: Wrong query plan with Range Checked for Each Record and ORDER BY ... LIMIT
#
# This must NOT have "Range checked for each record" without any
#   provisions to produce rows in the required ordering:
explain
select
t1.id,t2.id
from
t1 left join
t2 on t2.id2 = t1.id and
t2.id = (select dd.id
from t2 dd
where
dd.id2 = t1.id and
d1 > '2019-02-06 00:00:00'
                 order by
dd.d1, dd.d2, dd.id limit 1
);
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	PRIMARY	t1	ALL	NULL	NULL	NULL	NULL	#	
1	PRIMARY	t2	eq_ref	PRIMARY,id2	id2	8	test.t1.id,func	#	Using where; Using index
2	DEPENDENT SUBQUERY	dd	range	id2,for_latest_sort	for_latest_sort	6	NULL	#	Using where
drop table t1,t2,t3;
# End of 10.2 tests
#
# MDEV-26938 Support descending indexes internally in InnoDB
#
create table t1 (a int, b int, c int, key r (a desc, b asc));
insert t1 select seq % 10, seq div 10, seq from seq_1_to_55;
insert t1 values (NULL, NULL, NULL), (9, NULL, NULL);
explain select * from t1 force index(r) order by a,b limit 20;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	ALL	NULL	NULL	NULL	NULL	57	Using filesort
explain select * from t1 force index(r) order by a desc,b limit 20;
id	select_type	table	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t1	index	NULL	r	10	NULL	20	
select * from t1 force index(r) order by a desc,b limit 20;
a	b	c
9	NULL	NULL
9	0	9
9	1	19
9	2	29
9	3	39
9	4	49
8	0	8
8	1	18
8	2	28
8	3	38
8	4	48
7	0	7
7	1	17
7	2	27
7	3	37
7	4	47
6	0	6
6	1	16
6	2	26
6	3	36
drop table t1;