From 04ddd79b79ad0d6e9b585f56c379b5fe0aee41b4 Mon Sep 17 00:00:00 2001 From: Manuel Ung Date: Wed, 2 Nov 2022 02:43:45 -0700 Subject: [PATCH] Fix inefficient range tree construction from IN list Summary: Fixes https://bugs.mysql.com/108963 When constructing the range tree, we end up re-merging the current tree for every element of the IN list, and this causes optimization to be very slow. Fix this by switching the order of arguments, so that we always merge the new item into the existing tree instead. I've also considered fixing `key_or` to automatically swap the order of arguments, but there is already existing swapping logic that considers which tree needs copied vs can be edited in place, and it seems unclear how to integrate with that effectively (since the two goals could be conflicting). Test Plan: tested manually using the query affecting user before: ``` +----+-------------+------------------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | nee_violation_obj_chg_events | NULL | ALL | unique_event | NULL | NULL | NULL | 1 | 100.00 | Using where | +----+-------------+------------------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ 1 row in set, 40970 warnings (6 min 24.58 sec) ``` after ``` +----+-------------+------------------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+------------------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ | 1 | SIMPLE | nee_violation_obj_chg_events | NULL | ALL | unique_event | NULL | NULL | NULL | 1 | 100.00 | Using where | +----+-------------+------------------------------+------------+------+---------------+------+---------+------+------+----------+-------------+ 1 row in set, 40970 warnings (0.34 sec) ``` Reviewers: herman, yoshinori, #mysql_eng Reviewed By: herman Subscribers: pgl Differential Revision: https://phabricator.intern.facebook.com/D40948362 Tasks: T135027152 --- sql/range_optimizer/range_analysis.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/range_optimizer/range_analysis.cc b/sql/range_optimizer/range_analysis.cc index b62fcc499356..c6f83d50ee00 100644 --- a/sql/range_optimizer/range_analysis.cc +++ b/sql/range_optimizer/range_analysis.cc @@ -421,7 +421,7 @@ static SEL_TREE *get_func_mm_tree_from_in_predicate( */ if (and_tree == nullptr) return nullptr; } - or_tree = tree_or(param, remove_jump_scans, and_tree, or_tree); + or_tree = tree_or(param, remove_jump_scans, or_tree, and_tree); } return or_tree; }