@@ -2904,6 +2904,27 @@ make_join_readinfo(JOIN *join, ulonglong options, uint no_jbuf_after)
29042904 tab->read_first_record = join_read_first;
29052905 tab->type =JT_INDEX_SCAN; // Read with index_first / index_next
29062906 }
2907+ else if (!(tab->select && tab->select ->quick ))
2908+ {
2909+ DBUG_ASSERT (table->covering_keys .is_clear_all ());
2910+ if (!tab->do_loosescan ())
2911+ {
2912+ key_map clustering_keys;
2913+ for (uint i= 0 ; i < table->s ->keys ; i++)
2914+ {
2915+ if (tab->keys .is_set (i)
2916+ && table->file ->index_flags (i, 0 , 0 ) & HA_CLUSTERED_INDEX)
2917+ clustering_keys.set_bit (i);
2918+ }
2919+ uint index= find_shortest_key (table, &clustering_keys);
2920+ if (index != MAX_KEY)
2921+ {
2922+ tab->index = index;
2923+ tab->read_first_record = join_read_first;
2924+ tab->type = JT_INDEX_SCAN;
2925+ }
2926+ }
2927+ }
29072928 }
29082929 if (tab->select && tab->select ->quick &&
29092930 tab->select ->quick ->index != MAX_KEY && ! tab->table ->key_read )
@@ -3638,13 +3659,14 @@ uint find_shortest_key(TABLE *table, const key_map *usable_keys)
36383659 {
36393660 /*
36403661 If the primary key is clustered and found shorter key covers all table
3641- fields then primary key scan normally would be faster because amount of
3642- data to scan is the same but PK is clustered.
3662+ fields and is not clustering then primary key scan normally would be
3663+ faster because amount of data to scan is the same but PK is clustered.
36433664 It's safe to compare key parts with table fields since duplicate key
36443665 parts aren't allowed.
36453666 */
36463667 if (best == MAX_KEY ||
3647- table->key_info [best].user_defined_key_parts >= table->s ->fields )
3668+ ((table->key_info [best].user_defined_key_parts >= table->s ->fields )
3669+ && !(table->file ->index_flags (best, 0 , 0 ) & HA_CLUSTERED_INDEX)))
36483670 best= usable_clustered_pk;
36493671 }
36503672 return best;
@@ -4092,7 +4114,8 @@ test_if_skip_sort_order(JOIN_TAB *tab, ORDER *order, ha_rows select_limit,
40924114 (tab->type == JT_ALL &&
40934115 tab->join ->primary_tables > tab->join ->const_tables + 1 ) &&
40944116 ((unsigned ) best_key != table->s ->primary_key ||
4095- !table->file ->primary_key_is_clustered ()))
4117+ !table->file ->primary_key_is_clustered ()) &&
4118+ !(best_key >= 0 && (table->file ->index_flags (best_key, 0 , 0 ) & HA_CLUSTERED_INDEX)))
40964119 {
40974120 can_skip_sorting= false ;
40984121 goto fix_ICP;
@@ -5548,7 +5571,9 @@ test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER *order, TABLE *table,
55485571
55495572 bool is_covering= table->covering_keys .is_set (nr) ||
55505573 (nr == table->s ->primary_key &&
5551- table->file ->primary_key_is_clustered ());
5574+ table->file ->primary_key_is_clustered ()) ||
5575+ (table->file ->index_flags (nr, 0 , 0 )
5576+ & HA_CLUSTERED_INDEX);
55525577
55535578 /*
55545579 Don't use an index scan with ORDER BY without limit.
0 commit comments