=== modified file 'sql/opt_range.cc'
--- sql/opt_range.cc	2014-07-19 03:54:25 +0000
+++ sql/opt_range.cc	2014-07-19 05:23:54 +0000
@@ -888,6 +888,8 @@ class PARAM : public RANGE_OPT_PARAM
   /* Number of ranges in the last checked tree->key */
   uint n_ranges;
   uint8 first_null_comp; /* first null component if any, 0 - otherwise */
+
+  key_map keys_to_make_select_from;
 };
 
 
@@ -920,7 +922,6 @@ QUICK_RANGE_SELECT *get_quick_select(PAR
                                      SEL_ARG *key_tree, uint mrr_flags, 
                                      uint mrr_buf_size, MEM_ROOT *alloc);
 static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
-                                       key_map keys_to_make_select_from,
                                        bool index_read_must_be_used,
                                        bool update_tbl_stats,
                                        double read_time);
@@ -3006,6 +3007,7 @@ int SQL_SELECT::test_quick_select(THD *t
     param.remove_jump_scans= TRUE;
     param.force_default_mrr= ordered_output;
     param.possible_keys.clear_all();
+    param.keys_to_make_select_from= keys_to_make_select_from;
 
     thd->no_errors=1;				// Don't warn about NULL
     init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
@@ -3125,7 +3127,6 @@ int SQL_SELECT::test_quick_select(THD *t
 
       /* Get best 'range' plan and prepare data for making other plans */
       if ((range_trp= get_key_scans_params(&param, tree,
-                                           keys_to_make_select_from,
                                            FALSE, TRUE,
                                            best_read_time)))
       {
@@ -5154,9 +5155,7 @@ TABLE_READ_PLAN *get_best_disjunct_quick
   {
     DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
                                         "tree in SEL_IMERGE"););
-    key_map dummy;
-    dummy.set_all();
-    if (!(*cur_child= get_key_scans_params(param, *ptree, dummy, TRUE, FALSE, read_time)))
+    if (!(*cur_child= get_key_scans_params(param, *ptree, TRUE, FALSE, read_time)))
     {
       /*
         One of index scans in this index_merge is more expensive than entire
@@ -5477,9 +5476,7 @@ TABLE_READ_PLAN *merge_same_index_scans(
       2. the functions that estimate the cost of a range scan and an
          index merge retrievals are not well calibrated
     */
-    key_map dummy;
-    dummy.set_all();
-    trp= get_key_scans_params(param, *imerge->trees, dummy, FALSE, TRUE,
+    trp= get_key_scans_params(param, *imerge->trees, FALSE, TRUE,
                               read_time);
   }
 
@@ -7202,7 +7199,6 @@ TRP_ROR_INTERSECT *get_best_covering_ror
 */
 
 static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
-                                       key_map keys_to_make_select_from,
                                        bool index_read_must_be_used, 
                                        bool update_tbl_stats,
                                        double read_time)
@@ -7244,7 +7240,7 @@ static TRP_RANGE *get_key_scans_params(P
       if ((*key)->type == SEL_ARG::MAYBE_KEY ||
           (*key)->maybe_flag)
         param->needed_reg->set_bit(keynr);
-      if (!keys_to_make_select_from.is_set(keynr))
+      if (!param->keys_to_make_select_from.is_set(keynr))
         continue;
 
       bool read_index_only= index_read_must_be_used ? TRUE :

=== modified file 'sql/sql_select.cc'
--- sql/sql_select.cc	2014-07-19 03:54:25 +0000
+++ sql/sql_select.cc	2014-07-19 05:45:15 +0000
@@ -20187,7 +20187,7 @@ test_if_skip_sort_order(JOIN_TAB *tab,OR
          !(table->file->index_flags(best_key, 0, 1) & HA_CLUSTERED_INDEX)))
       goto use_filesort;
 
-    if (select &&
+    if (select && // psergey:  why doesn't this use a quick?
         table->quick_keys.is_set(best_key) && best_key != ref_key)
     {
       key_map map;

