From 68542caea1734c8ca89682c8d0740f896eab8e46 Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Wed, 18 Oct 2023 16:27:56 +0700 Subject: [PATCH] MDEV-32475 Add logging of test_if_skip_sort_order to optimizer trace --- mysql-test/main/opt_trace.result | 293 +++++++++++++++++-------------- sql/sql_select.cc | 6 + 2 files changed, 165 insertions(+), 134 deletions(-) diff --git a/mysql-test/main/opt_trace.result b/mysql-test/main/opt_trace.result index 62643c60bc7..3b7961c2349 100644 --- a/mysql-test/main/opt_trace.result +++ b/mysql-test/main/opt_trace.result @@ -468,6 +468,9 @@ select * from v2 { } ] } + }, + { + "test_if_skip_sort_order": [] } ] } @@ -796,6 +799,9 @@ explain select * from v1 { } ] } + }, + { + "test_if_skip_sort_order": [] } ] } @@ -1290,6 +1296,9 @@ EXPLAIN SELECT DISTINCT a FROM t1 { } ] } + }, + { + "test_if_skip_sort_order": [] } ] } @@ -1485,23 +1494,27 @@ EXPLAIN SELECT MIN(d) FROM t1 where b=2 and c=3 group by a { } }, { - "reconsidering_access_paths_for_index_ordering": { - "clause": "GROUP BY", - "fanout": 1, - "read_time": 3.3131, - "table": "t1", - "rows_estimation": 7, - "possible_keys": [ - { - "index": "a", - "can_resolve_order": true, - "updated_limit": 7, - "index_scan_time": 7, - "records": 7, - "chosen": true + "test_if_skip_sort_order": [ + { + "reconsidering_access_paths_for_index_ordering": { + "clause": "GROUP BY", + "fanout": 1, + "read_time": 3.3131, + "table": "t1", + "rows_estimation": 7, + "possible_keys": [ + { + "index": "a", + "can_resolve_order": true, + "updated_limit": 7, + "index_scan_time": 7, + "records": 7, + "chosen": true + } + ] } - ] - } + } + ] } ] } @@ -1687,23 +1700,27 @@ EXPLAIN SELECT id,MIN(a),MAX(a) FROM t1 WHERE a>=20010104e0 GROUP BY id { } }, { - "reconsidering_access_paths_for_index_ordering": { - "clause": "GROUP BY", - "fanout": 1, - "read_time": 2.0322, - "table": "t1", - "rows_estimation": 9, - "possible_keys": [ - { - "index": "id", - "can_resolve_order": true, - "updated_limit": 16, - "index_scan_time": 16, - "records": 16, - "chosen": true + "test_if_skip_sort_order": [ + { + "reconsidering_access_paths_for_index_ordering": { + "clause": "GROUP BY", + "fanout": 1, + "read_time": 2.0322, + "table": "t1", + "rows_estimation": 9, + "possible_keys": [ + { + "index": "id", + "can_resolve_order": true, + "updated_limit": 16, + "index_scan_time": 16, + "records": 16, + "chosen": true + } + ] } - ] - } + } + ] } ] } @@ -1878,23 +1895,27 @@ EXPLAIN SELECT * FROM t1 WHERE a = 20010104e0 GROUP BY id { } }, { - "reconsidering_access_paths_for_index_ordering": { - "clause": "GROUP BY", - "fanout": 1, - "read_time": 2.0322, - "table": "t1", - "rows_estimation": 9, - "possible_keys": [ - { - "index": "id", - "can_resolve_order": true, - "updated_limit": 16, - "index_scan_time": 16, - "records": 16, - "chosen": true + "test_if_skip_sort_order": [ + { + "reconsidering_access_paths_for_index_ordering": { + "clause": "GROUP BY", + "fanout": 1, + "read_time": 2.0322, + "table": "t1", + "rows_estimation": 9, + "possible_keys": [ + { + "index": "id", + "can_resolve_order": true, + "updated_limit": 16, + "index_scan_time": 16, + "records": 16, + "chosen": true + } + ] } - ] - } + } + ] } ] } @@ -2176,97 +2197,101 @@ explain select * from t1 where a=1 and b=2 order by c limit 1 { } }, { - "reconsidering_access_paths_for_index_ordering": { - "clause": "ORDER BY", - "fanout": 1, - "read_time": 22.001, - "table": "t1", - "rows_estimation": 21, - "possible_keys": [ - { - "index": "a_a", - "can_resolve_order": true, - "updated_limit": 47, - "index_scan_time": 47, - "usable": false, - "cause": "cost" - }, - { - "index": "a_c", - "can_resolve_order": true, - "updated_limit": 47, - "range_scan_time": 4.324, - "index_scan_time": 4.324, - "records": 180, - "chosen": true - }, - { - "index": "a_b", - "can_resolve_order": false, - "cause": "not usable index for the query" + "test_if_skip_sort_order": [ + { + "reconsidering_access_paths_for_index_ordering": { + "clause": "ORDER BY", + "fanout": 1, + "read_time": 22.001, + "table": "t1", + "rows_estimation": 21, + "possible_keys": [ + { + "index": "a_a", + "can_resolve_order": true, + "updated_limit": 47, + "index_scan_time": 47, + "usable": false, + "cause": "cost" + }, + { + "index": "a_c", + "can_resolve_order": true, + "updated_limit": 47, + "range_scan_time": 4.324, + "index_scan_time": 4.324, + "records": 180, + "chosen": true + }, + { + "index": "a_b", + "can_resolve_order": false, + "cause": "not usable index for the query" + } + ] } - ] - } - }, - { - "table": "t1", - "range_analysis": { - "table_scan": { - "rows": 1000, - "cost": 2e308 }, - "potential_range_indexes": [ - { - "index": "a_a", - "usable": false, - "cause": "not applicable" - }, - { - "index": "a_c", - "usable": true, - "key_parts": ["a", "c"] - }, - { - "index": "a_b", - "usable": false, - "cause": "not applicable" - } - ], - "setup_range_conditions": [], - "group_index_range": { - "chosen": false, - "cause": "no group by or distinct" - }, - "analyzing_range_alternatives": { - "range_scan_alternatives": [ - { - "index": "a_c", - "ranges": ["(1) <= (a) <= (1)"], - "rowid_ordered": false, - "using_mrr": false, - "index_only": false, - "rows": 180, - "cost": 229.72, + { + "table": "t1", + "range_analysis": { + "table_scan": { + "rows": 1000, + "cost": 2e308 + }, + "potential_range_indexes": [ + { + "index": "a_a", + "usable": false, + "cause": "not applicable" + }, + { + "index": "a_c", + "usable": true, + "key_parts": ["a", "c"] + }, + { + "index": "a_b", + "usable": false, + "cause": "not applicable" + } + ], + "setup_range_conditions": [], + "group_index_range": { + "chosen": false, + "cause": "no group by or distinct" + }, + "analyzing_range_alternatives": { + "range_scan_alternatives": [ + { + "index": "a_c", + "ranges": ["(1) <= (a) <= (1)"], + "rowid_ordered": false, + "using_mrr": false, + "index_only": false, + "rows": 180, + "cost": 229.72, + "chosen": true + } + ], + "analyzing_roworder_intersect": { + "cause": "too few roworder scans" + }, + "analyzing_index_merge_union": [] + }, + "chosen_range_access_summary": { + "range_access_plan": { + "type": "range_scan", + "index": "a_c", + "rows": 180, + "ranges": ["(1) <= (a) <= (1)"] + }, + "rows_for_plan": 180, + "cost_for_plan": 229.72, "chosen": true } - ], - "analyzing_roworder_intersect": { - "cause": "too few roworder scans" - }, - "analyzing_index_merge_union": [] - }, - "chosen_range_access_summary": { - "range_access_plan": { - "type": "range_scan", - "index": "a_c", - "rows": 180, - "ranges": ["(1) <= (a) <= (1)"] - }, - "rows_for_plan": 180, - "cost_for_plan": 229.72, - "chosen": true + } } - } + ] } ] } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index f3ebf830301..e3a3a33a7c1 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -23608,8 +23608,12 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, bool orig_cond_saved= false; int best_key= -1; bool changed_key= false; + THD *thd= tab->join->thd; DBUG_ENTER("test_if_skip_sort_order"); + Json_writer_object trace_wrapper(thd); + Json_writer_array trace_arr(thd, "test_if_skip_sort_order"); + /* Check that we are always called with first non-const table */ DBUG_ASSERT(tab == tab->join->join_tab + tab->join->const_tables); @@ -23618,6 +23622,8 @@ test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit, tab->type == JT_CONST || tab->type == JT_SYSTEM) { + Json_writer_object trace_skip(thd); + trace_skip.add("skipped", "single row access method"); DBUG_RETURN(1); }