@@ -2506,5 +2506,90 @@ DROP TABLE t1,t2;
2506
2506
#
2507
2507
# end of 5.3 tests
2508
2508
#
2509
+ #
2510
+ # Bug mdev-11161: The second execution of prepared statement
2511
+ # does not use generated key for materialized
2512
+ # derived table / view
2513
+ # (actually this is a 5.3 bug.)
2514
+ #
2515
+ create table t1 (
2516
+ mat_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
2517
+ matintnum CHAR(6) NOT NULL,
2518
+ test MEDIUMINT UNSIGNED NULL
2519
+ );
2520
+ create table t2 (
2521
+ mat_id MEDIUMINT UNSIGNED NOT NULL,
2522
+ pla_id MEDIUMINT UNSIGNED NOT NULL
2523
+ );
2524
+ insert into t1 values
2525
+ (NULL, 'a', 1), (NULL, 'b', 2), (NULL, 'c', 3), (NULL, 'd', 4),
2526
+ (NULL, 'e', 5), (NULL, 'f', 6), (NULL, 'g', 7), (NULL, 'h', 8),
2527
+ (NULL, 'i', 9);
2528
+ insert into t2 values
2529
+ (1, 100), (1, 101), (1, 102), (2, 100), (2, 103), (2, 104),
2530
+ (3, 101), (3, 102), (3, 105);
2531
+ explain
2532
+ SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
2533
+ FROM t1 m2
2534
+ INNER JOIN
2535
+ (SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
2536
+ FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
2537
+ GROUP BY mp.pla_id) d
2538
+ ON d.matintnum=m2.matintnum;
2539
+ id select_type table type possible_keys key key_len ref rows Extra
2540
+ 1 PRIMARY m2 ALL NULL NULL NULL NULL 9
2541
+ 1 PRIMARY <derived2> ref key0 key0 7 test.m2.matintnum 2
2542
+ 2 DERIVED mp ALL NULL NULL NULL NULL 9 Using temporary; Using filesort
2543
+ 2 DERIVED m1 eq_ref PRIMARY PRIMARY 3 test.mp.mat_id 1
2544
+ prepare stmt1 from
2545
+ "SELECT STRAIGHT_JOIN d.pla_id, m2.mat_id
2546
+ FROM t1 m2
2547
+ INNER JOIN
2548
+ (SELECT mp.pla_id, MIN(m1.matintnum) AS matintnum
2549
+ FROM t2 mp INNER JOIN t1 m1 ON mp.mat_id=m1.mat_id
2550
+ GROUP BY mp.pla_id) d
2551
+ ON d.matintnum=m2.matintnum";
2552
+ flush status;
2553
+ execute stmt1;
2554
+ pla_id mat_id
2555
+ 102 1
2556
+ 101 1
2557
+ 100 1
2558
+ 104 2
2559
+ 103 2
2560
+ 105 3
2561
+ show status like '%Handler_read%';
2562
+ Variable_name Value
2563
+ Handler_read_first 0
2564
+ Handler_read_key 21
2565
+ Handler_read_last 0
2566
+ Handler_read_next 6
2567
+ Handler_read_prev 0
2568
+ Handler_read_retry 0
2569
+ Handler_read_rnd 6
2570
+ Handler_read_rnd_deleted 0
2571
+ Handler_read_rnd_next 27
2572
+ flush status;
2573
+ execute stmt1;
2574
+ pla_id mat_id
2575
+ 102 1
2576
+ 101 1
2577
+ 100 1
2578
+ 104 2
2579
+ 103 2
2580
+ 105 3
2581
+ show status like '%Handler_read%';
2582
+ Variable_name Value
2583
+ Handler_read_first 0
2584
+ Handler_read_key 21
2585
+ Handler_read_last 0
2586
+ Handler_read_next 6
2587
+ Handler_read_prev 0
2588
+ Handler_read_retry 0
2589
+ Handler_read_rnd 6
2590
+ Handler_read_rnd_deleted 0
2591
+ Handler_read_rnd_next 27
2592
+ deallocate prepare stmt1;
2593
+ drop table t1,t2;
2509
2594
set optimizer_switch=@exit_optimizer_switch;
2510
2595
set join_cache_level=@exit_join_cache_level;
0 commit comments