| 33107 | */ |
| 33108 | |
| 33109 | static bool get_range_limit_read_cost(const POSITION *pos, |
| 33110 | const TABLE *table, |
| 33111 | uint keynr, |
| 33112 | ha_rows rows_limit_arg, |
| 33113 | ha_rows rows_to_scan, |
| 33114 | double *read_cost, |
| 33115 | double *read_rows) |
| 33116 | { |
| 33117 | double rows_limit= rows2double(rows_limit_arg); |
| 33118 | if (table->opt_range_keys.is_set(keynr)) |
| 33119 | { |
| 33120 | /* |
| 33121 | Start from quick select's rows and cost. These are always cheaper than |
| 33122 | full index scan/cost. |
| 33123 | */ |
| 33124 | double best_rows, range_rows; |
| 33125 | double range_cost= (double) table->opt_range[keynr].cost.fetch_cost(); |
| 33126 | best_rows= range_rows= (double) table->opt_range[keynr].rows; |
| 33127 | |
| 33128 | if (pos) |
| 33129 | { |
| 33130 | double cond_selectivity; |
| 33131 | /* |
| 33132 | Take into count table selectivity as the number of accepted |
| 33133 | rows for this table will be 'records_out'. |
| 33134 | |
| 33135 | For example: |
| 33136 | key1 BETWEEN 10 AND 1000 AND key2 BETWEEN 10 AND 20 |
| 33137 | |
| 33138 | If we are trying to do an ORDER BY on key1, we have to take into |
| 33139 | account that using key2 we have to examine much fewer rows. |
| 33140 | */ |
| 33141 | best_rows= pos->records_out; // Best rows with any key/keys |
| 33142 | /* |
| 33143 | We assign "double range_rows" from integer #rows a few lines above |
| 33144 | so comparison with 0.0 makes sense |
| 33145 | */ |
| 33146 | if (range_rows > 0.0) |
| 33147 | cond_selectivity= best_rows / range_rows; |
| 33148 | else |
| 33149 | cond_selectivity= 1.0; |
| 33150 | |
| 33151 | DBUG_ASSERT(cond_selectivity <= 1.000000001); |
| 33152 | set_if_smaller(cond_selectivity, 1.0); |
| 33153 | |
| 33154 | /* |
| 33155 | We have to examine more rows in the proportion to the selectivity of the |
| 33156 | the table |
| 33157 | */ |
| 33158 | rows_limit= rows_limit / cond_selectivity; |
| 33159 | } |
| 33160 | |
| 33161 | if (best_rows > rows_limit) |
| 33162 | { |
| 33163 | /* |
| 33164 | LIMIT clause specifies that we will need to read fewer records than |
| 33165 | quick select will return. Assume that quick select's cost is |
| 33166 | proportional to the number of records we need to return (e.g. if we |