From b3074128a6f2b5335ae7eff88a95374ba74911c7 Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Thu, 8 Jun 2023 11:13:10 +0300 Subject: [PATCH 1/6] MDEV-31380 post-fix: fix group_min_max.test with embedded and view-protocol embedded doesn't have optimizer trace, view-protocol doesn't work with long column names. --- mysql-test/main/group_min_max.result | 24 ------------ mysql-test/main/group_min_max.test | 24 ------------ .../main/group_min_max_notembedded.result | 26 +++++++++++++ .../main/group_min_max_notembedded.test | 37 +++++++++++++++++++ 4 files changed, 63 insertions(+), 48 deletions(-) create mode 100644 mysql-test/main/group_min_max_notembedded.result create mode 100644 mysql-test/main/group_min_max_notembedded.test diff --git a/mysql-test/main/group_min_max.result b/mysql-test/main/group_min_max.result index 0df9abc7a25..712466c8afb 100644 --- a/mysql-test/main/group_min_max.result +++ b/mysql-test/main/group_min_max.result @@ -4204,30 +4204,6 @@ a b s1 2 2 t2:t2a-null;min_t3_b:t3b-null 3 3 t2:1;min_t3_b:3 drop table t1,t2,t3; -# -# MDEV-31380: Assertion `s->table->opt_range_condition_rows <= s->found_records' failed -# (assertion in 10.6+, DBL_MAX costs in 10.5) -# -CREATE TABLE t1 (a INT, b INT, PRIMARY KEY(a), KEY(b)) ENGINE=InnoDB; -INSERT INTO t1 SELECT seq, seq FROM seq_1_to_100; -SET -@tmp=@@optimizer_use_condition_selectivity, -optimizer_use_condition_selectivity = 1, -@tmp2=@@optimizer_trace, -optimizer_trace=1; -SELECT DISTINCT * FROM t1 WHERE a IN (1, 2); -a b -1 1 -2 2 -select -CAST(json_value(json_extract(trace, '$**.chosen_access_method.cost'), '$[0]') -as DOUBLE) < 1.0e100 -from information_schema.optimizer_trace; -CAST(json_value(json_extract(trace, '$**.chosen_access_method.cost'), '$[0]') -as DOUBLE) < 1.0e100 -1 -set optimizer_use_condition_selectivity = @tmp, optimizer_trace=@tmp2; -drop table t1; # # End of 10.5 tests # diff --git a/mysql-test/main/group_min_max.test b/mysql-test/main/group_min_max.test index 482235571db..726b3e013e0 100644 --- a/mysql-test/main/group_min_max.test +++ b/mysql-test/main/group_min_max.test @@ -1859,30 +1859,6 @@ from t1; drop table t1,t2,t3; ---echo # ---echo # MDEV-31380: Assertion `s->table->opt_range_condition_rows <= s->found_records' failed ---echo # (assertion in 10.6+, DBL_MAX costs in 10.5) ---echo # - -CREATE TABLE t1 (a INT, b INT, PRIMARY KEY(a), KEY(b)) ENGINE=InnoDB; -INSERT INTO t1 SELECT seq, seq FROM seq_1_to_100; - -SET - @tmp=@@optimizer_use_condition_selectivity, - optimizer_use_condition_selectivity = 1, - @tmp2=@@optimizer_trace, - optimizer_trace=1; - -SELECT DISTINCT * FROM t1 WHERE a IN (1, 2); - -select - CAST(json_value(json_extract(trace, '$**.chosen_access_method.cost'), '$[0]') - as DOUBLE) < 1.0e100 -from information_schema.optimizer_trace; - -set optimizer_use_condition_selectivity = @tmp, optimizer_trace=@tmp2; -drop table t1; - --echo # --echo # End of 10.5 tests --echo # diff --git a/mysql-test/main/group_min_max_notembedded.result b/mysql-test/main/group_min_max_notembedded.result new file mode 100644 index 00000000000..e19560b4923 --- /dev/null +++ b/mysql-test/main/group_min_max_notembedded.result @@ -0,0 +1,26 @@ +# +# MDEV-31380: Assertion `s->table->opt_range_condition_rows <= s->found_records' failed +# (assertion in 10.6+, DBL_MAX costs in 10.5) +# +CREATE TABLE t1 (a INT, b INT, PRIMARY KEY(a), KEY(b)) ENGINE=InnoDB; +INSERT INTO t1 SELECT seq, seq FROM seq_1_to_100; +SET +@tmp=@@optimizer_use_condition_selectivity, +optimizer_use_condition_selectivity = 1, +@tmp2=@@optimizer_trace, +optimizer_trace=1; +SELECT DISTINCT * FROM t1 WHERE a IN (1, 2); +a b +1 1 +2 2 +select +CAST(json_value(json_extract(trace, '$**.chosen_access_method.cost'), '$[0]') +as DOUBLE) < 1.0e100 as ACCESS_METHOD_COST_IS_FINITE +from information_schema.optimizer_trace; +ACCESS_METHOD_COST_IS_FINITE +1 +set optimizer_use_condition_selectivity = @tmp, optimizer_trace=@tmp2; +drop table t1; +# +# End of 10.5 tests +# diff --git a/mysql-test/main/group_min_max_notembedded.test b/mysql-test/main/group_min_max_notembedded.test new file mode 100644 index 00000000000..930ee8464f8 --- /dev/null +++ b/mysql-test/main/group_min_max_notembedded.test @@ -0,0 +1,37 @@ +# +# Tests for group-min-max optimization that require optimizer trace +# and so need not-embedded. +# +--source include/default_optimizer_switch.inc +--source include/have_innodb.inc +--source include/have_sequence.inc +--source include/not_embedded.inc + +--echo # +--echo # MDEV-31380: Assertion `s->table->opt_range_condition_rows <= s->found_records' failed +--echo # (assertion in 10.6+, DBL_MAX costs in 10.5) +--echo # + +CREATE TABLE t1 (a INT, b INT, PRIMARY KEY(a), KEY(b)) ENGINE=InnoDB; +INSERT INTO t1 SELECT seq, seq FROM seq_1_to_100; + +SET + @tmp=@@optimizer_use_condition_selectivity, + optimizer_use_condition_selectivity = 1, + @tmp2=@@optimizer_trace, + optimizer_trace=1; + +SELECT DISTINCT * FROM t1 WHERE a IN (1, 2); + +select + CAST(json_value(json_extract(trace, '$**.chosen_access_method.cost'), '$[0]') + as DOUBLE) < 1.0e100 as ACCESS_METHOD_COST_IS_FINITE +from information_schema.optimizer_trace; + +set optimizer_use_condition_selectivity = @tmp, optimizer_trace=@tmp2; +drop table t1; + +--echo # +--echo # End of 10.5 tests +--echo # + From 841e905f200be403d10bbaffad0698f0baef4b90 Mon Sep 17 00:00:00 2001 From: Thirunarayanan Balathandayuthapani Date: Fri, 9 Jun 2023 17:56:04 +0530 Subject: [PATCH 2/6] MDEV-31442 page_cleaner thread aborts while releasing the tablespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After further I/O on a tablespace has been stopped (for example due to DROP TABLE or an operation that rebuilds a table), page cleaner thread tries to flush the pending writes for the tablespace and releases the tablespace reference even though it was not acquired. fil_space_t::flush(): Don't release the tablespace when it is being stopped and closed Thanks to Marko Mäkelä for suggesting this patch. --- storage/innobase/include/fil0fil.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/storage/innobase/include/fil0fil.h b/storage/innobase/include/fil0fil.h index 10365d167b7..053fe7a68e0 100644 --- a/storage/innobase/include/fil0fil.h +++ b/storage/innobase/include/fil0fil.h @@ -1526,9 +1526,11 @@ template inline void fil_space_t::flush() flush_low(); else { - if (!(acquire_low() & (STOPPING | CLOSING))) + if (!(acquire_low(STOPPING & CLOSING) & (STOPPING | CLOSING))) + { flush_low(); - release(); + release(); + } } } From 0e2e70c4c18c064c90fc3341f1b4c99646c689df Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Wed, 14 Jun 2023 13:55:35 +0300 Subject: [PATCH 3/6] MDEV-31479: Inconsistency between MRR and SQL layer costs can cause poor query plan (Same as TODO-3938: best_access_path shows negative costs for mrr=on) best_access_path() assumes that quick select cost includes (quick->rows/TIME_FOR_COMPARE) as a cost of checking the attached part of the WHERE condition. It calls adjust_quick_cost() to subtract addition from quick's cost. The problem was that DS-MRR cost formula didn't include this cost. For very large tables, adjust_quick_cost() would produce a negative cost which would cause assert in debug build or bad query plan choice in release builds. Approved-by: Monty --- sql/multi_range_read.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql/multi_range_read.cc b/sql/multi_range_read.cc index c7c3079f28f..54a0340638c 100644 --- a/sql/multi_range_read.cc +++ b/sql/multi_range_read.cc @@ -2006,6 +2006,9 @@ bool DsMrr_impl::get_disk_sweep_mrr_cost(uint keynr, ha_rows rows, uint flags, /* Total cost of all index accesses */ index_read_cost= primary_file->keyread_time(keynr, 1, rows); cost->add_io(index_read_cost, 1 /* Random seeks */); + + cost->cpu_cost+= (rows2double(rows) / TIME_FOR_COMPARE + + MULTI_RANGE_READ_SETUP_COST); return FALSE; } From f7e9ac0d889d5a68ab28d8acda219d052d16634b Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Mon, 12 Jun 2023 15:58:55 +0300 Subject: [PATCH 4/6] MDEV-31449: Assertion s->table->opt_range_condition_rows <= s->found_records Fix a typo in make_join_statistics(): when updating statistics for derived table, set s->table->... not "table->..." --- mysql-test/main/join.result | 15 +++++++++++++++ mysql-test/main/join.test | 19 +++++++++++++++++++ sql/sql_select.cc | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/mysql-test/main/join.result b/mysql-test/main/join.result index 859bc588ed4..e4b92b4234b 100644 --- a/mysql-test/main/join.result +++ b/mysql-test/main/join.result @@ -3424,3 +3424,18 @@ COUNT(*) 2 DROP TABLE t1, t2, t3; # End of 10.5 tests +# +# MDEV-31449: Assertion s->table->opt_range_condition_rows <= s->found_records +# +CREATE TABLE t1 (a INT, b INT); +INSERT INTO t1 VALUES (1,2),(3,4); +CREATE TABLE t2 (c INT); +INSERT INTO t2 VALUES (5),(6); +SET @tmp=@@OPTIMIZER_USE_CONDITION_SELECTIVITY, OPTIMIZER_USE_CONDITION_SELECTIVITY = 1; +SELECT * FROM +(SELECT t1.* FROM t1 WHERE t1.a IN (SELECT MAX(t2.c) FROM t2 JOIN t1)) AS sq1, +(SELECT t2.* FROM t2 JOIN t1 ON (t1.b IN (SELECT t1.b FROM t2 STRAIGHT_JOIN t1))) AS sq2; +a b c +SET OPTIMIZER_USE_CONDITION_SELECTIVITY=@tmp; +DROP TABLE t1,t2; +# End of 10.6 tests diff --git a/mysql-test/main/join.test b/mysql-test/main/join.test index c8bd2886b30..b665f034813 100644 --- a/mysql-test/main/join.test +++ b/mysql-test/main/join.test @@ -1835,3 +1835,22 @@ SELECT COUNT(*) FROM t1 LEFT JOIN (t2 LEFT JOIN t3 ON t2.b = t3.c) ON t1.a = t2. DROP TABLE t1, t2, t3; --echo # End of 10.5 tests + +--echo # +--echo # MDEV-31449: Assertion s->table->opt_range_condition_rows <= s->found_records +--echo # +CREATE TABLE t1 (a INT, b INT); +INSERT INTO t1 VALUES (1,2),(3,4); + +CREATE TABLE t2 (c INT); +INSERT INTO t2 VALUES (5),(6); + +SET @tmp=@@OPTIMIZER_USE_CONDITION_SELECTIVITY, OPTIMIZER_USE_CONDITION_SELECTIVITY = 1; + +SELECT * FROM +(SELECT t1.* FROM t1 WHERE t1.a IN (SELECT MAX(t2.c) FROM t2 JOIN t1)) AS sq1, +(SELECT t2.* FROM t2 JOIN t1 ON (t1.b IN (SELECT t1.b FROM t2 STRAIGHT_JOIN t1))) AS sq2; + +SET OPTIMIZER_USE_CONDITION_SELECTIVITY=@tmp; +DROP TABLE t1,t2; +--echo # End of 10.6 tests diff --git a/sql/sql_select.cc b/sql/sql_select.cc index f87c01f5681..afa5593bd7e 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -5575,7 +5575,7 @@ make_join_statistics(JOIN *join, List &tables_list, get_delayed_table_estimates(s->table, &s->records, &s->read_time, &s->startup_cost); s->found_records= s->records; - table->opt_range_condition_rows=s->records; + s->table->opt_range_condition_rows=s->records; } else s->scan_time(); From bd076d4dfff64c3ee3d7abc8fb8108a79c36d97c Mon Sep 17 00:00:00 2001 From: Thirunarayanan Balathandayuthapani Date: Mon, 12 Jun 2023 16:10:39 +0530 Subject: [PATCH 5/6] MDEV-31442 page_cleaner thread aborts while releasing the tablespace - InnoDB shouldn't acquire the tablespace when it is being stopped or closed --- storage/innobase/include/fil0fil.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storage/innobase/include/fil0fil.h b/storage/innobase/include/fil0fil.h index 053fe7a68e0..10c084bbbf1 100644 --- a/storage/innobase/include/fil0fil.h +++ b/storage/innobase/include/fil0fil.h @@ -1526,7 +1526,7 @@ template inline void fil_space_t::flush() flush_low(); else { - if (!(acquire_low(STOPPING & CLOSING) & (STOPPING | CLOSING))) + if (!(acquire_low(STOPPING | CLOSING) & (STOPPING | CLOSING))) { flush_low(); release(); From 0b61f4e0e4d9359bfba1983e45a1e6c03826c05c Mon Sep 17 00:00:00 2001 From: Sergei Petrunia Date: Mon, 19 Jun 2023 21:42:16 +0300 Subject: [PATCH 6/6] Fix comment --- sql/sql_priv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/sql_priv.h b/sql/sql_priv.h index a304cd39df7..7c8b604539c 100644 --- a/sql/sql_priv.h +++ b/sql/sql_priv.h @@ -190,7 +190,7 @@ /* The rest of the file is included in the server only */ #ifndef MYSQL_CLIENT -/* @@optimizer_switch flags. These must be in sync with optimizer_switch_typelib */ +/* @@optimizer_switch flags. These must be in sync with optimizer_switch_names */ #define OPTIMIZER_SWITCH_INDEX_MERGE (1ULL << 0) #define OPTIMIZER_SWITCH_INDEX_MERGE_UNION (1ULL << 1) #define OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION (1ULL << 2)