1--source include/innodb_prefix_index_cluster_optimization.inc
2
3SET SESSION 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(547119271,38928081,0,NULL,10,'','',0,0,NULL,NULL,''),(547119272,38928081,0,NULL,11,'','',0,0,NULL,NULL,''),
418(547119273,38928081,0,NULL,12,'','',0,0,NULL,NULL,''),(547119274,38928081,0,NULL,13,'','',0,0,NULL,NULL,''),
419(547119275,38928081,0,NULL,14,'','',0,0,NULL,NULL,''),(547119276,38928081,0,NULL,15,'','',0,0,NULL,NULL,''),
420(547119277,38928081,0,NULL,16,'','',0,0,NULL,NULL,''),(547119278,38928081,0,NULL,17,'','',0,0,NULL,NULL,''),
421(547119279,38928081,0,NULL,18,'','',0,0,NULL,NULL,''),(547119280,38928081,0,NULL,19,'','',0,0,NULL,NULL,'');
422
423CREATE TABLE t3 (
424  old_id int(10) unsigned NOT NULL AUTO_INCREMENT,
425  old_text mediumblob NOT NULL,
426  old_flags tinyblob NOT NULL,
427  PRIMARY KEY (old_id)
428) ENGINE=InnoDB DEFAULT CHARSET=latin1;
429INSERT INTO t3 VALUES
430(1,'text-0',''),(2,'text-1000',''),(3,'text-2000',''),(4,'text-3000',''),
431(5,'text-4000',''),(6,'text-5000',''),(7,'text-6000',''),(8,'text-7000',''),
432(9,'text-8000',''),(10,'text-9000',''),(11,'text-1',''),(12,'text-1001',''),
433(13,'text-2001',''),(14,'text-3001',''),(15,'text-4001',''),(16,'text-5001',''),
434(17,'text-6001',''),(18,'text-7001',''),(19,'text-8001',''),(20,'text-9001',''),
435(21,'text-2',''),(22,'text-1002',''),(23,'text-2002',''),(24,'text-3002',''),
436(25,'text-4002',''),(26,'text-5002',''),(27,'text-6002',''),(28,'text-7002',''),
437(29,'text-8002',''),(30,'text-9002',''),(31,'text-3',''),(32,'text-1003',''),
438(33,'text-2003',''),(34,'text-3003',''),(35,'text-4003',''),(36,'text-5003',''),
439(37,'text-6003',''),(38,'text-7003',''),(39,'text-8003',''),(40,'text-9003',''),
440(41,'text-4',''),(42,'text-1004',''),(43,'text-2004',''),(44,'text-3004',''),
441(45,'text-4004',''),(46,'text-5004',''),(47,'text-6004',''),(48,'text-7004',''),
442(49,'text-8004',''),(50,'text-9004',''),(51,'text-5',''),(52,'text-1005',''),
443(53,'text-2005',''),(54,'text-3005',''),(55,'text-4005',''),(56,'text-5005',''),
444(57,'text-6005',''),(58,'text-7005',''),(59,'text-8005',''),(60,'text-9005',''),
445(61,'text-6',''),(62,'text-1006',''),(63,'text-2006',''),(64,'text-3006',''),
446(65,'text-4006',''),(66,'text-5006',''),(67,'text-6006',''),(68,'text-7006',''),
447(69,'text-8006',''),(70,'text-9006',''),(71,'text-7',''),(72,'text-1007',''),
448(73,'text-2007',''),(74,'text-3007',''),(75,'text-4007',''),(76,'text-5007',''),
449(77,'text-6007',''),(78,'text-7007',''),(79,'text-8007',''),(80,'text-9007',''),
450(81,'text-8',''),(82,'text-1008',''),(83,'text-2008',''),(84,'text-3008',''),
451(85,'text-4008',''),(86,'text-5008',''),(87,'text-6008',''),(88,'text-7008',''),
452(89,'text-8008',''),(90,'text-9008',''),(91,'text-9',''),(92,'text-1009',''),
453(93,'text-2009',''),(94,'text-3009',''),(95,'text-4009',''),(96,'text-5009',''),
454(97,'text-6009',''),(98,'text-7009',''),(99,'text-8009',''),(100,'text-9009','');
455
456
457EXPLAIN
458SELECT *  FROM t1, t2 IGNORE INDEX (PRIMARY), t3
459  WHERE page_id=rev_page AND  rev_text_id=old_id  AND page_namespace=4 AND page_title='Sandbox'
460ORDER BY rev_timestamp ASC LIMIT 10;
461
462DROP TABLE t1,t2,t3;
463
464--echo #
465--echo # MDEV-5424 SELECT using ORDER BY DESC and LIMIT produces unexpected
466--echo # results (InnoDB/XtraDB)
467--echo #
468
469create table t1 (a bigint not null unique auto_increment, b varchar(10), primary key (a), key (b(2))) engine = myisam default character set utf8;
470create table t2 (a bigint not null unique auto_increment, b varchar(10), primary key (a), key (b(2))) engine = innodb default character set utf8;
471
472insert into t1 (b) values (null), (null), (null);
473insert into t2 (b) values (null), (null), (null);
474
475analyze table t1,t2;
476
477explain select a from t1 where b is null order by a desc limit 2;
478select a from t1 where b is null order by a desc limit 2;
479explain select a from t2 where b is null order by a desc limit 2;
480select a from t2 where b is null order by a desc limit 2;
481
482explain select a from t2 where b is null order by a desc;
483select a from t2 where b is null order by a desc;
484
485explain select a from t2 where b is null order by a desc,a,a;
486select a from t2 where b is null order by a desc,a,a;
487
488drop table t1, t2;
489
490--echo #
491--echo # MDEV-10325: Queries examines all rows of a tables when it should not
492--echo #
493create table t0 (a int);
494insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
495
496create table t1 (
497  pk int not null,
498  col1 varchar(32),
499  filler varchar(100),
500  key idx1(col1(10)),
501  primary key (pk)
502)engine=innodb;
503
504insert into t1
505select
506  A.a + 10*B.a + 100*C.a,
507  concat('1234567890-', 1000+ A.a + 10*B.a + 100*C.a),
508  repeat('filler-data-', 4)
509from
510  t0 A, t0 B, t0 C;
511
512let $q=explain select * from t1 where col1='1234567890-a';
513let $rows=query_get_value($q, rows, 1);
514if ($rows < 2)
515{
516  --echo The EXPLAIN should not produce a query plan with type=ref, rows=1
517  --die Fix for MDEV-10325 didnt work;
518}
519
520drop table t0,t1;
521
522--echo #
523--echo #
524--echo # MDEV-10360: Extended keys: index properties depend on index order
525--echo #
526create table t0 (a int);
527insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
528
529create table t1 (
530  index_id bigint(20) unsigned NOT NULL,
531  index_class varchar(265) COLLATE latin1_general_ci DEFAULT NULL ,
532  index_object_id int(10) unsigned NOT NULL DEFAULT '0' ,
533  index_date_updated int(10) unsigned DEFAULT NULL ,
534
535  PRIMARY KEY (index_id),
536  KEY object (index_class(181),index_object_id),
537  KEY index_date_updated (index_date_updated)
538) engine=innodb;
539
540create table t2 (
541  index_id bigint(20) unsigned NOT NULL,
542  index_class varchar(265) COLLATE latin1_general_ci DEFAULT NULL ,
543  index_object_id int(10) unsigned NOT NULL DEFAULT '0' ,
544  index_date_updated int(10) unsigned DEFAULT NULL ,
545
546  PRIMARY KEY (index_id),
547  KEY index_date_updated (index_date_updated),
548  KEY object (index_class(181),index_object_id)
549) engine=innodb;
550
551insert into t1 select
552  @a:=A.a + 10*B.a + 100*C.a,
553  concat('val-', @a),
554  123456,
555  A.a + 10*B.a
556from
557  t0 A, t0 B, t0 C;
558
559insert into t2 select * from t1;
560
561--echo # This must have the same query plan as the query below it:
562--echo # type=range, key=index_date_updated, key_len=13
563--replace_column 9 #
564explain
565select * from t1 force index(index_date_updated)
566where index_date_updated= 10 and index_id < 800;
567
568--echo # This used to work from the start:
569--replace_column 9 #
570explain
571select * from t2 force index(index_date_updated)
572where index_date_updated= 10 and index_id < 800;
573
574drop table t0,t1,t2;
575
576
577--echo #
578--echo # MDEV-11196: Error:Run-Time Check Failure #2 - Stack around the variable 'key_buff'
579--echo # was corrupted, server crashes in opt_sum_query
580
581SET @save_optimizer_use_condition_selectivity=@@optimizer_use_condition_selectivity, @@optimizer_use_condition_selectivity=4;
582
583CREATE TABLE t1 (
584  pk INT,
585  f1 VARCHAR(3),
586  f2 VARCHAR(1024),
587  PRIMARY KEY (pk),
588  KEY(f2)
589) ENGINE=InnoDB CHARSET utf8 ROW_FORMAT= DYNAMIC;
590
591INSERT INTO t1 VALUES (1,'foo','abc'),(2,'bar','def');
592SELECT MAX(t2.pk) FROM t1 t2 INNER JOIN t1 t3 ON t2.f1 = t3.f1 WHERE t2.pk <= 4;
593drop table t1;
594
595CREATE TABLE t1 (
596  pk1 INT,
597  pk2 INT,
598  f1 VARCHAR(3),
599  f2 VARCHAR(1021),
600  PRIMARY KEY (pk1,pk2),
601  KEY(f2)
602) ENGINE=InnoDB CHARSET utf8 ROW_FORMAT= DYNAMIC;
603
604INSERT INTO t1 VALUES (1,2,'2','abc'),(2,3,'3','def');
605explain format= json
606select * from t1 force index(f2)  where pk1 <= 5 and pk2 <=5 and f2 = 'abc' and f1 <= '3';
607drop table t1;
608
609CREATE TABLE t1 (
610f2 INT,
611pk2 INT,
612f1 VARCHAR(3),
613pk1 VARCHAR(1000),
614PRIMARY KEY (pk1,pk2),
615KEY k1(pk1,f2)
616) ENGINE=InnoDB CHARSET utf8 ROW_FORMAT= DYNAMIC;
617INSERT INTO t1 VALUES (1,2,'2','abc'),(2,3,'3','def');
618explain format= json
619select * from t1 force index(k1)  where f2 <= 5 and pk2 <=5 and pk1 = 'abc' and f1 <= '3';
620drop table t1;
621
622SET optimizer_use_condition_selectivity=@save_optimizer_use_condition_selectivity;
623
624--echo #
625--echo # MDEV-11172: EXPLAIN shows non-sensical value for key_len with type=index
626--echo #
627
628CREATE TABLE t1(a INT);
629INSERT INTO t1 VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
630
631CREATE TABLE t2 (
632  pk VARCHAR(50),
633  a VARCHAR(20),
634  KEY k1(a),
635  PRIMARY KEY(pk)
636)ENGINE=INNODB;
637
638INSERT INTO t2 SELECT a,a FROM t1;
639--replace_column 9 #
640EXPLAIN SELECT pk FROM t2 FORCE INDEX(k1);
641
642DROP TABLE t1,t2;
643
644set global innodb_stats_persistent= @innodb_stats_persistent_save;
645set global innodb_stats_persistent_sample_pages= @innodb_stats_persistent_sample_pages_save;
646