diff --git a/mysql-test/r/txsql_convert_to_count_star.result b/mysql-test/r/txsql_convert_to_count_star.result new file mode 100644 index 00000000000..d62eaea1822 --- /dev/null +++ b/mysql-test/r/txsql_convert_to_count_star.result @@ -0,0 +1,250 @@ +create table t1(a int, b int); +create table t2(a int not null, b int not null); +INSERT INTO t1 VALUES (1, 1), (2, NULL), (3, 3); +INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3); +ANALYZE TABLE t1,t2; +Table Op Msg_type Msg_text +test.t1 analyze status OK +test.t2 analyze status OK +set @saved_value=@@txsql_count_conversion_enabled; +set @@txsql_count_conversion_enabled = 0; +flush status; +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(*) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(*) from t1 where a > 1; +count(*) +2 +EXPLAIN format=tree select count(b) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(t1.b) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(b) from t1 where a > 1; +count(b) +1 +EXPLAIN format=tree select count(*) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(*) from t2 where a > 1; +count(*) +2 +EXPLAIN format=tree select count(b) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(t2.b) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 where a > 1; +count(b) +2 +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) + -> Table scan on t2 (cost=0.55 rows=3) + -> Hash + -> Table scan on t1 (cost=0.18 rows=3) + +select count(t2.b) from t2 left join t1 on t1.a = t2.a; +count(t2.b) +3 +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) + -> Table scan on t1 (cost=0.55 rows=3) + -> Hash + -> Table scan on t2 (cost=0.18 rows=3) + +select count(t2.b) from t1 left join t2 on t1.a = t2.a; +count(t2.b) +3 +EXPLAIN format=tree select count(b) from t2 group by a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 group by a; +count(b) +1 +1 +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +set @@txsql_count_conversion_enabled = 1; +EXPLAIN format=tree select count(*) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(*) from t1 where a > 1; +count(*) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 2 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(b) from t1 where a > 1; +EXPLAIN +-> Aggregate: count(t1.b) (cost=0.65 rows=1) + -> Filter: (t1.a > 1) (cost=0.55 rows=1) + -> Table scan on t1 (cost=0.55 rows=3) + +select count(b) from t1 where a > 1; +count(b) +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 4 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(*) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(0) (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(*) from t2 where a > 1; +count(*) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 6 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +EXPLAIN format=tree select count(b) from t2 where a > 1; +EXPLAIN +-> Aggregate: count(0) using conversion for count of not null column (cost=0.65 rows=1) + -> Filter: (t2.a > 1) (cost=0.55 rows=1) + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 where a > 1; +count(b) +2 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 8 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 2 +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(0) using conversion for count of not null column (cost=2.23 rows=1) + -> Left hash join (t1.a = t2.a) (cost=1.33 rows=9) + -> Table scan on t2 (cost=0.55 rows=3) + -> Hash + -> Table scan on t1 (cost=0.18 rows=3) + +select count(t2.b) from t2 left join t1 on t1.a = t2.a; +count(t2.b) +3 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 10 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 4 +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +EXPLAIN +-> Aggregate: count(t2.b) (cost=2.23 rows=1) + -> Left hash join (t2.a = t1.a) (cost=1.33 rows=9) + -> Table scan on t1 (cost=0.55 rows=3) + -> Hash + -> Table scan on t2 (cost=0.18 rows=3) + +select count(t2.b) from t1 left join t2 on t1.a = t2.a; +count(t2.b) +3 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 12 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 4 +EXPLAIN format=tree select count(b) from t2 group by a; +EXPLAIN +-> Table scan on + -> Aggregate using temporary table + -> Table scan on t2 (cost=0.55 rows=3) + +select count(b) from t2 group by a; +count(b) +1 +1 +1 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 14 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 6 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 14 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 6 +# --include save_and_set_px.inc +set @@txsql_count_conversion_enabled=0; +EXPLAIN format=tree select avg(b) from t2; +EXPLAIN +-> Aggregate: sum(t2.b), count(t2.b) + -> PX Receiver (slice: 0; workers: 1) + -> PX Sender (slice: 1; workers: 3) + -> Aggregate: sum(t2.b), count(t2.b) (cost=0.85 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select avg(b) from t2; +avg(b) +2.0000 +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 0 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 0 +set @@txsql_count_conversion_enabled=1; +EXPLAIN format=tree select avg(b) from t2; +EXPLAIN +-> Aggregate: sum(t2.b), count(0) + -> PX Receiver (slice: 0; workers: 1) + -> PX Sender (slice: 1; workers: 3) + -> Aggregate: sum(t2.b), count(0) (cost=0.85 rows=1) + -> Parallel table scan on t2 (cost=0.55 rows=3) + +select avg(b) from t2; +avg(b) +2.0000 +# --include set_px_to_saved.inc +show status like "%total_count_function%"; +Variable_name Value +Txsql_total_count_function 14 +show status like "%implicit_convert_to_count_star%"; +Variable_name Value +Txsql_implicit_convert_to_count_star 6 +set @@txsql_count_conversion_enabled=@saved_value; +DROP TABLE t1,t2; diff --git a/mysql-test/t/txsql_convert_to_count_star.test b/mysql-test/t/txsql_convert_to_count_star.test new file mode 100644 index 00000000000..6101a39802d --- /dev/null +++ b/mysql-test/t/txsql_convert_to_count_star.test @@ -0,0 +1,109 @@ + +create table t1(a int, b int); +create table t2(a int not null, b int not null); + +INSERT INTO t1 VALUES (1, 1), (2, NULL), (3, 3); +INSERT INTO t2 VALUES (1, 1), (2, 2), (3, 3); + +ANALYZE TABLE t1,t2; + +set @saved_value=@@txsql_count_conversion_enabled; +set @@txsql_count_conversion_enabled = 0; +flush status; +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(*) from t1 where a > 1; +select count(*) from t1 where a > 1; + +EXPLAIN format=tree select count(b) from t1 where a > 1; +select count(b) from t1 where a > 1; + +EXPLAIN format=tree select count(*) from t2 where a > 1; +select count(*) from t2 where a > 1; + +EXPLAIN format=tree select count(b) from t2 where a > 1; +select count(b) from t2 where a > 1; + +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +select count(t2.b) from t2 left join t1 on t1.a = t2.a; + +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +select count(t2.b) from t1 left join t2 on t1.a = t2.a; + +EXPLAIN format=tree select count(b) from t2 group by a; +select count(b) from t2 group by a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +set @@txsql_count_conversion_enabled = 1; + +EXPLAIN format=tree select count(*) from t1 where a > 1; +select count(*) from t1 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(b) from t1 where a > 1; +select count(b) from t1 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(*) from t2 where a > 1; +select count(*) from t2 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(b) from t2 where a > 1; +select count(b) from t2 where a > 1; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(t2.b) from t2 left join t1 on t1.a = t2.a; +select count(t2.b) from t2 left join t1 on t1.a = t2.a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(t2.b) from t1 left join t2 on t1.a = t2.a; +select count(t2.b) from t1 left join t2 on t1.a = t2.a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +EXPLAIN format=tree select count(b) from t2 group by a; +select count(b) from t2 group by a; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +--source suite/parallel_execution/inc/save_and_set_px.inc +connect(conn1,localhost,root,,); +set @@txsql_count_conversion_enabled=0; +connection conn1; +EXPLAIN format=tree select avg(b) from t2; +select avg(b) from t2; + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +set @@txsql_count_conversion_enabled=1; +EXPLAIN format=tree select avg(b) from t2; +select avg(b) from t2; +disconnect conn1; + +connection default; +--source suite/parallel_execution/inc/set_px_to_saved.inc + +show status like "%total_count_function%"; +show status like "%implicit_convert_to_count_star%"; + +set @@txsql_count_conversion_enabled=@saved_value; +DROP TABLE t1,t2; diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt index 59470d1b006..4b7e6f3e6e2 100644 --- a/sql/CMakeLists.txt +++ b/sql/CMakeLists.txt @@ -597,6 +597,7 @@ SET(SQL_SHARED_SOURCES sql_cmd_srs.cc sql_connect.cc sql_constraint.cc + sql_count_conversion.cc sql_cursor.cc sql_data_change.cc sql_db.cc diff --git a/sql/item_sum.h b/sql/item_sum.h index 0c28e962ce1..17c8b7d0454 100644 --- a/sql/item_sum.h +++ b/sql/item_sum.h @@ -1157,6 +1157,8 @@ class Item_sum_count : public Item_sum_int { return (sum_func() == COUNT_DISTINCT_FUNC) ? false : true; } #endif /* defined(HAVE_PX) */ + // True if it is count(0) converted from count(non_null_field) + bool is_coverted_count_zero{false}; }; /* Item to get the value of a stored sum function */ diff --git a/sql/join_optimizer/explain_access_path.cc b/sql/join_optimizer/explain_access_path.cc index 73d430c65f4..a0dfc4c1dd4 100644 --- a/sql/join_optimizer/explain_access_path.cc +++ b/sql/join_optimizer/explain_access_path.cc @@ -1032,6 +1032,14 @@ ExplainData ExplainAccessPath(const AccessPath *path, JOIN *join, } else { ret += ItemToString(*item); } + /* + Note: use real_sum_func(), i.e, Item_rollup_sum_switcher will return + master()->sum_func() in sum_func. + */ + if ((*item)->real_sum_func() == Item_sum::COUNT_FUNC && + ((Item_sum_count *)(*item))->is_coverted_count_zero) { + ret += " using conversion for count of not null column"; + } } description.push_back(move(ret)); children.push_back({path->aggregate().child}); diff --git a/sql/mysqld.cc b/sql/mysqld.cc index 662e69a3ea2..a34a2b2797c 100644 --- a/sql/mysqld.cc +++ b/sql/mysqld.cc @@ -10409,6 +10409,12 @@ SHOW_VAR status_vars[] = { {"Handler_read_rnd_next", (char *)offsetof(System_status_var, ha_read_rnd_next_count), SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, + {"Txsql_implicit_convert_to_count_star", + (char *)offsetof(System_status_var, txsql_implicit_convert_to_count_zero), + SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, + {"Txsql_total_count_function", + (char *)offsetof(System_status_var, txsql_total_count_function), + SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, {"Handler_rollback", (char *)offsetof(System_status_var, ha_rollback_count), SHOW_LONGLONG_STATUS, SHOW_SCOPE_ALL}, {"Handler_savepoint", diff --git a/sql/parallel_execution/px_item.cc b/sql/parallel_execution/px_item.cc index 120f918c617..e4328c6ea01 100644 --- a/sql/parallel_execution/px_item.cc +++ b/sql/parallel_execution/px_item.cc @@ -34,6 +34,7 @@ #include "sql/filesort.h" #include "sql/item.h" #include "sql/item_sum.h" +#include "sql/sql_count_conversion.h" #include "sql/sql_optimizer.h" #include "sql/sql_executor.h" #include "sql/log.h" @@ -219,7 +220,14 @@ Item_sum **Item_sum_avg::pq_rebuild_item(THD *thd, Query_block *select) { } Item_sum *new_sum_item = new (thd->mem_root) Item_sum_sum(POS(), args[0], has_with_distinct(), m_window); - Item_sum *new_count_item = new (thd->mem_root) Item_sum_count(POS(), args[0], m_window); + Item_sum *new_count_item = nullptr; + if (thd->variables.txsql_count_conversion_enabled && can_convert_to_zero(args[0])) { + Item *zero_args = new (thd->mem_root) Item_int(int32{0}, 1); + if (zero_args == nullptr) return nullptr; + new_count_item = new (thd->mem_root) Item_sum_count(POS(), zero_args, m_window); + } else { + new_count_item = new (thd->mem_root) Item_sum_count(POS(), args[0], m_window); + } if (new_sum_item == nullptr || new_sum_item->pq_copy_item(thd, select, this)) { return nullptr; } diff --git a/sql/sql_count_conversion.cc b/sql/sql_count_conversion.cc new file mode 100644 index 00000000000..0d7d84a802f --- /dev/null +++ b/sql/sql_count_conversion.cc @@ -0,0 +1,66 @@ +#include "sql/sql_count_conversion.h" +#include "sql/item.h" +#include "sql/item_sum.h" +#include "sql/parallel_execution/px_interface.h" +#include "sql/opt_trace.h" // Opt_trace_object +#include "sql/opt_trace_context.h" + +void inc_txsql_implicit_convert_to_count_zero() { + current_thd->status_var.txsql_implicit_convert_to_count_zero++; +} + +void inc_txsql_total_count_function() { + current_thd->status_var.txsql_total_count_function++; +} + +bool can_convert_to_zero(Item *item) { + return (item && item->type() == Item::FIELD_ITEM && !item->is_nullable()); +} + +bool count_col_conversion(THD *thd, Item_sum_count *item_count) { + inc_txsql_total_count_function(); + Opt_trace_context *const trace = &thd->opt_trace; + + if (can_convert_to_zero(item_count->get_arg(0))) { + Field *field = ((Item_field *)item_count->get_arg(0))->field; + TABLE *table = field->table; + table->release_field(field->field_index()); + item_count->set_arg(thd, 0, new (thd->mem_root) Item_int(int32{0}, 1)); + item_count->is_coverted_count_zero = true; + inc_txsql_implicit_convert_to_count_zero(); + Opt_trace_object trace_cnt_conversion(trace); + trace_cnt_conversion.add("using count conversion", true); + } + + return false; +} + +bool count_col_conversion(THD *thd, const mem_root_deque &fields) { + DBUG_TRACE; + + assert(thd && thd->variables.txsql_count_conversion_enabled); + for (Item *item : fields) { + if (item->type() == Item::SUM_FUNC_ITEM && !item->m_is_window_function) { + if (item->is_outer_reference()) { + continue; + } + + /* + MySQL will convert count(*) to count(0), and 0 has two meanings:the field + value is not considered and it will not be NULL.Therefore, count(*) counts + all values that contain NULL. Count(col), will add col to read_set, that is, + need to copy col to TABLE::record[0], and will not count the null value in + col. However, if col can ensure that it is not NULL, for example, there is + a not null restriction in the table definition, and col does not belong to + the inner table of left join. Then count(col) is equivalent to count(0). + */ + Item_sum *item_sum = down_cast(item); + if (item_sum->real_sum_func() == Item_sum::COUNT_FUNC && + count_col_conversion(thd, (Item_sum_count *)item_sum)) { + return true; + } + } + if (thd->is_error()) return true; + } + return false; +} \ No newline at end of file diff --git a/sql/sql_count_conversion.h b/sql/sql_count_conversion.h new file mode 100644 index 00000000000..d6dcfd19bd5 --- /dev/null +++ b/sql/sql_count_conversion.h @@ -0,0 +1,14 @@ +#ifndef SQL_COUNT_CONVERSION_INCLUDED +#define SQL_COUNT_CONVERSION_INCLUDED + +class Item; +class Item_sum_count; +class THD; +template +class mem_root_deque; + +extern bool can_convert_to_zero(Item *item); +extern bool count_col_conversion(THD *thd, const mem_root_deque &fields); +extern bool count_col_conversion(THD *thd, Item_sum_count *item_count); + +#endif /* SQL_SELECT_INCLUDED */ \ No newline at end of file diff --git a/sql/sql_optimizer.cc b/sql/sql_optimizer.cc index 8cf83799a28..f92c43ff858 100644 --- a/sql/sql_optimizer.cc +++ b/sql/sql_optimizer.cc @@ -97,6 +97,7 @@ #include "sql/sql_class.h" #include "sql/sql_const.h" #include "sql/sql_const_folding.h" +#include "sql/sql_count_conversion.h" #include "sql/sql_error.h" #include "sql/sql_join_buffer.h" // JOIN_CACHE #include "sql/sql_planner.h" // calculate_condition_filter @@ -446,6 +447,17 @@ bool JOIN::optimize(bool finalize_access_paths) { return true; } + /* + Try to optimize count(col) to count(0) if possible. + */ + if (thd->variables.txsql_count_conversion_enabled && + thd->lex->sql_command == SQLCOM_SELECT && + count_col_conversion(thd, *fields)) { + error = 1; + DBUG_PRINT("error", ("Error from count_col_conversion")); + return true; + } + /* Try to optimize count(*), min() and max() to const fields if there is implicit grouping (aggregate functions but no diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index a307f6263e2..f840b4bcb5c 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -5950,6 +5950,14 @@ int mysql_execute_command(THD *thd, bool first_level) { } if (unlikely(thd->has_backquery())) ha_end_backquery(thd); + for (TABLE *table = thd->open_tables; table; table = table->next) { + DBUG_PRINT("cnt_conversion", + ("clear fields_use_count of table: '%s' query_id: %lu", + table->s->table_name.str, (ulong)table->query_id)); + if (table->fields_use_count && !table->fields_use_count->empty()) { + table->fields_use_count->clear(); + } + } lex->cleanup(thd, true); diff --git a/sql/sys_vars.cc b/sql/sys_vars.cc index 4a9d42906b2..d6db8f8ec9a 100644 --- a/sql/sys_vars.cc +++ b/sql/sys_vars.cc @@ -8937,6 +8937,13 @@ static Sys_var_bool Sys_txsql_pread_count_enabled( CMD_LINE(OPT_ARG), DEFAULT(true), NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr)); +static Sys_var_bool Sys_txsql_count_conversion_enabled( + "txsql_count_conversion_enabled", + "Allow implicit conversion of count on not null column to count(0)", + HINT_UPDATEABLE SESSION_VAR(txsql_count_conversion_enabled), + CMD_LINE(OPT_ARG), DEFAULT(true), + NO_MUTEX_GUARD, NOT_IN_BINLOG, ON_CHECK(nullptr), ON_UPDATE(nullptr)); + #ifdef HAVE_TDSQL static Sys_var_bool Sys_g_sqlAsyn( "sqlasyn", diff --git a/sql/system_variables.h b/sql/system_variables.h index 9608a20e1ac..6070dd079b3 100644 --- a/sql/system_variables.h +++ b/sql/system_variables.h @@ -552,6 +552,8 @@ struct System_variables { #endif /* HAVE_PX */ /// @sa Sys_txsql_pread_count_enabled bool txsql_pread_count_enabled; + /// @sa Sys_txsql_count_conversion_enabled + bool txsql_count_conversion_enabled; /// @sa Sys_txsql_convert_view_to_cte_enabled bool txsql_convert_view_to_cte_enabled; }; @@ -626,6 +628,9 @@ struct System_status_var { /* Number of statements sent from the client. */ ulonglong questions; + ulonglong txsql_implicit_convert_to_count_zero; + ulonglong txsql_total_count_function; + /// How many queries have been executed on a secondary storage engine. ulonglong secondary_engine_execution_count; diff --git a/sql/table.cc b/sql/table.cc index 77bdb9386b6..88f4396ad54 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -3247,6 +3247,13 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, outparam->no_replicate = false; } + if (!outparam->fields_use_count) { + outparam->fields_use_count = new (root) Mem_root_array(root, share->fields); + if (outparam->fields_use_count == nullptr) goto err; // OOM + } else { + outparam->reset_fields_use_count(); + } + /* Increment the opened_tables counter, only when open flags set. */ if (db_stat) thd->status_var.opened_tables++; @@ -3269,6 +3276,9 @@ int open_table_from_share(THD *thd, TABLE_SHARE *share, const char *alias, free_items(table_cc.value_generator()->item_list); } } + if (outparam->fields_use_count) { + outparam->fields_use_count->clear(); + } outparam->file = nullptr; // For easier error checking outparam->db_stat = 0; if (!internal_tmp) root->Clear(); @@ -4127,6 +4137,8 @@ void TABLE::init(THD *thd, TABLE_LIST *tl) { if (!pos_in_table_list->prelocking_placeholder) { bind_value_generators_to_fields(); } + + reset_fields_use_count(); } /** @@ -4163,6 +4175,7 @@ void TABLE::reset() { m_parallel_scan = false; m_parallel_workers = 0; #endif /* defined(HAVE_PX) */ + reset_fields_use_count(); } /** @@ -4229,6 +4242,7 @@ bool TABLE::init_tmp_table(THD *thd, TABLE_SHARE *share, MEM_ROOT *m_root, #ifndef NDEBUG set_tmp_table_seq_id(thd->get_tmp_table_seq_id()); #endif + reset_fields_use_count(); return false; } @@ -5454,6 +5468,7 @@ void TABLE::mark_column_used(Field *field, enum enum_mark_columns mark) { case MARK_COLUMNS_READ: { Key_map part_of_key = field->part_of_key; bitmap_set_bit(read_set, field->field_index()); + use_field(field->field_index()); part_of_key.merge(field->part_of_prefixkey); covering_keys.intersect(part_of_key); @@ -5471,6 +5486,7 @@ void TABLE::mark_column_used(Field *field, enum enum_mark_columns mark) { case MARK_COLUMNS_TEMP: bitmap_set_bit(read_set, field->field_index()); + use_field(field->field_index()); if (field->is_virtual_gcol()) mark_gcol_in_maps(field); break; } @@ -5552,6 +5568,7 @@ void TABLE::mark_auto_increment_column() { store() to check overflow of auto_increment values */ bitmap_set_bit(read_set, found_next_number_field->field_index()); + use_field(found_next_number_field->field_index()); bitmap_set_bit(write_set, found_next_number_field->field_index()); if (s->next_number_keypart) mark_columns_used_by_index_no_reset(s->next_number_index, read_set); @@ -5584,8 +5601,10 @@ void TABLE::mark_columns_needed_for_delete(THD *thd) { if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE) { Field **reg_field; for (reg_field = field; *reg_field; reg_field++) { - if ((*reg_field)->is_flag_set(PART_KEY_FLAG)) + if ((*reg_field)->is_flag_set(PART_KEY_FLAG)) { bitmap_set_bit(read_set, (*reg_field)->field_index()); + use_field((*reg_field)->field_index()); + } } file->column_bitmaps_signal(); } @@ -5661,8 +5680,10 @@ void TABLE::mark_columns_needed_for_update(THD *thd, bool mark_binlog_columns) { Field **reg_field; for (reg_field = field; *reg_field; reg_field++) { /* Merge keys is all keys that had a column referred to in the query */ - if (merge_keys.is_overlapping((*reg_field)->part_of_key)) + if (merge_keys.is_overlapping((*reg_field)->part_of_key)) { bitmap_set_bit(read_set, (*reg_field)->field_index()); + use_field((*reg_field)->field_index()); + } } file->column_bitmaps_signal(); } @@ -5758,8 +5779,10 @@ void TABLE::mark_columns_per_binlog_row_image(THD *thd) { */ if ((s->primary_key < MAX_KEY) && (my_field->is_flag_set(PRI_KEY_FLAG) || - (my_field->type() != MYSQL_TYPE_BLOB))) + (my_field->type() != MYSQL_TYPE_BLOB))) { bitmap_set_bit(read_set, my_field->field_index()); + use_field(my_field->field_index()); + } if (my_field->type() != MYSQL_TYPE_BLOB) bitmap_set_bit(write_set, my_field->field_index()); @@ -7320,7 +7343,10 @@ void TABLE::mark_gcol_in_maps(const Field *field) { Typed array fields internally are using a conversion field, it needs to marked as readable in order to do conversions. */ - if (field->is_array()) bitmap_set_bit(read_set, field->field_index()); + if (field->is_array()) { + bitmap_set_bit(read_set, field->field_index()); + use_field(field->field_index()); + } /* Note that underlying base columns are here added to read_set but not added @@ -7338,6 +7364,7 @@ void TABLE::mark_gcol_in_maps(const Field *field) { for (uint i = 0; i < s->fields; i++) { if (bitmap_is_set(&field->gcol_info->base_columns_map, i)) { bitmap_set_bit(read_set, i); + use_field(i); if (this->field[i]->is_virtual_gcol()) bitmap_set_bit(write_set, i); } } diff --git a/sql/table.h b/sql/table.h index 11fcab2a960..d5138ba4a98 100644 --- a/sql/table.h +++ b/sql/table.h @@ -1663,6 +1663,43 @@ struct TABLE { // List of table check constraints. Sql_table_check_constraint_list *table_check_constraint_list{nullptr}; + public: + /** + Reference count of each field in read operation (read_set). Valid only + for SELECT statemnt. + */ + Mem_root_array *fields_use_count{nullptr}; + + /** + In the prepare phase, every time Field appears in fields and conditions, + read_set will be set once. Introduce TABLE::fields_use_count to record + the number of times each field is referenced. If use_count is 0, remove + it from TABLE::read_set. + */ + void release_field(uint16 field_index) { + if (fields_use_count == nullptr) return; + if (fields_use_count->empty()) return; + if (fields_use_count->size() > field_index && + --((*fields_use_count)[field_index]) == 0) { + bitmap_clear_bit(read_set, field_index); + } + } + + void use_field(uint16 field_index) { + if (fields_use_count == nullptr) return; + if (fields_use_count->empty()) return; + if (fields_use_count->size() > field_index) { + ++(*fields_use_count)[field_index]; + } + } + + void reset_fields_use_count() { + if (fields_use_count) { + fields_use_count->clear(); + fields_use_count->resize(s->fields); + } + } + private: /** If true, this table is inner w.r.t. some outer join operation, all columns diff --git a/unittest/gunit/fake_table.h b/unittest/gunit/fake_table.h index 7c8bb650d1c..fa3c54787dd 100644 --- a/unittest/gunit/fake_table.h +++ b/unittest/gunit/fake_table.h @@ -300,6 +300,7 @@ class Fake_TABLE : public TABLE { new_field->table_name = &table_name; new_field->set_field_index(pos); bitmap_set_bit(read_set, pos); + use_field(pos); const ptrdiff_t field_offset = pos * MAX_FIELD_WIDTH; new_field->set_field_ptr(record[0] + field_offset + 1); if (new_field->get_null_ptr() != nullptr)