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