1--source include/innodb_prefix_index_cluster_optimization.inc
2
3SET SESSION DEFAULT_STORAGE_ENGINE='InnoDB';
4
5set @innodb_stats_persistent_save= @@innodb_stats_persistent;
6set @innodb_stats_persistent_sample_pages_save=
7      @@innodb_stats_persistent_sample_pages;
8
9set global innodb_stats_persistent= 1;
10set global innodb_stats_persistent_sample_pages=100;
11
12CREATE DATABASE dbt3_s001;
13
14use dbt3_s001;
15
16--disable_query_log
17--disable_result_log
18--disable_warnings
19--source include/dbt3_s001.inc
20ANALYZE TABLE lineitem PERSISTENT FOR COLUMNS() INDEXES();
21--enable_warnings
22--enable_result_log
23--enable_query_log
24
25explain
26select count(*) from lineitem where l_orderkey=130 and l_shipdate='1992-07-01';
27flush status;
28select count(*) from lineitem where l_orderkey=130 and l_shipdate='1992-07-01';
29show status like 'handler_read%';
30
31explain
32select count(*) from lineitem use index(primary)
33  where l_orderkey=130 and l_linenumber=2 and l_shipdate='1992-07-01';
34flush status;
35select count(*) from lineitem use index(primary)
36  where l_orderkey=130 and l_linenumber=2 and l_shipdate='1992-07-01';
37show status like 'handler_read%';
38
39explain
40select count(*) from lineitem
41  where l_shipdate='1992-07-01' and l_orderkey between 1 and 1000;
42flush status;
43select count(*) from lineitem
44  where l_shipdate='1992-07-01' and l_orderkey between 1 and 1000;
45show status like 'handler_read%';
46
47explain
48select l_orderkey, l_linenumber from lineitem
49  where l_shipdate='1992-07-01' and l_orderkey between 1001 and 2000;
50flush status;
51select l_orderkey, l_linenumber from lineitem
52  where l_shipdate='1992-07-01' and l_orderkey between 1001 and 2000;
53show status like 'handler_read%';
54
55explain
56select min(l_orderkey) from lineitem where l_shipdate='1992-07-01';
57flush status;
58select min(l_orderkey) from lineitem where l_shipdate='1992-07-01';
59show status like 'handler_read%';
60
61explain
62select min(l_orderkey) from lineitem
63  where l_shipdate='1992-07-01' and l_orderkey between 1001 and 2000;
64flush status;
65select min(l_orderkey) from lineitem
66  where l_shipdate='1992-07-01' and l_orderkey between 1001 and 2000;
67show status like 'handler_read%';
68
69explain
70select max(l_linenumber) from lineitem
71  where l_shipdate='1992-07-01' and l_orderkey=130;
72flush status;
73select max(l_linenumber) from lineitem
74  where l_shipdate='1992-07-01' and l_orderkey=130;
75show status like 'handler_read%';
76
77explain
78select l_orderkey, l_linenumber
79  from lineitem use index (i_l_shipdate, i_l_receiptdate)
80  where l_shipdate='1992-07-01' and l_orderkey=130
81        or l_receiptdate='1992-07-01' and l_orderkey=5603;
82flush status;
83select l_orderkey, l_linenumber
84  from lineitem use index (i_l_shipdate, i_l_receiptdate)
85  where l_shipdate='1992-07-01' and l_orderkey=130
86        or l_receiptdate='1992-07-01' and l_orderkey=5603;
87show status like 'handler_read%';
88
89--replace_column 7 #
90explain
91select l_orderkey, l_linenumber
92  from lineitem use index (i_l_shipdate, i_l_receiptdate)
93  where l_shipdate='1992-07-01' and l_orderkey between 1 and 1000
94         or l_receiptdate='1992-07-01' and l_orderkey between 5001 and 6000;
95flush status;
96select l_orderkey, l_linenumber
97  from lineitem use index (i_l_shipdate, i_l_receiptdate)
98  where l_shipdate='1992-07-01' and l_orderkey between 1 and 1000
99         or l_receiptdate='1992-07-01' and l_orderkey between 5001 and 6000;
100show status like 'handler_read%';
101
102--replace_column 7 # 9 # 10 Using
103explain
104select l_orderkey, l_linenumber from lineitem
105  where l_shipdate='1992-07-01' and l_orderkey between 1 and 1000
106        or l_receiptdate='1992-07-01' and l_orderkey between 5001 and 6000;
107flush status;
108select l_orderkey, l_linenumber from lineitem
109  where l_shipdate='1992-07-01' and l_orderkey between 1 and 1000
110        or l_receiptdate='1992-07-01' and l_orderkey between 5001 and 6000;
111show status like 'handler_read_next';
112
113--replace_column 9 #
114explain
115select max(l_orderkey) from lineitem
116  where l_partkey between 1 and 10 group by l_partkey;
117flush status;
118select max(l_orderkey) from lineitem
119  where l_partkey between 1 and 10 group by l_partkey;
120show status like 'handler_read%';
121
122--replace_column 9 #
123explain
124select max(l_orderkey) from lineitem
125  where l_suppkey in (1,4) group by l_suppkey;
126flush status;
127select max(l_orderkey) from lineitem
128  where l_suppkey in (1,4) group by l_suppkey;
129show status like 'handler_read%';
130
131create index i_p_retailprice on part(p_retailprice);
132
133--replace_column 9 #
134explain
135select o_orderkey, p_partkey
136  from part use index (i_p_retailprice),
137       lineitem use index (i_l_partkey), orders
138  where p_retailprice > 1100 and o_orderdate='1997-01-01'
139        and o_orderkey=l_orderkey and p_partkey=l_partkey;
140flush status;
141select o_orderkey, p_partkey
142  from part use index (i_p_retailprice),
143       lineitem use index (i_l_partkey), orders
144  where p_retailprice > 1100 and o_orderdate='1997-01-01'
145        and o_orderkey=l_orderkey and p_partkey=l_partkey;
146show status like 'handler_read%';
147
148--echo #
149--echo # Bug mdev-3851: ref access used instead of expected eq_ref access
150--echo #                when extended_keys=on
151--echo #
152
153create table t0 (a int);
154insert into t0 values (1), (2), (3), (4), (5);
155create index i_p_size on part(p_size);
156
157explain
158select * from t0, part ignore index (primary)
159  where p_partkey=t0.a and p_size=1;
160
161select * from t0,  part ignore index (primary)
162  where p_partkey=t0.a and p_size=1;
163
164drop table t0;
165drop index i_p_size on part;
166
167DROP DATABASE dbt3_s001;
168
169use test;
170
171--echo #
172--echo # LP Bug #914560: query containing IN subquery
173--echo #                 + extended_keys = on
174--echo #
175
176set @save_optimizer_switch=@@optimizer_switch;
177
178SET optimizer_switch='materialization=on,semijoin=on';
179
180CREATE TABLE t1 (a int, b int) ENGINE=MyISAM;
181INSERT INTO t1 VALUES (1,1), (2,2);
182
183SELECT * FROM t1 WHERE 2 IN (SELECT MAX(s1.a) FROM t1 AS s1, t1 AS s2);
184EXPLAIN
185SELECT * FROM t1 WHERE 2 IN (SELECT MAX(s1.a) FROM t1 AS s1, t1 AS s2);
186
187DROP TABLE t1;
188
189set optimizer_switch=@save_optimizer_switch;
190
191--echo #
192--echo # LP Bug #915291: query using a materialized view
193--echo #                 + extended_keys = on
194--echo # (valgrinf complains fixed by the patch for bug #914560)
195--echo #
196
197SET optimizer_switch = 'derived_with_keys=on';
198
199CREATE TABLE t1 (a varchar(1)) ENGINE=MyISAM;
200INSERT INTO t1 VALUES ('j'), ('v');
201
202CREATE TABLE t2 (b varchar(1)) ENGINE=MyISAM;
203INSERT INTO t2 VALUES ('j'), ('v');
204
205CREATE TABLE t3 (c varchar(1));
206INSERT INTO t2 VALUES ('m'), ('n');
207
208CREATE VIEW v
209  AS SELECT DISTINCT * FROM t2 STRAIGHT_JOIN t3;
210
211SELECT * FROM t1, v WHERE a = b;
212
213DROP VIEW v;
214DROP TABLE t1,t2,t3;
215
216set optimizer_switch=@save_optimizer_switch;
217
218--echo #
219--echo # LP Bug #921167: query containing IN subquery
220--echo #                 + extended_keys = on
221--echo #
222
223CREATE TABLE t1 (
224  a int NOT NULL, b varchar(1) NOT NULL, KEY(a), KEY(b,a)
225) ENGINE=MyISAM;
226INSERT INTO t1 VALUES
227  (0,'j'), (8,'v'), (1,'c'), (8,'m'), (9,'d'),
228  (24,'d'), (6,'y'), (1,'t'), (6,'d'), (2,'s');
229
230CREATE TABLE t2 (
231  c int NOT NULL PRIMARY KEY
232) ENGINE=MyISAM;
233INSERT INTO t2 VALUES
234  (10), (11), (12), (13), (14),
235  (15), (16), (17), (18), (19), (24);
236
237EXPLAIN
238SELECT a FROM t1 AS t, t2
239  WHERE c = a AND b IN (SELECT b FROM t1, t2 WHERE b = t.b);
240SELECT a FROM t1 AS t, t2
241  WHERE c = a AND b IN (SELECT b FROM t1, t2 WHERE b = t.b);
242
243DROP TABLE t1,t2;
244
245--echo #
246--echo # LP Bug #923236: hash join + extended_keys = on
247--echo #
248
249CREATE TABLE t1 (a int) ENGINE=MyISAM;
250
251CREATE TABLE t2 (b int) ENGINE=MyISAM;
252
253INSERT INTO t1 (a) VALUES (4), (6);
254INSERT INTO t2 (b) VALUES (0), (8);
255
256set @save_join_cache_level=@@join_cache_level;
257
258SET join_cache_level=3;
259SET optimizer_switch='join_cache_hashed=on';
260SET optimizer_switch='join_cache_bka=on';
261
262EXPLAIN
263SELECT * FROM t1, t2 WHERE b=a;
264SELECT * FROM t1, t2 WHERE b=a;
265
266set join_cache_level=@save_join_cache_level;
267set optimizer_switch=@save_optimizer_switch;
268
269DROP TABLE t1,t2;
270
271
272--echo #
273--echo # Bug mdev-3888: INSERT with UPDATE on duplicate keys
274--echo #                with extended_keys=on
275--echo #
276
277CREATE TABLE t1 (
278c1 bigint(20) unsigned NOT NULL AUTO_INCREMENT,
279c2 bigint(20) unsigned NOT NULL,
280c3 bigint(20) unsigned NOT NULL,
281c4 varchar(128) DEFAULT NULL,
282PRIMARY KEY (c1),
283UNIQUE KEY uq (c2,c3),
284KEY c3 (c3),
285KEY c4 (c4)
286) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
287
288
289INSERT INTO t1 (c2, c3, c4) VALUES (58291525, 2580, 'foobar')
290  ON DUPLICATE KEY UPDATE c4 = VALUES(c4);
291INSERT INTO t1 (c2, c3, c4) VALUES (58291525, 2580, 'foobar')
292  ON DUPLICATE KEY UPDATE c4 = VALUES(c4);
293
294DROP TABLE t1;
295
296--echo #
297--echo # Bug mdev-4220: using ref instead of eq_ref
298--echo #                with extended_keys=on
299--echo # (performance regression introduced in the patch for mdev-3851)
300--echo #
301
302create table t1 (a int not null) engine=innodb;
303
304insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
305
306create table t2 (
307  pk int primary key, a int not null, b int, unique(a)
308)engine=innodb;
309
310insert into t2
311select
312  A.a + 10 * B.a, A.a + 10 * B.a, A.a + 10 * B.a
313from t1 A, t1 B;
314
315--replace_column 9 #
316explain
317select * from t1, t2 where t2.a=t1.a and t2.b < 2;
318flush status;
319select * from t1, t2 where t2.a=t1.a and t2.b < 2;
320show status like 'handler_read%';
321
322drop table t1,t2;
323
324# this test case did not demonstrate any regression
325# it is added for better testing
326
327create table t1(a int) engine=myisam;
328insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
329
330create table t2(a int) engine=myisam;
331insert into t2 select A.a + 10*B.a + 100*C.a from t1 A, t1 B, t1 C;
332
333create table t3 (
334  pk1 int not null, pk2 int not null, col1 int not null, col2 int not null)
335engine=innodb;
336insert into t3 select a,a,a,a from t2;
337alter table t3 add primary key (pk1, pk2);
338alter table t3 add  key (col1, col2);
339analyze table t1,t3;
340
341--replace_column 9 #
342explain
343select * from t1, t3 where t3.col1=t1.a and  t3.col2=t1.a;
344--replace_column 9 #
345explain
346select * from t1, t3 where t3.col1=t1.a and  t3.col2=t1.a and t3.pk1=t1.a;
347
348drop table t1,t2,t3;
349
350--echo #
351--echo # Bug mdev-4340: performance regression with extended_keys=on
352--echo #
353
354CREATE TABLE t1 (
355  page_id int(8) unsigned NOT NULL AUTO_INCREMENT,
356  page_namespace int(11) NOT NULL DEFAULT '0',
357  page_title varbinary(255) NOT NULL DEFAULT '',
358  page_restrictions tinyblob NOT NULL,
359  page_counter bigint(20) unsigned NOT NULL DEFAULT '0',
360  page_is_redirect tinyint(1) unsigned NOT NULL DEFAULT '0',
361  page_is_new tinyint(1) unsigned NOT NULL DEFAULT '0',
362  page_random double unsigned NOT NULL DEFAULT '0',
363  page_touched varbinary(14) NOT NULL DEFAULT '',
364  page_latest int(8) unsigned NOT NULL DEFAULT '0',
365  page_len int(8) unsigned NOT NULL DEFAULT '0',
366  PRIMARY KEY (page_id),
367  UNIQUE KEY name_title (page_namespace,page_title),
368  KEY page_random (page_random),
369  KEY page_len (page_len),
370  KEY page_redirect_namespace_len (page_is_redirect,page_namespace,page_len)
371) ENGINE=InnoDB AUTO_INCREMENT=38929100 DEFAULT CHARSET=binary;
372INSERT INTO t1 VALUES
373(38928077,0,'Sandbox','',0,0,0,0,'',0,0),(38928078,1,'Sandbox','',0,0,0,1,'',0,0),
374(38928079,2,'Sandbox','',0,0,0,2,'',0,0),(38928080,3,'Sandbox','',0,0,0,3,'',0,0),
375(38928081,4,'Sandbox','',0,0,0,4,'',0,0),(38928082,5,'Sandbox','',0,0,0,5,'',0,0);
376
377CREATE TABLE t2 (
378  rev_id int(8) unsigned NOT NULL AUTO_INCREMENT,
379  rev_page int(8) unsigned NOT NULL DEFAULT '0',
380  rev_text_id int(8) unsigned NOT NULL DEFAULT '0',
381  rev_comment varbinary(255) DEFAULT NULL,
382  rev_user int(5) unsigned NOT NULL DEFAULT '0',
383  rev_user_text varbinary(255) NOT NULL DEFAULT '',
384  rev_timestamp varbinary(14) NOT NULL DEFAULT '',
385  rev_minor_edit tinyint(1) unsigned NOT NULL DEFAULT '0',
386  rev_deleted tinyint(1) unsigned NOT NULL DEFAULT '0',
387  rev_len int(8) unsigned DEFAULT NULL,
388  rev_parent_id int(8) unsigned DEFAULT NULL,
389  rev_sha1 varbinary(32) NOT NULL DEFAULT '',
390  PRIMARY KEY (rev_page,rev_id),
391  UNIQUE KEY rev_id (rev_id),
392  KEY rev_timestamp (rev_timestamp),
393  KEY page_timestamp (rev_page,rev_timestamp),
394  KEY user_timestamp (rev_user,rev_timestamp),
395  KEY usertext_timestamp (rev_user_text,rev_timestamp,rev_user,rev_deleted,rev_minor_edit,rev_text_id,rev_comment)
396) ENGINE=InnoDB DEFAULT CHARSET=binary;
397INSERT INTO t2 VALUES
398(547116222,20,0,NULL,3,'','',0,0,NULL,NULL,''),(547117245,20,0,NULL,4,'','',0,0,NULL,NULL,''),
399(547118268,20,0,NULL,5,'','',0,0,NULL,NULL,''),(547114177,21,0,NULL,1,'','',0,0,NULL,NULL,''),
400(547115200,21,0,NULL,2,'','',0,0,NULL,NULL,''),(547116223,21,0,NULL,3,'','',0,0,NULL,NULL,''),
401(547117246,21,0,NULL,4,'','',0,0,NULL,NULL,''),(547118269,21,0,NULL,5,'','',0,0,NULL,NULL,''),
402(547114178,22,0,NULL,1,'','',0,0,NULL,NULL,''),(547115201,22,0,NULL,2,'','',0,0,NULL,NULL,''),
403(547116224,22,0,NULL,3,'','',0,0,NULL,NULL,''),(547117247,22,0,NULL,4,'','',0,0,NULL,NULL,''),
404(547116226,24,0,NULL,3,'','',0,0,NULL,NULL,''),(547117249,24,0,NULL,4,'','',0,0,NULL,NULL,''),
405(547118272,24,0,NULL,5,'','',0,0,NULL,NULL,''),(547114181,25,0,NULL,1,'','',0,0,NULL,NULL,''),
406(547115204,25,0,NULL,2,'','',0,0,NULL,NULL,''),(547116227,25,0,NULL,3,'','',0,0,NULL,NULL,''),
407(547116157,978,0,NULL,2,'','',0,0,NULL,NULL,''),(547117180,978,0,NULL,3,'','',0,0,NULL,NULL,''),
408(547118203,978,0,NULL,4,'','',0,0,NULL,NULL,''),(547119226,978,0,NULL,5,'','',0,0,NULL,NULL,''),
409(547115135,979,0,NULL,1,'','',0,0,NULL,NULL,''),(547116158,979,0,NULL,2,'','',0,0,NULL,NULL,''),
410(547116173,994,0,NULL,2,'','',0,0,NULL,NULL,''),(547117196,994,0,NULL,3,'','',0,0,NULL,NULL,''),
411(547118219,994,0,NULL,4,'','',0,0,NULL,NULL,''),(547119242,994,0,NULL,5,'','',0,0,NULL,NULL,''),
412(547115151,995,0,NULL,1,'','',0,0,NULL,NULL,''),(547116174,995,0,NULL,2,'','',0,0,NULL,NULL,''),
413(547117197,995,0,NULL,3,'','',0,0,NULL,NULL,''),(547118220,995,0,NULL,4,'','',0,0,NULL,NULL,''),
414(547118223,998,0,NULL,4,'','',0,0,NULL,NULL,''),(547119246,998,0,NULL,5,'','',0,0,NULL,NULL,''),
415(547115155,999,0,NULL,1,'','',0,0,NULL,NULL,''),(547116178,999,0,NULL,2,'','',0,0,NULL,NULL,''),
416(547117201,999,0,NULL,3,'','',0,0,NULL,NULL,''),(547118224,999,0,NULL,4,'','',0,0,NULL,NULL,''),
417(547117213,999,0,NULL,3,'','',0,0,NULL,NULL,''),(547117217,999,0,NULL,4,'','',0,0,NULL,NULL,''),
418(547117214,999,0,NULL,3,'','',0,0,NULL,NULL,''),(547117218,999,0,NULL,4,'','',0,0,NULL,NULL,''),
419(547117215,999,0,NULL,3,'','',0,0,NULL,NULL,''),(547117219,999,0,NULL,4,'','',0,0,NULL,NULL,''),
420(547117216,999,0,NULL,3,'','',0,0,NULL,NULL,''),(547117220,999,0,NULL,4,'','',0,0,NULL,NULL,''),
421(547119271,38928081,0,NULL,10,'','',0,0,NULL,NULL,''),(547119272,38928081,0,NULL,11,'','',0,0,NULL,NULL,''),
422(547119273,38928081,0,NULL,12,'','',0,0,NULL,NULL,''),(547119274,38928081,0,NULL,13,'','',0,0,NULL,NULL,''),
423(547119275,38928081,0,NULL,14,'','',0,0,NULL,NULL,''),(547119276,38928081,0,NULL,15,'','',0,0,NULL,NULL,''),
424(547119277,38928081,0,NULL,16,'','',0,0,NULL,NULL,''),(547119278,38928081,0,NULL,17,'','',0,0,NULL,NULL,''),
425(547119279,38928081,0,NULL,18,'','',0,0,NULL,NULL,''),(547119280,38928081,0,NULL,19,'','',0,0,NULL,NULL,'');
426
427CREATE TABLE t3 (
428  old_id int(10) unsigned NOT NULL AUTO_INCREMENT,
429  old_text mediumblob NOT NULL,
430  old_flags tinyblob NOT NULL,
431  PRIMARY KEY (old_id)
432) ENGINE=InnoDB DEFAULT CHARSET=latin1;
433INSERT INTO t3 VALUES
434(1,'text-0',''),(2,'text-1000',''),(3,'text-2000',''),(4,'text-3000',''),
435(5,'text-4000',''),(6,'text-5000',''),(7,'text-6000',''),(8,'text-7000',''),
436(9,'text-8000',''),(10,'text-9000',''),(11,'text-1',''),(12,'text-1001',''),
437(13,'text-2001',''),(14,'text-3001',''),(15,'text-4001',''),(16,'text-5001',''),
438(17,'text-6001',''),(18,'text-7001',''),(19,'text-8001',''),(20,'text-9001',''),
439(21,'text-2',''),(22,'text-1002',''),(23,'text-2002',''),(24,'text-3002',''),
440(25,'text-4002',''),(26,'text-5002',''),(27,'text-6002',''),(28,'text-7002',''),
441(29,'text-8002',''),(30,'text-9002',''),(31,'text-3',''),(32,'text-1003',''),
442(33,'text-2003',''),(34,'text-3003',''),(35,'text-4003',''),(36,'text-5003',''),
443(37,'text-6003',''),(38,'text-7003',''),(39,'text-8003',''),(40,'text-9003',''),
444(41,'text-4',''),(42,'text-1004',''),(43,'text-2004',''),(44,'text-3004',''),
445(45,'text-4004',''),(46,'text-5004',''),(47,'text-6004',''),(48,'text-7004',''),
446(49,'text-8004',''),(50,'text-9004',''),(51,'text-5',''),(52,'text-1005',''),
447(53,'text-2005',''),(54,'text-3005',''),(55,'text-4005',''),(56,'text-5005',''),
448(57,'text-6005',''),(58,'text-7005',''),(59,'text-8005',''),(60,'text-9005',''),
449(61,'text-6',''),(62,'text-1006',''),(63,'text-2006',''),(64,'text-3006',''),
450(65,'text-4006',''),(66,'text-5006',''),(67,'text-6006',''),(68,'text-7006',''),
451(69,'text-8006',''),(70,'text-9006',''),(71,'text-7',''),(72,'text-1007',''),
452(73,'text-2007',''),(74,'text-3007',''),(75,'text-4007',''),(76,'text-5007',''),
453(77,'text-6007',''),(78,'text-7007',''),(79,'text-8007',''),(80,'text-9007',''),
454(81,'text-8',''),(82,'text-1008',''),(83,'text-2008',''),(84,'text-3008',''),
455(85,'text-4008',''),(86,'text-5008',''),(87,'text-6008',''),(88,'text-7008',''),
456(89,'text-8008',''),(90,'text-9008',''),(91,'text-9',''),(92,'text-1009',''),
457(93,'text-2009',''),(94,'text-3009',''),(95,'text-4009',''),(96,'text-5009',''),
458(97,'text-6009',''),(98,'text-7009',''),(99,'text-8009',''),(100,'text-9009','');
459
460
461EXPLAIN
462SELECT *  FROM t1, t2 IGNORE INDEX (PRIMARY), t3
463  WHERE page_id=rev_page AND  rev_text_id=old_id  AND page_namespace=4 AND page_title='Sandbox'
464ORDER BY rev_timestamp ASC LIMIT 10;
465
466DROP TABLE t1,t2,t3;
467
468--echo #
469--echo # MDEV-5424 SELECT using ORDER BY DESC and LIMIT produces unexpected
470--echo # results (InnoDB/XtraDB)
471--echo #
472
473create table t1 (a bigint not null unique auto_increment, b varchar(10), primary key (a), key (b(2))) engine = myisam default character set utf8;
474create table t2 (a bigint not null unique auto_increment, b varchar(10), primary key (a), key (b(2))) engine = innodb default character set utf8;
475
476insert into t1 (b) values (null), (null), (null);
477insert into t2 (b) values (null), (null), (null);
478
479analyze table t1,t2;
480
481explain select a from t1 where b is null order by a desc limit 2;
482select a from t1 where b is null order by a desc limit 2;
483explain select a from t2 where b is null order by a desc limit 2;
484select a from t2 where b is null order by a desc limit 2;
485
486explain select a from t2 where b is null order by a desc;
487select a from t2 where b is null order by a desc;
488
489explain select a from t2 where b is null order by a desc,a,a;
490select a from t2 where b is null order by a desc,a,a;
491
492drop table t1, t2;
493
494--echo #
495--echo # MDEV-10325: Queries examines all rows of a tables when it should not
496--echo #
497create table t0 (a int);
498insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
499
500create table t1 (
501  pk int not null,
502  col1 varchar(32),
503  filler varchar(100),
504  key idx1(col1(10)),
505  primary key (pk)
506)engine=innodb;
507
508insert into t1
509select
510  A.a + 10*B.a + 100*C.a,
511  concat('1234567890-', 1000+ A.a + 10*B.a + 100*C.a),
512  repeat('filler-data-', 4)
513from
514  t0 A, t0 B, t0 C;
515
516let $q=explain select * from t1 where col1='1234567890-a';
517let $rows=query_get_value($q, rows, 1);
518if ($rows < 2)
519{
520  --echo The EXPLAIN should not produce a query plan with type=ref, rows=1
521  --die Fix for MDEV-10325 didnt work;
522}
523
524drop table t0,t1;
525
526--echo #
527--echo #
528--echo # MDEV-10360: Extended keys: index properties depend on index order
529--echo #
530create table t0 (a int);
531insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
532
533create table t1 (
534  index_id bigint(20) unsigned NOT NULL,
535  index_class varchar(265) COLLATE latin1_general_ci DEFAULT NULL ,
536  index_object_id int(10) unsigned NOT NULL DEFAULT '0' ,
537  index_date_updated int(10) unsigned DEFAULT NULL ,
538
539  PRIMARY KEY (index_id),
540  KEY object (index_class(181),index_object_id),
541  KEY index_date_updated (index_date_updated)
542) engine=innodb;
543
544create table t2 (
545  index_id bigint(20) unsigned NOT NULL,
546  index_class varchar(265) COLLATE latin1_general_ci DEFAULT NULL ,
547  index_object_id int(10) unsigned NOT NULL DEFAULT '0' ,
548  index_date_updated int(10) unsigned DEFAULT NULL ,
549
550  PRIMARY KEY (index_id),
551  KEY index_date_updated (index_date_updated),
552  KEY object (index_class(181),index_object_id)
553) engine=innodb;
554
555insert into t1 select
556  @a:=A.a + 10*B.a + 100*C.a,
557  concat('val-', @a),
558  123456,
559  A.a + 10*B.a
560from
561  t0 A, t0 B, t0 C;
562
563insert into t2 select * from t1;
564
565--echo # This must have the same query plan as the query below it:
566--echo # type=range, key=index_date_updated, key_len=13
567--replace_column 9 #
568explain
569select * from t1 force index(index_date_updated)
570where index_date_updated= 10 and index_id < 800;
571
572--echo # This used to work from the start:
573--replace_column 9 #
574explain
575select * from t2 force index(index_date_updated)
576where index_date_updated= 10 and index_id < 800;
577
578drop table t0,t1,t2;
579
580
581--echo #
582--echo # MDEV-11196: Error:Run-Time Check Failure #2 - Stack around the variable 'key_buff'
583--echo # was corrupted, server crashes in opt_sum_query
584
585SET @save_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity, @@optimizer_use_condition_selectivity=4;
586
587CREATE TABLE t1 (
588  pk INT,
589  f1 VARCHAR(3),
590  f2 VARCHAR(1024),
591  PRIMARY KEY (pk),
592  KEY(f2)
593) ENGINE=InnoDB CHARSET utf8 ROW_FORMAT= DYNAMIC;
594
595INSERT INTO t1 VALUES (1,'foo','abc'),(2,'bar','def');
596SELECT MAX(t2.pk) FROM t1 t2 INNER JOIN t1 t3 ON t2.f1 = t3.f1 WHERE t2.pk <= 4;
597drop table t1;
598
599CREATE TABLE t1 (
600  pk1 INT,
601  pk2 INT,
602  f1 VARCHAR(3),
603  f2 VARCHAR(1021),
604  PRIMARY KEY (pk1,pk2),
605  KEY(f2)
606) ENGINE=InnoDB CHARSET utf8 ROW_FORMAT= DYNAMIC;
607
608INSERT INTO t1 VALUES (1,2,'2','abc'),(2,3,'3','def');
609explain format= json
610select * from t1 force index(f2)  where pk1 <= 5 and pk2 <=5 and f2 = 'abc' and f1 <= '3';
611drop table t1;
612
613CREATE TABLE t1 (
614f2 INT,
615pk2 INT,
616f1 VARCHAR(3),
617pk1 VARCHAR(1000),
618PRIMARY KEY (pk1,pk2),
619KEY k1(pk1,f2)
620) ENGINE=InnoDB CHARSET utf8 ROW_FORMAT= DYNAMIC;
621INSERT INTO t1 VALUES (1,2,'2','abc'),(2,3,'3','def');
622explain format= json
623select * from t1 force index(k1)  where f2 <= 5 and pk2 <=5 and pk1 = 'abc' and f1 <= '3';
624drop table t1;
625
626SET optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
627
628--echo #
629--echo # MDEV-11172: EXPLAIN shows non-sensical value for key_len with type=index
630--echo #
631
632CREATE TABLE t1(a INT);
633INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
634
635CREATE TABLE t2 (
636  pk VARCHAR(50),
637  a VARCHAR(20),
638  KEY k1(a),
639  PRIMARY KEY(pk)
640)ENGINE=INNODB;
641
642INSERT INTO t2 SELECT a,a FROM t1;
643--replace_column 9 #
644EXPLAIN SELECT pk FROM t2 FORCE INDEX(k1);
645
646DROP TABLE t1,t2;
647
648set global innodb_stats_persistent= @innodb_stats_persistent_save;
649set global innodb_stats_persistent_sample_pages= @innodb_stats_persistent_sample_pages_save;
650