You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
MDEV-36486 Optimizer hints are resolved against the INSERT part of INSERT..SELECT
When processing queries like
INSERT INTO t1 (..) SELECT .. FROM t1, t2 ...,
there is a single query block (i.e., a single SELECT_LEX) for both INSERT and
SELECT parts. During hints resolution, when hints are attached to particular
TABLE_LIST's, the search is performed by table name across the whole
query block.
So, if a table mentioned in an optimizer hint is present in the INSERT part,
the hint is attached to the that table. This is obviously wrong as
optimizer hints are supposed to only affect the SELECT part of
an INSERT..SELECT clause.
This commit disables possible attaching hints to tables in the INSERT part
and fixes some other bugs related to INSERT..SELECT statements processing
Copy file name to clipboardExpand all lines: mysql-test/main/opt_hints.result
+55
Original file line number
Diff line number
Diff line change
@@ -1379,6 +1379,61 @@ SELECT
1379
1379
Warnings:
1380
1380
Warning 1064 Optimizer hint syntax error near '? bad syntax */ 1' at line 2
1381
1381
DROP TABLE t1;
1382
+
1383
+
# MDEV-36486 Optimizer hints are resolved against the INSERT part of INSERT..SELECT
1384
+
1385
+
CREATE TABLE t1 (a INT, KEY(a));
1386
+
INSERT INTO t1 VALUES (1),(2),(3);
1387
+
CREATE TABLE t2 (a INT, KEY(a));
1388
+
INSERT INTO t2 VALUES (1),(2),(3);
1389
+
# See that the range optimization is employed when there are no hints:
1390
+
EXPLAIN EXTENDED
1391
+
INSERT INTO t1 (a) SELECT a FROM t1 WHERE a>1 AND a<=3;
1392
+
id select_type table type possible_keys key key_len ref rows filtered Extra
1393
+
1 SIMPLE t1 range a a 5 NULL 2 100.00 Using where; Using index; Using temporary
1394
+
Warnings:
1395
+
Note 1003 insert into `test`.`t1`(a) select sql_buffer_result `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1 and `test`.`t1`.`a` <= 3
1396
+
# No range optimization any more:
1397
+
EXPLAIN EXTENDED
1398
+
INSERT INTO t1 (a) SELECT /*+ no_range_optimization (t1 a)*/ a FROM t1 WHERE a>1 AND a<=3;
1399
+
id select_type table type possible_keys key key_len ref rows filtered Extra
1400
+
1 SIMPLE t1 index a a 5 NULL 3 100.00 Using where; Using index; Using temporary
1401
+
Warnings:
1402
+
Note 1003 insert into `test`.`t1`(a) select /*+ NO_RANGE_OPTIMIZATION(`t1`@`select#2` `a`) */ sql_buffer_result `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1 and `test`.`t1`.`a` <= 3
1403
+
# Alternatively, a hint may be placed next to INSERT keyword:
1404
+
EXPLAIN EXTENDED
1405
+
INSERT /*+ no_range_optimization (t1)*/ INTO t1 (a) SELECT a FROM t1 WHERE a>1 AND a<=3;
1406
+
id select_type table type possible_keys key key_len ref rows filtered Extra
1407
+
1 SIMPLE t1 index a a 5 NULL 3 100.00 Using where; Using index; Using temporary
1408
+
Warnings:
1409
+
Note 1003 insert into `test`.`t1`(a) select /*+ NO_RANGE_OPTIMIZATION(`t1`@`select#1`) */ sql_buffer_result `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1 and `test`.`t1`.`a` <= 3
1410
+
# But if hints are present at both INSERT and SELECT parts,
1411
+
# those at the INSERT part are ignored:
1412
+
EXPLAIN EXTENDED
1413
+
INSERT /*+ no_range_optimization (t1)*/ INTO t1 (a) SELECT /*+ mrr(t1)*/ a
1414
+
FROM t1 WHERE a>1 AND a<=3;
1415
+
id select_type table type possible_keys key key_len ref rows filtered Extra
1416
+
1 SIMPLE t1 range a a 5 NULL 2 100.00 Using where; Using index; Using temporary
1417
+
Warnings:
1418
+
Note 1003 insert into `test`.`t1`(a) select /*+ MRR(`t1`@`select#2`) */ sql_buffer_result `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1 and `test`.`t1`.`a` <= 3
1419
+
# Table `t2` cannot be resolved since it is not present in the SELECT part
1420
+
# (a warning expected):
1421
+
EXPLAIN EXTENDED
1422
+
INSERT INTO t2 (a) SELECT /*+ no_range_optimization (t2)*/ a FROM t1 WHERE a>1 AND a<=3;
1423
+
id select_type table type possible_keys key key_len ref rows filtered Extra
1424
+
1 SIMPLE t1 range a a 5 NULL 2 100.00 Using where; Using index
1425
+
Warnings:
1426
+
Warning 4212 Unresolved table name `t2`@`select#2` for NO_RANGE_OPTIMIZATION hint
1427
+
Note 1003 insert into `test`.`t2`(a) select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1 and `test`.`t1`.`a` <= 3
1428
+
# Alternative placement of the hint:
1429
+
EXPLAIN EXTENDED
1430
+
INSERT /*+ no_range_optimization (t2 ix1)*/ INTO t2 (a) SELECT a FROM t1 WHERE a>1 AND a<=3;
1431
+
id select_type table type possible_keys key key_len ref rows filtered Extra
1432
+
1 SIMPLE t1 range a a 5 NULL 2 100.00 Using where; Using index
1433
+
Warnings:
1434
+
Warning 4213 Unresolved index name `t2`@`select#1` `ix1` for NO_RANGE_OPTIMIZATION hint
1435
+
Note 1003 insert into `test`.`t2`(a) select `test`.`t1`.`a` AS `a` from `test`.`t1` where `test`.`t1`.`a` > 1 and `test`.`t1`.`a` <= 3
0 commit comments