From 37612cd7b3b235567aad3250334ce906ecfb5e75 Mon Sep 17 00:00:00 2001 From: tianfengli Date: Tue, 15 Oct 2024 11:23:56 +0800 Subject: [PATCH] show more detailed optimizer trace information in test_if_cheaper_ordering() --- sql/sql_select.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 7f77dcf18ec..e0abba66b96 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -5144,6 +5144,9 @@ bool test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER_with_src *order, const bool has_limit = (select_limit != HA_POS_ERROR); const join_type cur_access_method = tab ? tab->type() : JT_ALL; + Opt_trace_context *const trace = ¤t_thd->opt_trace; + Opt_trace_object trace_this(trace, "considering_cheaper_ordering"); + if (join) { read_time = tab->position()->read_cost; for (uint jt = tab->idx() + 1; jt < join->primary_tables; jt++) { @@ -5154,6 +5157,7 @@ bool test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER_with_src *order, } else read_time = table->file->table_scan_cost().total_cost(); + trace_this.add("read_cost", read_time); /* Calculate the selectivity of the ref_key for REF_ACCESS. For RANGE_ACCESS we use table->quick_condition_rows. @@ -5279,6 +5283,10 @@ bool test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER_with_src *order, min(table->file->page_read_cost(nr, rec_per_key), table_scan_time.total_cost()); + Opt_trace_object trace_this_key(trace); + trace_this_key.add_alnum("index", keyinfo->name) + .add("index_scan_cost", index_scan_time); + /* Switch to index that gives order if its scan time is smaller than read_time of current chosen access method. In addition, if the @@ -5317,8 +5325,13 @@ bool test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER_with_src *order, is_best_covering = is_covering; best_key_direction = direction; best_select_limit = select_limit; + + trace_this_key.add("select_limit", select_limit) + .add("rows", quick_records) + .add("chosen", true); } - } + } else + trace_this_key.add("chosen", false).add_alnum("cause", "cost"); } } } -- 2.19.1