From fe1f4ca8ced02aad38fa3e71da67b2a172787bda Mon Sep 17 00:00:00 2001 From: Monty Date: Fri, 27 Jan 2023 19:36:02 +0200 Subject: [PATCH] MDEV-30486 Table is not eliminated in bb-11.0 Some tables where not eliminated when they could have been. This was caused because HA_KEYREAD_ONLY is not set anymore for InnoDB clustered index and the elimination code was depending on field->part_of_key_not_clustered which was not set if HA_KEYREAD_ONLY is not present. Fixed by moving out field->part_of_key and field->part_of_key_not_clustered from under HA_KEYREAD_ONLY (which they should never have been part of). Other things: - Fixed a bug in make_join_select() that caused range to be used when there where elminiated or constant tables present (Caused wrong change of plans in join_outer_innodb.test). This also affected show_explain.test and subselct_sj_mat.test where wrong 'range's where replaced with index scans. Reviewer: Sergei Petrunia --- mysql-test/main/join_outer_innodb.result | 2 -- mysql-test/main/range_aria_dbt3.result | 10 ++++++++++ mysql-test/main/range_aria_dbt3.test | 6 ++++++ mysql-test/main/show_explain.result | 4 ++-- mysql-test/main/subselect_sj_mat.result | 4 ++-- sql/sql_select.cc | 3 ++- sql/table.cc | 12 +++++++++--- 7 files changed, 31 insertions(+), 10 deletions(-) diff --git a/mysql-test/main/join_outer_innodb.result b/mysql-test/main/join_outer_innodb.result index 208113b4870..dbbbe89944b 100644 --- a/mysql-test/main/join_outer_innodb.result +++ b/mysql-test/main/join_outer_innodb.result @@ -442,7 +442,6 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t4 eq_ref PRIMARY PRIMARY 4 test.t1.a2 1 1 SIMPLE t5 eq_ref PRIMARY PRIMARY 4 test.t4.d1 1 Using where 1 SIMPLE t6 eq_ref PRIMARY PRIMARY 4 test.t1.a3 1 Using where -1 SIMPLE e2 eq_ref PRIMARY PRIMARY 4 test.t6.f1 1 Using where 1 SIMPLE t7 eq_ref PRIMARY PRIMARY 1 test.t1.a7 1 1 SIMPLE t11 eq_ref PRIMARY PRIMARY 4 test.t1.a5 1 1 SIMPLE t12 eq_ref PRIMARY PRIMARY 4 test.t11.k3 1 Using where @@ -465,7 +464,6 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t4 eq_ref PRIMARY PRIMARY 4 test.t1.a2 1 1 SIMPLE t5 eq_ref PRIMARY PRIMARY 4 test.t4.d1 1 Using where 1 SIMPLE t6 eq_ref PRIMARY PRIMARY 4 test.t1.a3 1 Using where -1 SIMPLE e2 eq_ref PRIMARY PRIMARY 4 test.t6.f1 1 Using where 1 SIMPLE t7 eq_ref PRIMARY PRIMARY 1 test.t1.a7 1 1 SIMPLE t11 eq_ref PRIMARY PRIMARY 4 test.t1.a5 1 1 SIMPLE t12 eq_ref PRIMARY PRIMARY 4 test.t11.k3 1 Using where diff --git a/mysql-test/main/range_aria_dbt3.result b/mysql-test/main/range_aria_dbt3.result index f08a1b244f8..be3bc79c3a8 100644 --- a/mysql-test/main/range_aria_dbt3.result +++ b/mysql-test/main/range_aria_dbt3.result @@ -19,6 +19,16 @@ id select_type table type possible_keys key key_len ref rows Extra SELECT COUNT(*) FROM lineitem WHERE l_orderkey BETWEEN 111 AND 262 OR ( l_orderkey BETWEEN 152 AND 672 AND l_linenumber BETWEEN 4 AND 9 ); COUNT(*) 293 +# +# MDEV-30486 Table is not eliminated in bb-11.0 +# +explain SELECT c_custkey, c_name AS currency2 FROM partsupp LEFT JOIN part ON ( p_partkey = ps_partkey ) JOIN supplier ON (s_suppkey = ps_suppkey) JOIN lineitem ON ( ps_suppkey = l_suppkey ) JOIN orders ON ( l_orderkey = o_orderkey ) JOIN customer ON ( o_custkey = c_custkey ) HAVING c_custkey > 150; +id select_type table type possible_keys key key_len ref rows Extra +1 SIMPLE supplier index PRIMARY PRIMARY 4 NULL 10 Using index +1 SIMPLE partsupp ref i_ps_suppkey i_ps_suppkey 4 dbt3_s001.supplier.s_suppkey 16 +1 SIMPLE lineitem ref PRIMARY,i_l_suppkey,i_l_orderkey,i_l_orderkey_quantity i_l_suppkey 5 dbt3_s001.supplier.s_suppkey 100 +1 SIMPLE orders eq_ref PRIMARY,i_o_custkey PRIMARY 4 dbt3_s001.lineitem.l_orderkey 1 Using where +1 SIMPLE customer eq_ref PRIMARY PRIMARY 4 dbt3_s001.orders.o_custkey 1 DROP DATABASE dbt3_s001; # # End of 10.5 tests diff --git a/mysql-test/main/range_aria_dbt3.test b/mysql-test/main/range_aria_dbt3.test index 141bf43885b..b373d5b999b 100644 --- a/mysql-test/main/range_aria_dbt3.test +++ b/mysql-test/main/range_aria_dbt3.test @@ -26,6 +26,12 @@ explain SELECT COUNT(*) FROM lineitem WHERE l_orderkey BETWEEN 111 AND 262 OR ( SELECT COUNT(*) FROM lineitem WHERE l_orderkey BETWEEN 111 AND 262 OR ( l_orderkey BETWEEN 152 AND 672 AND l_linenumber BETWEEN 4 AND 9 ); +--echo # +--echo # MDEV-30486 Table is not eliminated in bb-11.0 +--echo # + +explain SELECT c_custkey, c_name AS currency2 FROM partsupp LEFT JOIN part ON ( p_partkey = ps_partkey ) JOIN supplier ON (s_suppkey = ps_suppkey) JOIN lineitem ON ( ps_suppkey = l_suppkey ) JOIN orders ON ( l_orderkey = o_orderkey ) JOIN customer ON ( o_custkey = c_custkey ) HAVING c_custkey > 150; + DROP DATABASE dbt3_s001; --echo # diff --git a/mysql-test/main/show_explain.result b/mysql-test/main/show_explain.result index 1888052647b..20333c925d4 100644 --- a/mysql-test/main/show_explain.result +++ b/mysql-test/main/show_explain.result @@ -1264,7 +1264,7 @@ explain SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t2 system NULL NULL NULL NULL 1 -1 SIMPLE t1 range b b 6 NULL 107 Using where; Using index +1 SIMPLE t1 index b b 6 NULL 107 Using where; Using index 1 SIMPLE t3 ref PRIMARY PRIMARY 5 test.t1.b 1 Using index set @show_explain_probe_select_id=1; SET debug_dbug='+d,show_explain_probe_do_select'; @@ -1273,7 +1273,7 @@ connection default; show explain for $thr2; id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t2 system NULL NULL NULL NULL 1 -1 SIMPLE t1 range b b 6 NULL 107 Using where; Using index +1 SIMPLE t1 index b b 6 NULL 107 Using where; Using index 1 SIMPLE t3 ref PRIMARY PRIMARY 5 test.t1.b 1 Using index Warnings: Note 1003 SELECT b AS field1, b AS field2 FROM t1, t2, t3 WHERE d = b ORDER BY field1, field2 diff --git a/mysql-test/main/subselect_sj_mat.result b/mysql-test/main/subselect_sj_mat.result index 6d1366e532b..7bec603abef 100644 --- a/mysql-test/main/subselect_sj_mat.result +++ b/mysql-test/main/subselect_sj_mat.result @@ -2023,7 +2023,7 @@ WHERE (a, c) IN (SELECT s1.b, s1.c FROM t2 AS s1, t2 AS s2 WHERE s2.d = s1.e AND s1.e = (SELECT MAX(e) FROM t2)); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 system NULL NULL NULL NULL 1 -1 PRIMARY t2 range c c 5 NULL 8 Using where; Using index +1 PRIMARY t2 index c c 5 NULL 8 Using where; Using index 1 PRIMARY s1 ref c c 5 test.t2.c 1 Using where 1 PRIMARY s2 ref d d 4 const 2 Using where; Using index; FirstMatch(t2) 3 SUBQUERY t2 ALL NULL NULL NULL NULL 8 @@ -2042,7 +2042,7 @@ WHERE (a, c) IN (SELECT s1.b, s1.c FROM t2 AS s1, t2 AS s2 WHERE s2.d = s1.e AND s1.e = (SELECT MAX(e) FROM t2)); id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 system NULL NULL NULL NULL 1 -1 PRIMARY t2 range c c 5 NULL 8 Using where; Using index +1 PRIMARY t2 index c c 5 NULL 8 Using where; Using index 1 PRIMARY s1 hash_ALL c #hash#c 5 test.t2.c 8 Using where; Using join buffer (flat, BNLH join) 1 PRIMARY s2 hash_range d #hash#d:d 4:4 const 2 Using where; Using index; FirstMatch(t2); Using join buffer (incremental, BNLH join) 3 SUBQUERY t2 ALL NULL NULL NULL NULL 8 diff --git a/sql/sql_select.cc b/sql/sql_select.cc index b68e7931a81..b1f28b9af51 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -13390,7 +13390,8 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond) if (!tab->table->is_filled_at_execution() && !tab->loosescan_match_tab && // (1) - ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) || + ((cond && (!tab->keys.is_subset(tab->const_keys) && + i > join->const_tables)) || (!tab->const_keys.is_clear_all() && i == join->const_tables && join->unit->lim.get_select_limit() < join->best_positions[i].records_read && diff --git a/sql/table.cc b/sql/table.cc index df4d88a1527..e7b1e459c37 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -2046,7 +2046,8 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, next_chunk+= str_db_type_length + 2; } - share->set_use_ext_keys_flag(plugin_hton(se_plugin)->flags & HTON_SUPPORTS_EXTENDED_KEYS); + share->set_use_ext_keys_flag(plugin_hton(se_plugin)->flags & + HTON_SUPPORTS_EXTENDED_KEYS); if (create_key_infos(disk_buff + 6, frm_image_end, keys, keyinfo, new_frm_ver, &ext_key_parts, @@ -3083,12 +3084,17 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, if (handler_file->index_flags(key, i, 0) & HA_KEYREAD_ONLY) { share->keys_for_keyread.set_bit(key); + /* + part_of_key is used to check if we can use the field + as part of covering key (which implies HA_KEYREAD_ONLY). + */ field->part_of_key.set_bit(key); - if (i < keyinfo->user_defined_key_parts) - field->part_of_key_not_clustered.set_bit(key); } if (handler_file->index_flags(key, i, 1) & HA_READ_ORDER) field->part_of_sortkey.set_bit(key); + + if (i < keyinfo->user_defined_key_parts) + field->part_of_key_not_clustered.set_bit(key); } if (!(key_part->key_part_flag & HA_REVERSE_SORT) && usable_parts == i)