diff --git a/mysql-test/r/analyze_stmt.result b/mysql-test/r/analyze_stmt.result index 0e62654f256..b07c6679186 100644 --- a/mysql-test/r/analyze_stmt.result +++ b/mysql-test/r/analyze_stmt.result @@ -74,4 +74,9 @@ analyze select a, a in (select t0.b from t0 where t0.b+1=t1.b+1) from t1; id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra 1 PRIMARY t1 ALL NULL NULL NULL NULL 3 3 100.00 100.00 2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 3 100.00 33.33 Using where +# Try a subquery that is never executed +analyze select a, a in (select t0.b from t0 where t0.b+1=t1.b+1) from t1 where t1.a > 5; +id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra +1 PRIMARY t1 ALL NULL NULL NULL NULL 3 3 100.00 0.00 Using where +2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 NULL 100.00 NULL Using where drop table t0,t1; diff --git a/mysql-test/t/analyze_stmt.test b/mysql-test/t/analyze_stmt.test index 5169b342cde..a59c4017625 100644 --- a/mysql-test/t/analyze_stmt.test +++ b/mysql-test/t/analyze_stmt.test @@ -58,5 +58,8 @@ insert into t1 values (1,1),(2,2),(3,3); --echo # See .test file for the right values of r_rows and r_filtered. analyze select a, a in (select t0.b from t0 where t0.b+1=t1.b+1) from t1; +--echo # Try a subquery that is never executed +analyze select a, a in (select t0.b from t0 where t0.b+1=t1.b+1) from t1 where t1.a > 5; + drop table t0,t1; diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc index 58029ba7907..adfceae346c 100644 --- a/sql/sql_explain.cc +++ b/sql/sql_explain.cc @@ -553,9 +553,16 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai /* `r_rows` */ if (is_analyze) { - ha_rows avg_rows= tracker.get_avg_rows(); - item_list.push_back(new Item_int((longlong) (ulonglong) avg_rows, - MY_INT64_NUM_DECIMAL_DIGITS)); + if (!tracker.has_scans()) + { + item_list.push_back(item_null); + } + else + { + ha_rows avg_rows= tracker.get_avg_rows(); + item_list.push_back(new Item_int((longlong) (ulonglong) avg_rows, + MY_INT64_NUM_DECIMAL_DIGITS)); + } } /* `filtered` */ @@ -572,12 +579,19 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai /* `r_filtered` */ if (is_analyze) { - double r_filtered; - if (tracker.r_rows > 0) - r_filtered= 100.0 * (double)tracker.r_rows_after_table_cond / tracker.r_rows; + if (!tracker.has_scans()) + { + item_list.push_back(item_null); + } else - r_filtered= 100.0; - item_list.push_back(new Item_float(r_filtered, 2)); + { + double r_filtered; + if (tracker.r_rows > 0) + r_filtered= 100.0 * (double)tracker.r_rows_after_table_cond / tracker.r_rows; + else + r_filtered= 100.0; + item_list.push_back(new Item_float(r_filtered, 2)); + } } /* `Extra` */ diff --git a/sql/sql_explain.h b/sql/sql_explain.h index f9279a406f7..7aa62111a0a 100644 --- a/sql/sql_explain.h +++ b/sql/sql_explain.h @@ -28,6 +28,7 @@ public: ha_rows r_rows_after_table_cond; /* Rows after applying the table condition */ ha_rows r_rows_after_where; /* Rows after applying attached part of WHERE */ + bool has_scans() { return (r_scans != 0); } ha_rows get_avg_rows() { return r_scans ? (ha_rows)rint((double) r_rows / r_scans): 0;