184
Iterate through all items in the SELECT clause and replace
185
COUNT(), MIN() and MAX() with constants (if possible).
189
Iterate through all items in the SELECT clause and replace
190
COUNT(), MIN() and MAX() with constants (if possible).
188
193
while ((item= it++))
190
195
if (item->type() == Item::SUM_FUNC_ITEM)
192
197
Item_sum *item_sum= (((Item_sum*) item));
193
switch (item_sum->sum_func()) {
194
case Item_sum::COUNT_FUNC:
196
If the expr in COUNT(expr) can never be null we can change this
197
to the number of rows in the tables if this number is exact and
198
there are no outer joins.
200
if (!conds && !((Item_sum_count*) item)->args[0]->maybe_null &&
201
!outer_tables && maybe_exact_count)
205
if ((count= get_exact_record_count(tables)) == UINT64_MAX)
207
/* Error from handler in counting rows. Don't optimize count() */
211
is_exact_count= 1; // count is now exact
213
((Item_sum_count*) item)->make_const_count((int64_t) count);
214
recalc_const_item= 1;
219
case Item_sum::MIN_FUNC:
222
If MIN(expr) is the first part of a key or if all previous
223
parts of the key is found in the COND, then we can use
224
indexes to find the key.
226
Item *expr=item_sum->args[0];
227
if (expr->real_item()->type() == Item::FIELD_ITEM)
229
unsigned char key_buff[MAX_KEY_LENGTH];
230
table_reference_st ref;
231
uint32_t range_fl, prefix_len;
233
ref.key_buff= key_buff;
234
Item_field *item_field= (Item_field*) (expr->real_item());
235
Table *table= item_field->field->table;
238
Look for a partial key that can be used for optimization.
239
If we succeed, ref.key_length will contain the length of
240
this key, while prefix_len will contain the length of
241
the beginning of this key without field used in MIN().
242
Type of range for the key part for this field will be
243
returned in range_fl.
245
if (table->cursor->inited || (outer_tables & table->map) ||
246
!find_key_for_maxmin(0, &ref, item_field->field, conds,
247
&range_fl, &prefix_len))
252
error= table->cursor->ha_index_init((uint32_t) ref.key, 1);
255
error= table->cursor->index_first(table->record[0]);
259
Use index to replace MIN/MAX functions with their values
260
according to the following rules:
262
1) Insert the minimum non-null values where the WHERE clause still
264
2) a NULL value if there are only NULL values for key_part_k.
265
3) Fail, producing a row of nulls
267
Implementation: Read the smallest value using the search key. If
268
the interval is open, read the next value after the search
269
key. If read fails, and we're looking for a MIN() value for a
270
nullable column, test if there is an exact match for the key.
272
if (!(range_fl & NEAR_MIN))
274
Closed interval: Either The MIN argument is non-nullable, or
275
we have a >= predicate for the MIN argument.
277
error= table->cursor->index_read_map(table->record[0],
279
make_prev_keypart_map(ref.key_parts),
280
HA_READ_KEY_OR_NEXT);
284
Open interval: There are two cases:
285
1) We have only MIN() and the argument column is nullable, or
286
2) there is a > predicate on it, nullability is irrelevant.
287
We need to scan the next bigger record first.
289
error= table->cursor->index_read_map(table->record[0],
291
make_prev_keypart_map(ref.key_parts),
294
If the found record is outside the group formed by the search
295
prefix, or there is no such record at all, check if all
296
records in that group have NULL in the MIN argument
297
column. If that is the case return that NULL.
299
Check if case 1 from above holds. If it does, we should read
302
if (item_field->field->real_maybe_null() &&
303
ref.key_buff[prefix_len] == 1 &&
305
Last keypart (i.e. the argument to MIN) is set to NULL by
306
find_key_for_maxmin only if all other keyparts are bound
307
to constants in a conjunction of equalities. Hence, we
308
can detect this by checking only if the last keypart is
311
(error == HA_ERR_KEY_NOT_FOUND ||
312
key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
314
assert(item_field->field->real_maybe_null());
315
error= table->cursor->index_read_map(table->record[0],
317
make_prev_keypart_map(ref.key_parts),
322
/* Verify that the read tuple indeed matches the search key */
323
if (!error && reckey_in_range(0, &ref, item_field->field,
324
conds, range_fl, prefix_len))
325
error= HA_ERR_KEY_NOT_FOUND;
329
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
331
table->cursor->ha_index_end();
334
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
335
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
336
/* HA_ERR_LOCK_DEADLOCK or some other error */
337
table->print_error(error, MYF(0));
340
removed_tables|= table->map;
342
else if (!expr->const_item() || !is_exact_count)
345
The optimization is not applicable in both cases:
346
(a) 'expr' is a non-constant expression. Then we can't
347
replace 'expr' by a constant.
348
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
349
NULL if the query does not return any rows. Thus, if we are not
350
able to determine if the query returns any rows, we can't apply
351
the optimization and replace MIN/MAX with a constant.
358
/* If count == 0, then we know that is_exact_count == true. */
359
((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
362
((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
363
((Item_sum_min*) item_sum)->make_const();
364
recalc_const_item= 1;
367
case Item_sum::MAX_FUNC:
370
If MAX(expr) is the first part of a key or if all previous
371
parts of the key is found in the COND, then we can use
372
indexes to find the key.
374
Item *expr=item_sum->args[0];
375
if (expr->real_item()->type() == Item::FIELD_ITEM)
377
unsigned char key_buff[MAX_KEY_LENGTH];
378
table_reference_st ref;
379
uint32_t range_fl, prefix_len;
381
ref.key_buff= key_buff;
382
Item_field *item_field= (Item_field*) (expr->real_item());
383
Table *table= item_field->field->table;
386
Look for a partial key that can be used for optimization.
387
If we succeed, ref.key_length will contain the length of
388
this key, while prefix_len will contain the length of
389
the beginning of this key without field used in MAX().
390
Type of range for the key part for this field will be
391
returned in range_fl.
393
if (table->cursor->inited || (outer_tables & table->map) ||
394
!find_key_for_maxmin(1, &ref, item_field->field, conds,
395
&range_fl, &prefix_len))
400
error= table->cursor->ha_index_init((uint32_t) ref.key, 1);
403
error= table->cursor->index_last(table->record[0]);
405
error= table->cursor->index_read_map(table->record[0], key_buff,
406
make_prev_keypart_map(ref.key_parts),
407
range_fl & NEAR_MAX ?
409
HA_READ_PREFIX_LAST_OR_PREV);
410
if (!error && reckey_in_range(1, &ref, item_field->field,
411
conds, range_fl, prefix_len))
412
error= HA_ERR_KEY_NOT_FOUND;
416
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
418
table->cursor->ha_index_end();
421
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
422
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
423
/* HA_ERR_LOCK_DEADLOCK or some other error */
424
table->print_error(error, MYF(ME_FATALERROR));
427
removed_tables|= table->map;
429
else if (!expr->const_item() || !is_exact_count)
432
The optimization is not applicable in both cases:
433
(a) 'expr' is a non-constant expression. Then we can't
434
replace 'expr' by a constant.
435
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
436
NULL if the query does not return any rows. Thus, if we are not
437
able to determine if the query returns any rows, we can't apply
438
the optimization and replace MIN/MAX with a constant.
445
/* If count != 1, then we know that is_exact_count == true. */
446
((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
449
((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
450
((Item_sum_max*) item_sum)->make_const();
451
recalc_const_item= 1;
198
switch (item_sum->sum_func())
200
case Item_sum::COUNT_FUNC:
202
If the expr in COUNT(expr) can never be null we can change this
203
to the number of rows in the tables if this number is exact and
204
there are no outer joins.
206
if (! conds && ! ((Item_sum_count*) item)->args[0]->maybe_null &&
207
! outer_tables && maybe_exact_count)
209
if (! is_exact_count)
211
if ((count= get_exact_record_count(tables)) == UINT64_MAX)
213
/* Error from handler in counting rows. Don't optimize count() */
217
is_exact_count= 1; // count is now exact
219
((Item_sum_count*) item)->make_const_count((int64_t) count);
220
recalc_const_item= 1;
227
case Item_sum::MIN_FUNC:
230
If MIN(expr) is the first part of a key or if all previous
231
parts of the key is found in the COND, then we can use
232
indexes to find the key.
234
Item *expr=item_sum->args[0];
235
if (expr->real_item()->type() == Item::FIELD_ITEM)
237
unsigned char key_buff[MAX_KEY_LENGTH];
238
table_reference_st ref;
239
uint32_t range_fl, prefix_len;
241
ref.key_buff= key_buff;
242
Item_field *item_field= (Item_field*) (expr->real_item());
243
Table *table= item_field->field->table;
246
Look for a partial key that can be used for optimization.
247
If we succeed, ref.key_length will contain the length of
248
this key, while prefix_len will contain the length of
249
the beginning of this key without field used in MIN().
250
Type of range for the key part for this field will be
251
returned in range_fl.
253
if (table->cursor->inited ||
254
(outer_tables & table->map) ||
255
! find_key_for_maxmin(0,
265
error= table->cursor->ha_index_init(static_cast<uint32_t>(ref.key), 1);
267
if (! ref.key_length)
269
error= table->cursor->index_first(table->record[0]);
274
Use index to replace MIN/MAX functions with their values
275
according to the following rules:
277
1) Insert the minimum non-null values where the WHERE clause still
279
2) a NULL value if there are only NULL values for key_part_k.
280
3) Fail, producing a row of nulls
282
Implementation: Read the smallest value using the search key. If
283
the interval is open, read the next value after the search
284
key. If read fails, and we're looking for a MIN() value for a
285
nullable column, test if there is an exact match for the key.
287
if (! (range_fl & NEAR_MIN))
289
Closed interval: Either The MIN argument is non-nullable, or
290
we have a >= predicate for the MIN argument.
292
error= table->cursor->index_read_map(table->record[0],
294
make_prev_keypart_map(ref.key_parts),
295
HA_READ_KEY_OR_NEXT);
299
Open interval: There are two cases:
300
1) We have only MIN() and the argument column is nullable, or
301
2) there is a > predicate on it, nullability is irrelevant.
302
We need to scan the next bigger record first.
304
error= table->cursor->index_read_map(table->record[0],
306
make_prev_keypart_map(ref.key_parts),
309
If the found record is outside the group formed by the search
310
prefix, or there is no such record at all, check if all
311
records in that group have NULL in the MIN argument
312
column. If that is the case return that NULL.
314
Check if case 1 from above holds. If it does, we should read
317
if (item_field->field->real_maybe_null() &&
318
ref.key_buff[prefix_len] == 1 &&
320
Last keypart (i.e. the argument to MIN) is set to NULL by
321
find_key_for_maxmin only if all other keyparts are bound
322
to constants in a conjunction of equalities. Hence, we
323
can detect this by checking only if the last keypart is
326
(error == HA_ERR_KEY_NOT_FOUND ||
327
key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
329
assert(item_field->field->real_maybe_null());
330
error= table->cursor->index_read_map(table->record[0],
332
make_prev_keypart_map(ref.key_parts),
337
/* Verify that the read tuple indeed matches the search key */
346
error= HA_ERR_KEY_NOT_FOUND;
351
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
353
table->cursor->ha_index_end();
356
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
358
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
360
/* HA_ERR_LOCK_DEADLOCK or some other error */
361
table->print_error(error, MYF(0));
364
removed_tables|= table->map;
366
else if (! expr->const_item() || ! is_exact_count)
369
The optimization is not applicable in both cases:
370
(a) 'expr' is a non-constant expression. Then we can't
371
replace 'expr' by a constant.
372
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
373
NULL if the query does not return any rows. Thus, if we are not
374
able to determine if the query returns any rows, we can't apply
375
the optimization and replace MIN/MAX with a constant.
382
/* If count == 0, then we know that is_exact_count == true. */
383
((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
387
((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
389
((Item_sum_min*) item_sum)->make_const();
390
recalc_const_item= 1;
393
case Item_sum::MAX_FUNC:
396
If MAX(expr) is the first part of a key or if all previous
397
parts of the key is found in the COND, then we can use
398
indexes to find the key.
400
Item *expr= item_sum->args[0];
401
if (expr->real_item()->type() == Item::FIELD_ITEM)
403
unsigned char key_buff[MAX_KEY_LENGTH];
404
table_reference_st ref;
405
uint32_t range_fl, prefix_len;
407
ref.key_buff= key_buff;
408
Item_field *item_field= (Item_field*) (expr->real_item());
409
Table *table= item_field->field->table;
412
Look for a partial key that can be used for optimization.
413
If we succeed, ref.key_length will contain the length of
414
this key, while prefix_len will contain the length of
415
the beginning of this key without field used in MAX().
416
Type of range for the key part for this field will be
417
returned in range_fl.
419
if (table->cursor->inited ||
420
(outer_tables & table->map) ||
421
! find_key_for_maxmin(1,
431
error= table->cursor->ha_index_init(static_cast<uint32_t>(ref.key), 1);
433
if (! ref.key_length)
435
error= table->cursor->index_last(table->record[0]);
439
error= table->cursor->index_read_map(table->record[0],
441
make_prev_keypart_map(ref.key_parts),
442
range_fl & NEAR_MAX ?
444
HA_READ_PREFIX_LAST_OR_PREV);
454
error= HA_ERR_KEY_NOT_FOUND;
459
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
461
table->cursor->ha_index_end();
464
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
466
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
468
/* HA_ERR_LOCK_DEADLOCK or some other error */
469
table->print_error(error, MYF(ME_FATALERROR));
472
removed_tables|= table->map;
474
else if (! expr->const_item() || ! is_exact_count)
477
The optimization is not applicable in both cases:
478
(a) 'expr' is a non-constant expression. Then we can't
479
replace 'expr' by a constant.
480
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
481
NULL if the query does not return any rows. Thus, if we are not
482
able to determine if the query returns any rows, we can't apply
483
the optimization and replace MIN/MAX with a constant.
490
/* If count != 1, then we know that is_exact_count == true. */
491
((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
495
((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
497
((Item_sum_max*) item_sum)->make_const();
498
recalc_const_item= 1;
459
506
else if (const_result)
461
508
if (recalc_const_item)
462
510
item->update_used_tables();
463
if (!item->const_item())
512
if (! item->const_item())
468
If we have a where clause, we can only ignore searching in the
469
tables if MIN/MAX optimisation replaced all used tables
470
We do not use replaced values in case of:
471
SELECT MIN(key) FROM table_1, empty_table
472
removed_tables is != 0 if we have used MIN() or MAX().
519
If we have a where clause, we can only ignore searching in the
520
tables if MIN/MAX optimisation replaced all used tables
521
We do not use replaced values in case of:
522
SELECT MIN(key) FROM table_1, empty_table
523
removed_tables is != 0 if we have used MIN() or MAX().
474
525
if (removed_tables && used_tables != removed_tables)
475
527
const_result= 0; // We didn't remove all tables
476
529
return const_result;
481
Test if the predicate compares a field with constants.
483
@param func_item Predicate item
484
@param[out] args Here we store the field followed by constants
485
@param[out] inv_order Is set to 1 if the predicate is of the form
489
0 func_item is a simple predicate: a field is compared with
495
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order)
533
bool optimizer::simple_pred(Item_func *func_item, Item **args, bool *inv_order)
499
537
switch (func_item->argument_count()) {