diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 3b07122e7c1..e1092388ab3 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -9975,6 +9975,12 @@ sort_positions(SORT_POSITION *a, SORT_POSITION *b)
     return 1;
   if (a->position->records_read < b->position->records_read)
     return -1;
+  
+  if (a->position->read_time > b->position->read_time)
+    return 1;
+  if (a->position->read_time < b->position->read_time)
+    return -1;
+
   return CMP_NUM(*a->join_tab, *b->join_tab);
 }
 
@@ -10312,6 +10318,8 @@ best_extension_by_limited_search(JOIN      *join,
   }
   DBUG_ASSERT(join->next_sort_position <=
               join->sort_positions + join->sort_space);
+  double min_rec_count= DBL_MAX;
+  double min_rec_count_read_time= DBL_MAX;
 
   accepted_tables= 0;
   for (SORT_POSITION *pos= sort ; pos < sort_end ; pos++)
@@ -10400,9 +10408,36 @@ best_extension_by_limited_search(JOIN      *join,
             best_record_count= current_record_count;
             best_read_time=    current_read_time;
           }
+          else
+          {
+            if (current_record_count < min_rec_count &&
+                (!(position->key_dependent & allowed_tables) ||
+                 position->records_read < 2.0))
+            {
+              min_rec_count= current_record_count;
+              min_rec_count_read_time= current_read_time;
+            }
+            else
+            {
+              if (current_record_count >= min_rec_count &&
+                  current_read_time >= min_rec_count_read_time)
+              {
+                trace_one_table.add("pruned_by_heuristic2", true);
+                restore_prev_nj_state(s);
+                restore_prev_sj_state(remaining_tables, s, idx);
+                continue;
+              }
+            }
+          }
         }
         else
         {
+          /*
+             Typically,  we get here if
+               best_record_count < current_record_count &&
+               best_read_time < current_read_time
+             That is, both record_count and read_time are worse than what we've saw.
+          */
           DBUG_EXECUTE("opt", print_plan(join, idx+1,
                                          current_record_count,
                                          read_time,
