MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mrr_tests.inc
1 
2 create table t1(a int);
3 show create table t1;
4 insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
5 create table t2(a int);
6 insert into t2 select A.a + 10*(B.a + 10*C.a) from t1 A, t1 B, t1 C;
7 
8 
9 create table t3 (
10  a char(8) not null, b char(8) not null, filler char(200),
11  key(a)
12 );
13 insert into t3 select @a:=concat('c-', 1000+ A.a, '=w'), @a, 'filler' from t2 A;
14 insert into t3 select concat('c-', 1000+A.a, '=w'), concat('c-', 2000+A.a, '=w'),
15  'filler-1' from t2 A;
16 insert into t3 select concat('c-', 1000+A.a, '=w'), concat('c-', 3000+A.a, '=w'),
17  'filler-2' from t2 A;
18 
19 # Test empty result set
20 select a,filler from t3 where a >= 'c-9011=w';
21 
22 # Ok, t3.ref_length=6, limit is 64 => 10 elements fit into the buffer
23 # Test the cases when buffer gets exhausted at different points in source
24 # intervals:
25 
26 # 1. Split is in the middle of the range
27 --sorted_result
28 select a,filler from t3 where a >= 'c-1011=w' and a <= 'c-1015=w';
29 
30 # 2. Split is at range edge
31 --sorted_result
32 select a,filler from t3 where (a>='c-1011=w' and a <= 'c-1013=w') or
33  (a>='c-1014=w' and a <= 'c-1015=w');
34 
35 # 3. Split is at range edge, with some rows between ranges.
36 insert into t3 values ('c-1013=z', 'c-1013=z', 'err');
37 insert into t3 values ('a-1014=w', 'a-1014=w', 'err');
38 
39 --sorted_result
40 select a,filler from t3 where (a>='c-1011=w' and a <= 'c-1013=w') or
41  (a>='c-1014=w' and a <= 'c-1015=w');
42 delete from t3 where b in ('c-1013=z', 'a-1014=w');
43 
44 # 4. Split is within the equality range.
45 --sorted_result
46 select a,filler from t3 where a='c-1011=w' or a='c-1012=w' or a='c-1013=w' or
47  a='c-1014=w' or a='c-1015=w';
48 
49 # 5. Split is at the edge of equality range.
50 insert into t3 values ('c-1013=w', 'del-me', 'inserted');
51 --sorted_result
52 select a,filler from t3 where a='c-1011=w' or a='c-1012=w' or a='c-1013=w' or
53  a='c-1014=w' or a='c-1015=w';
54 delete from t3 where b='del-me';
55 
56 # PK tests are not included here.
57 
58 alter table t3 add primary key(b);
59 
60 ## PK scan tests
61 # 6. Split is between 'unique' PK ranges
62 select b,filler from t3 where (b>='c-1011=w' and b<= 'c-1018=w') or
63  b IN ('c-1019=w', 'c-1020=w', 'c-1021=w',
64  'c-1022=w', 'c-1023=w', 'c-1024=w');
65 
66 # 7. Between non-uniq and uniq range
67 select b,filler from t3 where (b>='c-1011=w' and b<= 'c-1020=w') or
68  b IN ('c-1021=w', 'c-1022=w', 'c-1023=w');
69 
70 # 8. Between uniq and non-uniq range
71 select b,filler from t3 where (b>='c-1011=w' and b<= 'c-1018=w') or
72  b IN ('c-1019=w', 'c-1020=w') or
73  (b>='c-1021=w' and b<= 'c-1023=w');
74 ## End of PK scan tests
75 
76 #
77 # Now try different keypart types and special values
78 #
79 create table t4 (a varchar(10), b int, c char(10), filler char(200),
80  key idx1 (a, b, c));
81 
82 # insert buffer_size * 1.5 all-NULL tuples
83 insert into t4 (filler) select concat('NULL-', 15-a) from t2 order by a limit 15;
84 
85 insert into t4 (a,b,c,filler)
86  select 'b-1',NULL,'c-1', concat('NULL-', 15-a) from t2 order by a limit 15;
87 insert into t4 (a,b,c,filler)
88  select 'b-1',NULL,'c-222', concat('NULL-', 15-a) from t2 order by a limit 15;
89 insert into t4 (a,b,c,filler)
90  select 'bb-1',NULL,'cc-2', concat('NULL-', 15-a) from t2 order by a limit 15;
91 insert into t4 (a,b,c,filler)
92  select 'zz-1',NULL,'cc-2', 'filler-data' from t2 order by a limit 500;
93 
94 -- disable_query_log
95 -- disable_result_log
96 ANALYZE TABLE t4;
97 -- enable_result_log
98 -- enable_query_log
99 
100 explain
101  select * from t4 where a IS NULL and b IS NULL and (c IS NULL or c='no-such-row1'
102  or c='no-such-row2');
103 select * from t4 where a IS NULL and b IS NULL and (c IS NULL or c='no-such-row1'
104  or c='no-such-row2');
105 
106 explain
107  select * from t4 where (a ='b-1' or a='bb-1') and b IS NULL and (c='c-1' or c='cc-2');
108 select * from t4 where (a ='b-1' or a='bb-1') and b IS NULL and (c='c-1' or c='cc-2');
109 
110 select * from t4 ignore index(idx1) where (a ='b-1' or a='bb-1') and b IS NULL and (c='c-1' or c='cc-2');
111 drop table t1, t2, t3, t4;
112 
113 #
114 # Check how ICP works with NULLs and partially-covered indexes
115 #
116 create table t1 (a int, b int not null,unique key (a,b),index(b));
117 insert ignore into t1 values (1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(null,7),(9,9),(8,8),(7,7),(null,9),(null,9),(6,6);
118 create table t2 like t1;
119 insert into t2 select * from t1;
120 alter table t1 modify b blob not null, add c int not null, drop key a, add unique key (a,b(20),c), drop key b, add key (b(10));
121 
122 select * from t1 where a is null;
123 select * from t1 where (a is null or a > 0 and a < 3) and b > 7 limit 3;
124 
125 select * from t1 where a is null and b=9 or a is null and b=7 limit 3;
126 drop table t1, t2;
127 
128 #
129 # BUG#30622: Incorrect query results for MRR + filesort
130 #
131 CREATE TABLE t1 (
132  ID int(10) unsigned NOT NULL AUTO_INCREMENT,
133  col1 int(10) unsigned DEFAULT NULL,
134  key1 int(10) unsigned NOT NULL DEFAULT '0',
135  key2 int(10) unsigned DEFAULT NULL,
136  text1 text,
137  text2 text,
138  col2 smallint(6) DEFAULT '100',
139  col3 enum('headers','bodyandsubject') NOT NULL DEFAULT 'bodyandsubject',
140  col4 tinyint(3) unsigned NOT NULL DEFAULT '0',
141  PRIMARY KEY (ID),
142  KEY (key1),
143  KEY (key2)
144 ) AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
145 
146 INSERT INTO t1 VALUES
147 (1,NULL,1130,NULL,'Hello',NULL,100,'bodyandsubject',0),
148 (2,NULL,1130,NULL,'bye',NULL,100,'bodyandsubject',0),
149 (3,NULL,1130,NULL,'red',NULL,100,'bodyandsubject',0),
150 (4,NULL,1130,NULL,'yellow',NULL,100,'bodyandsubject',0),
151 (5,NULL,1130,NULL,'blue',NULL,100,'bodyandsubject',0);
152 
153 select * FROM t1 WHERE key1=1130 AND col1 IS NULL ORDER BY text1;
154 
155 drop table t1;
156 
157 
158 --echo
159 --echo BUG#37851: Crash in test_if_skip_sort_order tab->select is zero
160 --echo
161 CREATE TABLE t1 (
162  pk int(11) NOT NULL AUTO_INCREMENT,
163  PRIMARY KEY (pk)
164 );
165 INSERT INTO t1 VALUES (1);
166 
167 CREATE TABLE t2 (
168  pk int(11) NOT NULL AUTO_INCREMENT,
169  int_key int(11) DEFAULT NULL,
170  PRIMARY KEY (pk),
171  KEY int_key (int_key)
172 );
173 INSERT INTO t2 VALUES (1,1),(2,6),(3,0);
174 
175 -- disable_query_log
176 -- disable_result_log
177 ANALYZE TABLE t1;
178 ANALYZE TABLE t2;
179 -- enable_result_log
180 -- enable_query_log
181 
182 EXPLAIN EXTENDED
183 SELECT MIN(t1.pk)
184 FROM t1 WHERE EXISTS (
185  SELECT t2.pk
186  FROM t2
187  WHERE t2.int_key IS NULL
188  GROUP BY t2.pk
189 );
190 
191 DROP TABLE t1, t2;
192 
193 -- echo #
194 -- echo # BUG#42048 Discrepancy between MyISAM and Maria's ICP implementation
195 -- echo #
196 create table t0 (a int);
197 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
198 create table t1 (a int, b char(20), filler char(200), key(a,b(10)));
199 insert into t1 select A.a + 10*(B.a + 10*C.a), 'bbb','filler' from t0 A, t0 B, t0 C;
200 update t1 set b=repeat(char(65+a), 20) where a < 25;
201 
202 -- disable_query_log
203 -- disable_result_log
204 ANALYZE TABLE t1;
205 -- enable_result_log
206 -- enable_query_log
207 
208 --echo This must show range + using index condition:
209 --replace_column 9 x
210 explain select * from t1 where a < 10 and b = repeat(char(65+a), 20);
211 select * from t1 where a < 10 and b = repeat(char(65+a), 20);
212 drop table t0,t1;
213 
214 -- echo #
215 -- echo # BUG#41136: ORDER BY + range access: EXPLAIN shows "Using MRR" while MRR is actually not used
216 -- echo #
217 create table t0 (a int);
218 insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
219 create table t1 (a int, b int, key(a));
220 insert into t1 select A.a + 10 *(B.a + 10*C.a), A.a + 10 *(B.a + 10*C.a) from t0 A, t0 B, t0 C;
221 -- disable_query_log
222 -- disable_result_log
223 ANALYZE TABLE t1;
224 -- enable_result_log
225 -- enable_query_log
226 -- echo This mustn't show "Using MRR":
227 explain select * from t1 where a < 20 order by a;
228 drop table t0, t1;
229 
230 # Try big rowid sizes
231 set @read_rnd_buffer_size_save= @@read_rnd_buffer_size;
232 set read_rnd_buffer_size=64;
233 
234 # By default InnoDB will fill values only for key parts used by the query,
235 # which will cause DS-MRR to supply an invalid tuple on scan restoration.
236 # This test was originally developed for verifying that DS-MRR's code
237 # extra(HA_EXTRA_RETRIEVE_ALL_COLS) call has effect. This has now been
238 # replaced by using the table's read_set bitmap.
239 create table t1(a int);
240 insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
241 create table t2(a char(8), b char(8), c char(8), filler char(100), key k1(a,b,c) );
242 
243 insert into t2 select
244  concat('a-', 1000 + A.a, '-a'),
245  concat('b-', 1000 + B.a, '-b'),
246  concat('c-', 1000 + C.a, '-c'),
247  'filler'
248 from t1 A, t1 B, t1 C;
249 
250 # The use of "force index" is to ensure the query is done as a range scan.
251 # Without "force index", InnoDB's record count estimate is sometimes
252 # ~400 instead of 1000, which causes a table scan.
253 let query=
254 select count(length(a) + length(filler))
255 from t2 force index (k1)
256 where a>='a-1000-a' and a <'a-1001-a';
257 
258 # The expected rows differs a bit with different page sizes
259 --replace_result 98 ROWS 99 ROWS
260 eval EXPLAIN $query;
261 eval $query;
262 
263 drop table t2;
264 
265 # Try a very big rowid
266 create table t2 (a char(100), b char(100), c char(100), d int,
267  filler char(10), key(d), primary key (a,b,c));
268 insert into t2 select A.a, B.a, B.a, A.a, 'filler' from t1 A, t1 B;
269 -- disable_query_log
270 -- disable_result_log
271 ANALYZE TABLE t2;
272 -- enable_result_log
273 -- enable_query_log
274 --replace_column 9 #
275 explain select * from t2 force index (d) where d < 10;
276 drop table t2;
277 
278 drop table t1;
279 set @@read_rnd_buffer_size= @read_rnd_buffer_size_save;
280 
281 #
282 # BUG#33033 "MySQL/InnoDB crashes with simple select range query"
283 #
284 create table t1 (f1 int not null, f2 int not null,f3 int not null, f4 char(1), primary key (f1,f2), key ix(f3));
285 
286 --disable_query_log
287 let $1=55;
288 
289 while ($1)
290 {
291  eval insert into t1(f1,f2,f3,f4) values ($1,$1,$1,'A');
292  dec $1;
293 }
294 --enable_query_log
295 
296 # The following must not crash:
297 --sorted_result
298 select * from t1 where (f3>=5 and f3<=10) or (f3>=1 and f3<=4);
299 
300 drop table t1;
301 
302 --echo
303 --echo BUG#37977: Wrong result returned on GROUP BY + OR + Innodb
304 --echo
305 CREATE TABLE t1 (
306  `pk` int(11) NOT NULL AUTO_INCREMENT,
307  `int_nokey` int(11) NOT NULL,
308  `int_key` int(11) NOT NULL,
309  `date_key` date NOT NULL,
310  `date_nokey` date NOT NULL,
311  `time_key` time NOT NULL,
312  `time_nokey` time NOT NULL,
313  `datetime_key` datetime NOT NULL,
314  `datetime_nokey` datetime NOT NULL,
315  `varchar_key` varchar(5) DEFAULT NULL,
316  `varchar_nokey` varchar(5) DEFAULT NULL,
317  PRIMARY KEY (`pk`),
318  KEY `int_key` (`int_key`),
319  KEY `date_key` (`date_key`),
320  KEY `time_key` (`time_key`),
321  KEY `datetime_key` (`datetime_key`),
322  KEY `varchar_key` (`varchar_key`)
323 );
324 
325 INSERT INTO t1 VALUES
326 (1,5,5,'2009-10-16','2009-10-16','09:28:15','09:28:15','2007-09-14 05:34:08','2007-09-14 05:34:08','qk','qk'),
327 (2,6,6,'0000-00-00','0000-00-00','23:06:39','23:06:39','0000-00-00 00:00:00','0000-00-00 00:00:00','j','j'),
328 (3,10,10,'2000-12-18','2000-12-18','22:16:19','22:16:19','2006-11-04 15:42:50','2006-11-04 15:42:50','aew','aew'),
329 (4,0,0,'2001-09-18','2001-09-18','00:00:00','00:00:00','2004-03-23 13:23:35','2004-03-23 13:23:35',NULL,NULL),
330 (5,6,6,'2007-08-16','2007-08-16','22:13:38','22:13:38','2004-08-19 11:01:28','2004-08-19 11:01:28','qu','qu');
331 select pk from t1 WHERE `varchar_key` > 'kr' group by pk;
332 select pk from t1 WHERE `int_nokey` IS NULL OR `varchar_key` > 'kr' group by pk;
333 drop table t1;
334 
335 --echo #
336 --echo # BUG#39447: Error with NOT NULL condition and LIMIT 1
337 --echo #
338 CREATE TABLE t1 (
339  id int(11) NOT NULL,
340  parent_id int(11) DEFAULT NULL,
341  name varchar(10) DEFAULT NULL,
342  PRIMARY KEY (id),
343  KEY ind_parent_id (parent_id)
344 );
345 
346 insert into t1 (id, parent_id, name) values
347 (10,NULL,'A'),
348 (20,10,'B'),
349 (30,10,'C'),
350 (40,NULL,'D'),
351 (50,40,'E'),
352 (60,40,'F'),
353 (70,NULL,'J');
354 
355 -- disable_query_log
356 -- disable_result_log
357 ANALYZE TABLE t1;
358 -- enable_result_log
359 -- enable_query_log
360 
361 SELECT id FROM t1 WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1;
362 --echo This must show type=index, extra=Using where
363 explain SELECT * FROM t1 WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1;
364 SELECT * FROM t1 WHERE parent_id IS NOT NULL ORDER BY id DESC LIMIT 1;
365 drop table t1;
366 
367 --echo #
368 --echo # Bug#50381 "Assertion failing in handler.h:1283:
369 --echo # void COST_VECT::add_io(double, double)"
370 --echo #
371 
372 CREATE TABLE t1 (
373  c1 INT NOT NULL,
374  c2 VARCHAR(1) DEFAULT NULL,
375  PRIMARY KEY (c1)
376 );
377 
378 CREATE TABLE t2 (
379  c1 INT NOT NULL,
380  c2 VARCHAR(1) DEFAULT NULL,
381  PRIMARY KEY (c1)
382 );
383 
384 INSERT INTO t2 VALUES (10,'v');
385 INSERT INTO t2 VALUES (11,'r');
386 
387 SELECT t1.c2
388 FROM t2 STRAIGHT_JOIN t1 ON t1.c1 < t2.c1;
389 
390 DROP TABLE t1, t2;
391 
392 --echo #
393 --echo # Bug#58463: Error Can't find record on SELECT with JOIN and ORDER BY
394 --echo #
395 
396 # To produce the same query plan as in the bug report the first table
397 # must be stored in MyISAM.
398 CREATE TABLE t1 (
399  pk INT NOT NULL,
400  PRIMARY KEY (pk)
401 ) ENGINE=MyISAM;
402 
403 INSERT INTO t1 VALUES (2);
404 
405 CREATE TABLE t2 (
406  pk INT NOT NULL,
407  i1 INT NOT NULL,
408  i2 INT NOT NULL,
409  c1 VARCHAR(1024) CHARACTER SET utf8,
410  PRIMARY KEY (pk),
411  KEY k1 (i1)
412 );
413 
414 INSERT INTO t2 VALUES (3, 9, 1, NULL);
415 
416 -- disable_query_log
417 -- disable_result_log
418 ANALYZE TABLE t1;
419 ANALYZE TABLE t2;
420 -- enable_result_log
421 -- enable_query_log
422 
423 let query=
424 SELECT i1
425 FROM t1 LEFT JOIN t2 ON t1.pk = t2.i2
426 WHERE t2.i1 > 5
427 AND t2.pk IS NULL
428 ORDER BY i1;
429 
430 eval EXPLAIN $query;
431 eval $query;
432 
433 DROP TABLE t1, t2;
434 
435 --echo #
436 --echo # Bug#12321461: CRASH IN DSMRR_IMPL::DSMRR_INIT ON SELECT STRAIGHT_JOIN
437 --echo #
438 
439 # This test should run without join buffering
440 set @save_optimizer_switch = @@optimizer_switch;
441 set optimizer_switch='block_nested_loop=off,batched_key_access=off';
442 
443 CREATE TABLE t1 (
444  pk INTEGER,
445  c1 VARCHAR(1) NOT NULL,
446  PRIMARY KEY (pk)
447 );
448 
449 CREATE TABLE t2 (
450  c1 VARCHAR(1) NOT NULL
451 );
452 
453 INSERT INTO t2 VALUES ('v'), ('c');
454 
455 -- disable_query_log
456 -- disable_result_log
457 ANALYZE TABLE t1;
458 ANALYZE TABLE t2;
459 -- enable_result_log
460 -- enable_query_log
461 
462 let query=
463 SELECT STRAIGHT_JOIN t1.c1
464 FROM t1 RIGHT OUTER JOIN t2 ON t1.c1 = t2.c1
465 WHERE t1.pk > 176;
466 
467 eval EXPLAIN $query;
468 eval $query;
469 
470 DROP TABLE t1,t2;
471 
472 # Restore join buffer settings to their original values
473 set optimizer_switch= @save_optimizer_switch;
474 
475 --echo #
476 --echo # Bug#13249966 MRR: RANDOM ERROR DUE TO UNINITIALIZED RES WITH
477 --echo # SMALL READ_RND_BUFFER_SIZE
478 --echo #
479 
480 set @read_rnd_buffer_size_save= @@read_rnd_buffer_size;
481 set read_rnd_buffer_size=1;
482 select @@read_rnd_buffer_size;
483 
484 CREATE TABLE t1 (
485  i1 INTEGER NOT NULL,
486  i2 INTEGER NOT NULL,
487  KEY (i2)
488 );
489 
490 INSERT INTO t1 VALUES (0,1),(1,2),(2,3);
491 
492 -- disable_query_log
493 -- disable_result_log
494 ANALYZE TABLE t1;
495 -- enable_result_log
496 -- enable_query_log
497 
498 let query=
499 SELECT i1
500 FROM t1
501 WHERE i2 > 2;
502 
503 eval EXPLAIN $query;
504 eval $query;
505 
506 DROP TABLE t1;
507 
508 set @@read_rnd_buffer_size= @read_rnd_buffer_size_save;
509 select @@read_rnd_buffer_size;
510 
511 --echo #
512 --echo # Bug 12365385 STRAIGHT_JOIN QUERY QUICKLY EXHAUSTS SYSTEM+VIRT.
513 --echo # MEMORY LEADING TO SYSTEM CRASH
514 --echo #
515 
516 CREATE TABLE ten (a INTEGER);
517 INSERT INTO ten VALUES (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
518 
519 CREATE TABLE t1 (
520  pk INTEGER NOT NULL,
521  i1 INTEGER NOT NULL,
522  c1 VARCHAR(10) NOT NULL,
523  PRIMARY KEY (pk)
524 );
525 
526 INSERT INTO t1
527  SELECT a, 1, 'MySQL' FROM ten;
528 
529 CREATE TABLE t2 (
530  pk INTEGER NOT NULL,
531  c1 VARCHAR(10) NOT NULL,
532  c2 varchar(10) NOT NULL,
533  PRIMARY KEY (pk)
534 );
535 
536 INSERT INTO t2
537  SELECT a, 'MySQL', 'MySQL' FROM ten;
538 
539 CREATE TABLE t3 (
540  pk INTEGER NOT NULL,
541  c1 VARCHAR(10) NOT NULL,
542  PRIMARY KEY (pk)
543 );
544 
545 INSERT INTO t3
546  SELECT a, 'MySQL' FROM ten;
547 
548 CREATE TABLE t4 (
549  pk int(11) NOT NULL,
550  c1_key varchar(10) CHARACTER SET utf8 NOT NULL,
551  c2 varchar(10) NOT NULL,
552  c3 varchar(10) NOT NULL,
553  PRIMARY KEY (pk),
554  KEY k1 (c1_key)
555 );
556 
557 # t4 is empty
558 
559 CREATE TABLE t5 (
560  pk INTEGER NOT NULL,
561  c1 VARCHAR(10) NOT NULL,
562  PRIMARY KEY (pk)
563 );
564 
565 INSERT INTO t5
566  SELECT a, 'MySQL' FROM ten;
567 
568 -- disable_query_log
569 -- disable_result_log
570 ANALYZE TABLE t1;
571 ANALYZE TABLE t2;
572 ANALYZE TABLE t3;
573 ANALYZE TABLE t4;
574 ANALYZE TABLE t5;
575 -- enable_result_log
576 -- enable_query_log
577 
578 let query=
579 SELECT STRAIGHT_JOIN *
580 FROM
581  (t1 LEFT JOIN
582  (t2 LEFT JOIN
583  (t3 LEFT OUTER JOIN t4 ON t3.c1 <= t4.c1_key)
584  ON t2.c1 = t4.c3)
585  ON t1.c1 = t4.c2)
586  RIGHT OUTER JOIN t5 ON t2.c2 <= t5.c1
587 WHERE t1.i1 = 1;
588 
589 eval EXPLAIN $query;
590 eval $query;
591 
592 DROP TABLE ten, t1, t2, t3, t4, t5;
593