Skip to content

Commit

Permalink
MDEV-22014: Rowid Filtering is not displayed well in the optimizer trace
Browse files Browse the repository at this point in the history
- Print the rowid filters that are available for use with each table.
- Make print_best_access_for_table() print which filter it has picked.
- Make best_access_path() print the filter for considered ref accesses.
  • Loading branch information
spetrunia committed Apr 2, 2020
1 parent bdcecfa commit a219006
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 265 deletions.
410 changes: 161 additions & 249 deletions mysql-test/main/opt_trace.result

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions mysql-test/main/opt_trace_index_merge.result
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ explain select * from t1 where a=1 or b=1 {
"type": "index_merge",
"records": 2,
"cost": 4.1484,
"uses_join_buffering": false,
"filter_used": false
"uses_join_buffering": false
}
},
"rows_for_plan": 2,
Expand Down
13 changes: 11 additions & 2 deletions mysql-test/main/opt_trace_index_merge_innodb.result
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
}
}
},
{
"table": "t1",
"rowid_filters": [
{
"key": "key1",
"build_cost": 1.1801,
"rows": 1
}
]
},
{
"selectivity_for_indexes": [
{
Expand Down Expand Up @@ -212,8 +222,7 @@ explain select * from t1 where pk1 != 0 and key1 = 1 {
"type": "ref",
"records": 1,
"cost": 2,
"uses_join_buffering": false,
"filter_used": false
"uses_join_buffering": false
}
},
"rows_for_plan": 1,
Expand Down
6 changes: 2 additions & 4 deletions mysql-test/main/opt_trace_security.result
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ select * from db1.t1 {
"type": "scan",
"records": 3,
"cost": 2.0051,
"uses_join_buffering": false,
"filter_used": false
"uses_join_buffering": false
}
},
"rows_for_plan": 3,
Expand Down Expand Up @@ -227,8 +226,7 @@ select * from db1.v1 {
"type": "scan",
"records": 3,
"cost": 2.0051,
"uses_join_buffering": false,
"filter_used": false
"uses_join_buffering": false
}
},
"rows_for_plan": 3,
Expand Down
21 changes: 13 additions & 8 deletions sql/opt_trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "my_json_writer.h"
#include "sp_head.h"

#include "rowid_filter.h"

const char I_S_table_name[]= "OPTIMIZER_TRACE";

/**
Expand Down Expand Up @@ -661,14 +663,17 @@ void print_best_access_for_table(THD *thd, POSITION *pos,
{
DBUG_ASSERT(thd->trace_started());

Json_writer_object trace_best_access(thd, "chosen_access_method");
trace_best_access.add("type", type == JT_ALL ? "scan" :
join_type_str[type]);
trace_best_access.add("records", pos->records_read);
trace_best_access.add("cost", pos->read_time);
trace_best_access.add("uses_join_buffering", pos->use_join_buffer);
trace_best_access.add("filter_used",
pos->range_rowid_filter_info != NULL);
Json_writer_object obj(thd, "chosen_access_method");
obj.add("type", type == JT_ALL ? "scan" : join_type_str[type]);
obj.add("records", pos->records_read);
obj.add("cost", pos->read_time);
obj.add("uses_join_buffering", pos->use_join_buffer);
if (pos->range_rowid_filter_info)
{
uint key_no= pos->range_rowid_filter_info->key_no;
obj.add("rowid_filter_key",
pos->table->table->key_info[key_no].name);
}
}


Expand Down
29 changes: 29 additions & 0 deletions sql/rowid_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "opt_range.h"
#include "rowid_filter.h"
#include "sql_select.h"
#include "opt_trace.h"


inline
Expand Down Expand Up @@ -403,9 +404,37 @@ void TABLE::init_cost_info_for_usable_range_rowid_filters(THD *thd)
}

prune_range_rowid_filters();

if (unlikely(thd->trace_started()))
trace_range_rowid_filters(thd);
}


void TABLE::trace_range_rowid_filters(THD *thd) const
{
if (!range_rowid_filter_cost_info_elems)
return;

Range_rowid_filter_cost_info **p= range_rowid_filter_cost_info_ptr;
Range_rowid_filter_cost_info **end= p + range_rowid_filter_cost_info_elems;

Json_writer_object js_obj(thd);
js_obj.add_table_name(this);
Json_writer_array js_arr(thd, "rowid_filters");

for (; p < end; p++)
(*p)->trace_info(thd);
}


void Range_rowid_filter_cost_info::trace_info(THD *thd)
{
Json_writer_object js_obj(thd);
js_obj.add("key", table->key_info[key_no].name);
js_obj.add("build_cost", b);
js_obj.add("rows", est_elements);
}

/**
@brief
Choose the best range filter for the given access of the table
Expand Down
2 changes: 2 additions & 0 deletions sql/rowid_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,8 @@ class Range_rowid_filter_cost_info : public Sql_alloc

double get_a() { return a; }

void trace_info(THD *thd);

friend
void TABLE::prune_range_rowid_filters();

Expand Down
2 changes: 2 additions & 0 deletions sql/sql_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7758,6 +7758,8 @@ best_access_path(JOIN *join,
filter->get_cmp_gain(rows);
tmp-= filter->get_adjusted_gain(rows) - filter->get_cmp_gain(rows);
DBUG_ASSERT(tmp >= 0);
trace_access_idx.add("rowid_filter_key",
s->table->key_info[filter->key_no].name);
}
}
trace_access_idx.add("rows", records).add("cost", tmp);
Expand Down
1 change: 1 addition & 0 deletions sql/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@ struct TABLE

void init_cost_info_for_usable_range_rowid_filters(THD *thd);
void prune_range_rowid_filters();
void trace_range_rowid_filters(THD *thd) const;
Range_rowid_filter_cost_info *
best_range_rowid_filter_for_partial_join(uint access_key_no,
double records,
Expand Down

0 comments on commit a219006

Please sign in to comment.