From a11a10191adcf4307ed0d400ef872b74c492d192 Mon Sep 17 00:00:00 2001 From: Dave Gosselin Date: Fri, 29 Mar 2024 10:25:21 -0400 Subject: [PATCH] accrue statistics to correct handler --- sql/handler.cc | 5 +++-- sql/handler.h | 8 +++++++- sql/sql_explain.cc | 14 +++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/sql/handler.cc b/sql/handler.cc index b52551fee31..f2310a1489d 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -6995,6 +6995,7 @@ extern "C" check_result_t handler_index_cond_check(void* h_arg) check_result_t res; DEBUG_SYNC(thd, "handler_index_cond_check"); + DBUG_ASSERT(h->handler_stats); enum thd_kill_levels killed= thd_kill_level(thd); if (unlikely(killed != THD_IS_NOT_KILLED)) @@ -7008,13 +7009,13 @@ extern "C" check_result_t handler_index_cond_check(void* h_arg) if (unlikely(h->end_range) && h->compare_key2(h->end_range) > 0) return CHECK_OUT_OF_RANGE; h->increment_statistics(&SSV::ha_icp_attempts); - h->active_handler_stats.icp_attempts++; + h->handler_stats->icp_attempts++; res= CHECK_NEG; if (h->pushed_idx_cond->val_int()) { res= CHECK_POS; h->fast_increment_statistics(&SSV::ha_icp_match); - h->active_handler_stats.icp_match++; + h->handler_stats->icp_match++; } return res; } diff --git a/sql/handler.h b/sql/handler.h index 0224313924c..0b6a8151311 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -3211,7 +3211,13 @@ protected: ha_rows estimation_rows_to_insert; handler *lookup_handler; - /* Statistics for the query. Updated if handler_stats.in_use is set */ + /* + Statistics for the query. Prefer to use the handler_stats pointer + below rather than this object directly as the clone() method will + modify how stats are accounted by adjusting the handler_stats + pointer. Referring to active_handler_stats directly will yield + surprising and possibly incorrect results. + */ ha_handler_stats active_handler_stats; void set_handler_stats(); public: diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc index 48fe7ab01c2..274f107c0ce 100644 --- a/sql/sql_explain.cc +++ b/sql/sql_explain.cc @@ -1938,13 +1938,13 @@ static void trace_engine_stats(handler *file, Json_writer *writer) static void print_r_icp_filtered(handler *file, Json_writer *writer) { - if (file && file->handler_stats && file->pushed_idx_cond) - { - ha_handler_stats *hs= file->handler_stats; - double r_icp_filtered = hs->icp_attempts ? - (double)(hs->icp_match) / (double)(hs->icp_attempts) : 1.0; - writer->add_member("r_icp_filtered").add_double(r_icp_filtered * 100); - } + if (!file || !file->handler_stats || !file->pushed_idx_cond) + return; + + ha_handler_stats *hs= file->handler_stats; + double r_icp_filtered = hs->icp_attempts ? + (double)(hs->icp_match) / (double)(hs->icp_attempts) : 0.0; + writer->add_member("r_icp_filtered").add_double(r_icp_filtered * 100); } void Explain_table_access::print_explain_json(Explain_query *query,