Bug #117770 rechecking_index_usage causes the materialized table to be generated using table scan in Materialize semijoin strategy
| Submitted: | 22 Mar 2025 5:32 | Modified: | 24 Mar 2025 7:53 |
|---|---|---|---|
| Reporter: | Jingqi Tian (OCA) | Email Updates: | |
| Status: | Verified | Impact on me: | |
| Category: | MySQL Server: Optimizer | Severity: | S5 (Performance) |
| Version: | 8.0.36, 8.0.41, 8.4.4, 9.2.0 | OS: | Any |
| Assigned to: | CPU Architecture: | Any |
[24 Mar 2025 7:53]
MySQL Verification Team
Hello Jingqi Tian, Thank you for the report and test case. regards, Umesh
[23 Nov 22:49]
Eyal Reuveni
Another case of this bug on 8.4.11. Here the range on the materialized table does not come from the subquery itself: it is copied in from the outer query through the semijoin equality. In production, the optimizer picked this plan by itself, without any hint.
The query has this shape:
SELECT p.id FROM p
WHERE EXISTS (SELECT 1 FROM l JOIN ch ON ch.cid = l.cid
WHERE l.pid = p.id AND ch.grp <> 7)
AND p.id IN (11, 22);
Because l.pid = p.id and p.id IN (11, 22), the range optimizer gives l a range on pid IN (11, 22), estimated at a few rows. The materialization nest is costed with that range. Then make_join_query_block rechecks l with recheck_reason not_first_table. Inside the nest, p is not in l's prefix, so l's condition becomes TRUE (range_scan_possible: false, cause: condition_always_true). The access drops to JT_ALL, and the cost is not recomputed.
Production (about 956M rows in l), EXPLAIN FORMAT=JSON for l inside materialized_from_subquery:
access_type ALL, rows_examined_per_scan 956511837, rows_produced_per_join 4,
filtered 0.00, read_cost 1.40, attached_condition "true"
The whole query was costed at 7.72, so MaterializeLookup beat FirstMatch. Real executions ran for minutes and were killed by max_execution_time.
Repro on 8.4.11. The default plan here is FirstMatch, so a hint forces the strategy:
CREATE TABLE p (id INT PRIMARY KEY);
CREATE TABLE l (pk INT AUTO_INCREMENT PRIMARY KEY, pid INT NOT NULL, cid INT NOT NULL,
UNIQUE KEY pid_cid (pid, cid), KEY cid (cid));
CREATE TABLE ch (pk INT AUTO_INCREMENT PRIMARY KEY, cid INT NOT NULL, grp INT NOT NULL,
UNIQUE KEY cid (cid), KEY grp_cid (grp, cid));
SET SESSION cte_max_recursion_depth = 1000000;
INSERT INTO p WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM s WHERE n < 10000) SELECT n FROM s;
INSERT INTO ch (cid, grp) WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM s WHERE n < 200000) SELECT n, n % 500 FROM s;
INSERT IGNORE INTO l (pid, cid) WITH RECURSIVE s(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM s WHERE n < 300000)
SELECT 1 + (n % 10000), 1 + ((n * 7919) % 200000) FROM s;
ANALYZE TABLE p, l, ch;
EXPLAIN ANALYZE SELECT /*+ SEMIJOIN(@sq MATERIALIZATION) */ p.id FROM p
WHERE EXISTS (SELECT /*+ QB_NAME(sq) */ 1 FROM l JOIN ch ON ch.cid = l.cid
WHERE l.pid = p.id AND ch.grp <> 7)
AND p.id IN (11, 22);
-> Materialize with deduplication (cost=24.3..24.3 rows=20.6) (actual ... rows=9980 loops=1)
-> Table scan on l (cost=8.27 rows=40) (actual ... rows=200000 loops=1)
So it is estimated at 40 rows and actually reads 200,000. Workarounds: NO_SEMIJOIN on the subquery, or rewriting it as a JOIN. Adding LIMIT 1 to the EXISTS does not prevent the semijoin.
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.053 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: rechecking_index_usage causes the materialized table to be generated using table scan in Materialize semijoin strategy. How to repeat: 1. Create table CREATE TABLE t1 ( id int PRIMARY KEY, col1 int NOT NULL ); CREATE TABLE t2 ( id int PRIMARY KEY, col1 int NOT NULL, col2 int, KEY index_col1 (col1), KEY index_col2 (col2) ); 2. Create procedure DELIMITER // CREATE PROCEDURE insert_data () BEGIN DECLARE num int; SET num = 1; WHILE num <= 1000 DO INSERT INTO t1 VALUES (num, num); INSERT INTO t2 VALUES (num, num, num); SET num = num + 1; END WHILE; END;// DELIMITER ; 3. Call procedure to insert data call insert_data(); 4. Execute query EXPLAIN SELECT count(*) FROM t1 WHERE t1.col1 not in (SELECT col1 FROM t2 force index(index_col2) WHERE t2.col2 in (1,2)); +----+--------------+-------------+------------+--------+---------------------+---------------------+---------+-------------+------+----------+-------------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+--------------+-------------+------------+--------+---------------------+---------------------+---------+-------------+------+----------+-------------------------+ | 1 | SIMPLE | t1 | NULL | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | NULL | | 1 | SIMPLE | <subquery2> | NULL | eq_ref | <auto_distinct_key> | <auto_distinct_key> | 5 | bug.t1.col1 | 1 | 100.00 | Using where; Not exists | | 2 | MATERIALIZED | t2 | NULL | ALL | index_col2 | NULL | NULL | NULL | 1000 | 100.00 | Using where | +----+--------------+-------------+------------+--------+---------------------+---------------------+---------+-------------+------+----------+-------------------------+ It would be better to use range scan on index_col2 of table t to materialize table <subquery2>, but table scan was used instead. If we check optimizer_trace, we will see that the access type of t2 is changed from range to table_scan due to rechecking_index_usage. "rechecking_index_usage": { "recheck_reason": "not_first_table", "range_analysis": { "table_scan": { "rows": 1000, "cost": 1.79769e+308 }, "potential_range_indexes": [ { "index": "PRIMARY", "usable": false, "cause": "not_applicable" }, { "index": "index_col1", "usable": false, "cause": "not_applicable" }, { "index": "index_col2", "usable": true, "key_parts": [ "col2", "id" ] } ], "setup_range_conditions": [ ], "range_scan_possible": false, "cause": "condition_always_true", "group_index_range": { "chosen": false, "cause": "not_single_table" }, "skip_scan_range": { "chosen": false, "cause": "not_single_table" } } } And the reason is 'condition_always_true'. Suggested fix: The rechecking_index_usage should not be applied to the base table used to generate the materialized table.