| 7992 | */ |
| 7993 | |
| 7994 | void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array) |
| 7995 | { |
| 7996 | KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*); |
| 7997 | |
| 7998 | for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++) |
| 7999 | { |
| 8000 | table_map map; |
| 8001 | /* |
| 8002 | If we find a ref, assume this table matches a proportional |
| 8003 | part of this table. |
| 8004 | For example 100 records matching a table with 5000 records |
| 8005 | gives 5000/100 = 50 records per key |
| 8006 | Constant tables are ignored. |
| 8007 | To avoid bad matches, we don't make ref_table_rows less than 100. |
| 8008 | */ |
| 8009 | keyuse->ref_table_rows= ~(ha_rows) 0; // If no ref |
| 8010 | if (keyuse->used_tables & |
| 8011 | (map= (keyuse->used_tables & ~join->const_table_map & |
| 8012 | ~OUTER_REF_TABLE_BIT))) |
| 8013 | { |
| 8014 | uint n_tables= my_count_bits(map); |
| 8015 | if (n_tables == 1) // Only one table |
| 8016 | { |
| 8017 | DBUG_ASSERT(!(map & PSEUDO_TABLE_BITS)); // Must be a real table |
| 8018 | Table_map_iterator it(map); |
| 8019 | int tablenr= it.next_bit(); |
| 8020 | DBUG_ASSERT(tablenr != Table_map_iterator::BITMAP_END); |
| 8021 | TABLE *tmp_table=join->table[tablenr]; |
| 8022 | if (tmp_table) // already created |
| 8023 | keyuse->ref_table_rows= MY_MAX(tmp_table->file->stats.records, 100); |
| 8024 | } |
| 8025 | } |
| 8026 | /* |
| 8027 | Outer reference (external field) is constant for single executing |
| 8028 | of subquery |
| 8029 | */ |
| 8030 | if (keyuse->used_tables == OUTER_REF_TABLE_BIT) |
| 8031 | keyuse->ref_table_rows= 1; |
| 8032 | } |
| 8033 | } |
| 8034 | |
| 8035 | /** |
| 8036 | Check for the presence of AGGFN(DISTINCT a) queries that may be subject |
no test coverage detected