73
92
UINT64_MAX Error: Could not calculate number of rows
74
93
# Multiplication of number of rows in all tables
77
static uint64_t get_exact_record_count(TABLE_LIST *tables)
95
static uint64_t get_exact_record_count(TableList *tables)
80
for (TABLE_LIST *tl= tables; tl; tl= tl->next_leaf)
98
for (TableList *tl= tables; tl; tl= tl->next_leaf)
82
ha_rows tmp= tl->table->file->records();
100
ha_rows tmp= tl->table->cursor->records();
83
101
if ((tmp == HA_POS_ERROR))
84
103
return UINT64_MAX;
92
Substitutes constants for some COUNT(), MIN() and MAX() functions.
94
@param tables list of leaves of join table tree
95
@param all_fields All fields to be returned
96
@param conds WHERE clause
99
This function is only called for queries with sum functions and no
105
1 if all items were resolved
107
HA_ERR_KEY_NOT_FOUND on impossible conditions
109
HA_ERR_... if a deadlock or a lock wait timeout happens, for example
112
int opt_sum_query(TABLE_LIST *tables, List<Item> &all_fields,COND *conds)
111
int optimizer::sum_query(TableList *tables, List<Item> &all_fields, COND *conds)
114
113
List_iterator_fast<Item> it(all_fields);
115
114
int const_result= 1;
116
bool recalc_const_item= 0;
115
bool recalc_const_item= false;
117
116
uint64_t count= 1;
118
bool is_exact_count= true, maybe_exact_count= true;
119
table_map removed_tables= 0, outer_tables= 0, used_tables= 0;
117
bool is_exact_count= true;
118
bool maybe_exact_count= true;
119
table_map removed_tables= 0;
120
table_map outer_tables= 0;
121
table_map used_tables= 0;
120
122
table_map where_tables= 0;
125
128
where_tables= conds->used_tables();
128
Analyze outer join dependencies, and, if possible, compute the number
131
for (TABLE_LIST *tl= tables; tl; tl= tl->next_leaf)
132
Analyze outer join dependencies, and, if possible, compute the number
135
for (TableList *tl= tables; tl; tl= tl->next_leaf)
133
TABLE_LIST *embedded;
134
for (embedded= tl ; embedded; embedded= embedded->embedding)
137
TableList *embedded= NULL;
138
for (embedded= tl; embedded; embedded= embedded->embedding)
136
140
if (embedded->on_expr)
140
/* Don't replace expression on a table that is part of an outer join */
144
/* Don't replace expression on a table that is part of an outer join */
142
146
outer_tables|= tl->table->map;
145
We can't optimise LEFT JOIN in cases where the WHERE condition
146
restricts the table that is used, like in:
147
SELECT MAX(t1.a) FROM t1 LEFT JOIN t2 join-condition
148
WHERE t2.field IS NULL;
149
We can't optimise LEFT JOIN in cases where the WHERE condition
150
restricts the table that is used, like in:
151
SELECT MAX(t1.a) FROM t1 LEFT JOIN t2 join-condition
152
WHERE t2.field IS NULL;
150
154
if (tl->table->map & where_tables)
154
159
used_tables|= tl->table->map;
157
If the storage manager of 'tl' gives exact row count as part of
158
statistics (cheap), compute the total number of rows. If there are
159
no outer table dependencies, this count may be used as the real count.
160
Schema tables are filled after this function is invoked, so we can't
163
if (!(tl->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) ||
163
If the storage manager of 'tl' gives exact row count as part of
164
statistics (cheap), compute the total number of rows. If there are
165
no outer table dependencies, this count may be used as the real count.
166
Schema tables are filled after this function is invoked, so we can't
169
if (! (tl->table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)))
166
maybe_exact_count&= test(!tl->schema_table &&
167
(tl->table->file->ha_table_flags() &
171
maybe_exact_count&= test(tl->table->cursor->getEngine()->check_flag(HTON_BIT_HAS_RECORDS));
169
172
is_exact_count= false;
170
count= 1; // ensure count != 0
173
count= 1; // ensure count != 0
174
error= tl->table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
177
error= tl->table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
177
tl->table->file->print_error(error, MYF(ME_FATALERROR));
180
tl->table->print_error(error, MYF(ME_FATALERROR));
180
count*= tl->table->file->stats.records;
183
count*= tl->table->cursor->stats.records;
185
Iterate through all items in the SELECT clause and replace
186
COUNT(), MIN() and MAX() with constants (if possible).
188
Iterate through all items in the SELECT clause and replace
189
COUNT(), MIN() and MAX() with constants (if possible).
189
192
while ((item= it++))
191
194
if (item->type() == Item::SUM_FUNC_ITEM)
193
196
Item_sum *item_sum= (((Item_sum*) item));
194
switch (item_sum->sum_func()) {
195
case Item_sum::COUNT_FUNC:
197
If the expr in COUNT(expr) can never be null we can change this
198
to the number of rows in the tables if this number is exact and
199
there are no outer joins.
201
if (!conds && !((Item_sum_count*) item)->args[0]->maybe_null &&
202
!outer_tables && maybe_exact_count)
206
if ((count= get_exact_record_count(tables)) == UINT64_MAX)
208
/* Error from handler in counting rows. Don't optimize count() */
212
is_exact_count= 1; // count is now exact
214
((Item_sum_count*) item)->make_const((int64_t) count);
215
recalc_const_item= 1;
220
case Item_sum::MIN_FUNC:
197
switch (item_sum->sum_func())
223
If MIN(expr) is the first part of a key or if all previous
224
parts of the key is found in the COND, then we can use
225
indexes to find the key.
227
Item *expr=item_sum->args[0];
228
if (expr->real_item()->type() == Item::FIELD_ITEM)
230
uchar key_buff[MAX_KEY_LENGTH];
232
uint range_fl, prefix_len;
234
ref.key_buff= key_buff;
235
Item_field *item_field= (Item_field*) (expr->real_item());
236
TABLE *table= item_field->field->table;
239
Look for a partial key that can be used for optimization.
240
If we succeed, ref.key_length will contain the length of
241
this key, while prefix_len will contain the length of
242
the beginning of this key without field used in MIN().
243
Type of range for the key part for this field will be
244
returned in range_fl.
246
if (table->file->inited || (outer_tables & table->map) ||
247
!find_key_for_maxmin(0, &ref, item_field->field, conds,
248
&range_fl, &prefix_len))
253
error= table->file->ha_index_init((uint) ref.key, 1);
256
error= table->file->index_first(table->record[0]);
260
Use index to replace MIN/MAX functions with their values
261
according to the following rules:
263
1) Insert the minimum non-null values where the WHERE clause still
265
2) a NULL value if there are only NULL values for key_part_k.
266
3) Fail, producing a row of nulls
268
Implementation: Read the smallest value using the search key. If
269
the interval is open, read the next value after the search
270
key. If read fails, and we're looking for a MIN() value for a
271
nullable column, test if there is an exact match for the key.
273
if (!(range_fl & NEAR_MIN))
275
Closed interval: Either The MIN argument is non-nullable, or
276
we have a >= predicate for the MIN argument.
278
error= table->file->index_read_map(table->record[0],
280
make_prev_keypart_map(ref.key_parts),
281
HA_READ_KEY_OR_NEXT);
199
case Item_sum::COUNT_FUNC:
201
If the expr in COUNT(expr) can never be null we can change this
202
to the number of rows in the tables if this number is exact and
203
there are no outer joins.
205
if (! conds && ! ((Item_sum_count*) item)->args[0]->maybe_null &&
206
! outer_tables && maybe_exact_count)
208
if (! is_exact_count)
285
Open interval: There are two cases:
286
1) We have only MIN() and the argument column is nullable, or
287
2) there is a > predicate on it, nullability is irrelevant.
288
We need to scan the next bigger record first.
290
error= table->file->index_read_map(table->record[0],
292
make_prev_keypart_map(ref.key_parts),
295
If the found record is outside the group formed by the search
296
prefix, or there is no such record at all, check if all
297
records in that group have NULL in the MIN argument
298
column. If that is the case return that NULL.
300
Check if case 1 from above holds. If it does, we should read
303
if (item_field->field->real_maybe_null() &&
304
ref.key_buff[prefix_len] == 1 &&
306
Last keypart (i.e. the argument to MIN) is set to NULL by
307
find_key_for_maxmin only if all other keyparts are bound
308
to constants in a conjunction of equalities. Hence, we
309
can detect this by checking only if the last keypart is
312
(error == HA_ERR_KEY_NOT_FOUND ||
313
key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
210
if ((count= get_exact_record_count(tables)) == UINT64_MAX)
315
assert(item_field->field->real_maybe_null());
316
error= table->file->index_read_map(table->record[0],
318
make_prev_keypart_map(ref.key_parts),
212
/* Error from handler in counting rows. Don't optimize count() */
216
is_exact_count= 1; // count is now exact
323
/* Verify that the read tuple indeed matches the search key */
324
if (!error && reckey_in_range(0, &ref, item_field->field,
325
conds, range_fl, prefix_len))
326
error= HA_ERR_KEY_NOT_FOUND;
330
table->file->extra(HA_EXTRA_NO_KEYREAD);
332
table->file->ha_index_end();
335
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
336
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
337
/* HA_ERR_LOCK_DEADLOCK or some other error */
338
table->file->print_error(error, MYF(0));
341
removed_tables|= table->map;
343
else if (!expr->const_item() || !is_exact_count)
346
The optimization is not applicable in both cases:
347
(a) 'expr' is a non-constant expression. Then we can't
348
replace 'expr' by a constant.
349
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
350
NULL if the query does not return any rows. Thus, if we are not
351
able to determine if the query returns any rows, we can't apply
352
the optimization and replace MIN/MAX with a constant.
359
/* If count == 0, then we know that is_exact_count == true. */
360
((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
363
((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
364
((Item_sum_min*) item_sum)->make_const();
365
recalc_const_item= 1;
368
case Item_sum::MAX_FUNC:
371
If MAX(expr) is the first part of a key or if all previous
372
parts of the key is found in the COND, then we can use
373
indexes to find the key.
375
Item *expr=item_sum->args[0];
376
if (expr->real_item()->type() == Item::FIELD_ITEM)
378
uchar key_buff[MAX_KEY_LENGTH];
380
uint range_fl, prefix_len;
382
ref.key_buff= key_buff;
383
Item_field *item_field= (Item_field*) (expr->real_item());
384
TABLE *table= item_field->field->table;
387
Look for a partial key that can be used for optimization.
388
If we succeed, ref.key_length will contain the length of
389
this key, while prefix_len will contain the length of
390
the beginning of this key without field used in MAX().
391
Type of range for the key part for this field will be
392
returned in range_fl.
394
if (table->file->inited || (outer_tables & table->map) ||
395
!find_key_for_maxmin(1, &ref, item_field->field, conds,
396
&range_fl, &prefix_len))
401
error= table->file->ha_index_init((uint) ref.key, 1);
404
error= table->file->index_last(table->record[0]);
218
((Item_sum_count*) item)->make_const_count((int64_t) count);
219
recalc_const_item= 1;
406
error= table->file->index_read_map(table->record[0], key_buff,
407
make_prev_keypart_map(ref.key_parts),
408
range_fl & NEAR_MAX ?
410
HA_READ_PREFIX_LAST_OR_PREV);
411
if (!error && reckey_in_range(1, &ref, item_field->field,
412
conds, range_fl, prefix_len))
413
error= HA_ERR_KEY_NOT_FOUND;
417
table->file->extra(HA_EXTRA_NO_KEYREAD);
419
table->file->ha_index_end();
422
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
423
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
424
/* HA_ERR_LOCK_DEADLOCK or some other error */
425
table->file->print_error(error, MYF(ME_FATALERROR));
428
removed_tables|= table->map;
430
else if (!expr->const_item() || !is_exact_count)
433
The optimization is not applicable in both cases:
434
(a) 'expr' is a non-constant expression. Then we can't
435
replace 'expr' by a constant.
436
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
437
NULL if the query does not return any rows. Thus, if we are not
438
able to determine if the query returns any rows, we can't apply
439
the optimization and replace MIN/MAX with a constant.
226
case Item_sum::MIN_FUNC:
229
If MIN(expr) is the first part of a key or if all previous
230
parts of the key is found in the COND, then we can use
231
indexes to find the key.
233
Item *expr=item_sum->args[0];
234
if (expr->real_item()->type() == Item::FIELD_ITEM)
236
unsigned char key_buff[MAX_KEY_LENGTH];
237
table_reference_st ref;
238
uint32_t range_fl, prefix_len;
240
ref.key_buff= key_buff;
241
Item_field *item_field= (Item_field*) (expr->real_item());
242
Table *table= item_field->field->table;
245
Look for a partial key that can be used for optimization.
246
If we succeed, ref.key_length will contain the length of
247
this key, while prefix_len will contain the length of
248
the beginning of this key without field used in MIN().
249
Type of range for the key part for this field will be
250
returned in range_fl.
252
if (table->cursor->inited ||
253
(outer_tables & table->map) ||
254
! find_key_for_maxmin(0,
264
error= table->cursor->ha_index_init(static_cast<uint32_t>(ref.key), 1);
266
if (! ref.key_length)
268
error= table->cursor->index_first(table->record[0]);
273
Use index to replace MIN/MAX functions with their values
274
according to the following rules:
276
1) Insert the minimum non-null values where the WHERE clause still
278
2) a NULL value if there are only NULL values for key_part_k.
279
3) Fail, producing a row of nulls
281
Implementation: Read the smallest value using the search key. If
282
the interval is open, read the next value after the search
283
key. If read fails, and we're looking for a MIN() value for a
284
nullable column, test if there is an exact match for the key.
286
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);
300
Open interval: There are two cases:
301
1) We have only MIN() and the argument column is nullable, or
302
2) there is a > predicate on it, nullability is irrelevant.
303
We need to scan the next bigger record first.
305
error= table->cursor->index_read_map(table->record[0],
307
make_prev_keypart_map(ref.key_parts),
310
If the found record is outside the group formed by the search
311
prefix, or there is no such record at all, check if all
312
records in that group have NULL in the MIN argument
313
column. If that is the case return that NULL.
315
Check if case 1 from above holds. If it does, we should read
318
if (item_field->field->real_maybe_null() &&
319
ref.key_buff[prefix_len] == 1 &&
321
Last keypart (i.e. the argument to MIN) is set to NULL by
322
find_key_for_maxmin only if all other keyparts are bound
323
to constants in a conjunction of equalities. Hence, we
324
can detect this by checking only if the last keypart is
327
(error == HA_ERR_KEY_NOT_FOUND ||
328
key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
330
assert(item_field->field->real_maybe_null());
331
error= table->cursor->index_read_map(table->record[0],
333
make_prev_keypart_map(ref.key_parts),
338
/* Verify that the read tuple indeed matches the search key */
347
error= HA_ERR_KEY_NOT_FOUND;
352
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
354
table->cursor->ha_index_end();
357
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
359
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
361
/* HA_ERR_LOCK_DEADLOCK or some other error */
362
table->print_error(error, MYF(0));
365
removed_tables|= table->map;
367
else if (! expr->const_item() || ! is_exact_count)
370
The optimization is not applicable in both cases:
371
(a) 'expr' is a non-constant expression. Then we can't
372
replace 'expr' by a constant.
373
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
374
NULL if the query does not return any rows. Thus, if we are not
375
able to determine if the query returns any rows, we can't apply
376
the optimization and replace MIN/MAX with a constant.
383
/* If count == 0, then we know that is_exact_count == true. */
384
((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
388
((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
390
((Item_sum_min*) item_sum)->make_const();
391
recalc_const_item= 1;
394
case Item_sum::MAX_FUNC:
397
If MAX(expr) is the first part of a key or if all previous
398
parts of the key is found in the COND, then we can use
399
indexes to find the key.
401
Item *expr= item_sum->args[0];
402
if (expr->real_item()->type() == Item::FIELD_ITEM)
404
unsigned char key_buff[MAX_KEY_LENGTH];
405
table_reference_st ref;
406
uint32_t range_fl, prefix_len;
408
ref.key_buff= key_buff;
409
Item_field *item_field= (Item_field*) (expr->real_item());
410
Table *table= item_field->field->table;
413
Look for a partial key that can be used for optimization.
414
If we succeed, ref.key_length will contain the length of
415
this key, while prefix_len will contain the length of
416
the beginning of this key without field used in MAX().
417
Type of range for the key part for this field will be
418
returned in range_fl.
420
if (table->cursor->inited ||
421
(outer_tables & table->map) ||
422
! find_key_for_maxmin(1,
432
error= table->cursor->ha_index_init(static_cast<uint32_t>(ref.key), 1);
434
if (! ref.key_length)
436
error= table->cursor->index_last(table->record[0]);
440
error= table->cursor->index_read_map(table->record[0],
442
make_prev_keypart_map(ref.key_parts),
443
range_fl & NEAR_MAX ?
445
HA_READ_PREFIX_LAST_OR_PREV);
455
error= HA_ERR_KEY_NOT_FOUND;
460
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
462
table->cursor->ha_index_end();
465
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
467
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
469
/* HA_ERR_LOCK_DEADLOCK or some other error */
470
table->print_error(error, MYF(ME_FATALERROR));
473
removed_tables|= table->map;
475
else if (! expr->const_item() || ! is_exact_count)
478
The optimization is not applicable in both cases:
479
(a) 'expr' is a non-constant expression. Then we can't
480
replace 'expr' by a constant.
481
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
482
NULL if the query does not return any rows. Thus, if we are not
483
able to determine if the query returns any rows, we can't apply
484
the optimization and replace MIN/MAX with a constant.
491
/* If count != 1, then we know that is_exact_count == true. */
492
((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
496
((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
498
((Item_sum_max*) item_sum)->make_const();
499
recalc_const_item= 1;
446
/* If count != 1, then we know that is_exact_count == true. */
447
((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
450
((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
451
((Item_sum_max*) item_sum)->make_const();
452
recalc_const_item= 1;
460
507
else if (const_result)
462
509
if (recalc_const_item)
463
511
item->update_used_tables();
464
if (!item->const_item())
513
if (! item->const_item())
469
If we have a where clause, we can only ignore searching in the
470
tables if MIN/MAX optimisation replaced all used tables
471
We do not use replaced values in case of:
472
SELECT MIN(key) FROM table_1, empty_table
473
removed_tables is != 0 if we have used MIN() or MAX().
520
If we have a where clause, we can only ignore searching in the
521
tables if MIN/MAX optimisation replaced all used tables
522
We do not use replaced values in case of:
523
SELECT MIN(key) FROM table_1, empty_table
524
removed_tables is != 0 if we have used MIN() or MAX().
475
526
if (removed_tables && used_tables != removed_tables)
476
528
const_result= 0; // We didn't remove all tables
477
530
return const_result;
482
Test if the predicate compares a field with constants.
484
@param func_item Predicate item
485
@param[out] args Here we store the field followed by constants
486
@param[out] inv_order Is set to 1 if the predicate is of the form
490
0 func_item is a simple predicate: a field is compared with
496
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order)
534
bool optimizer::simple_pred(Item_func *func_item, Item **args, bool &inv_order)
500
switch (func_item->argument_count()) {
538
switch (func_item->argument_count())
502
541
/* MULT_EQUAL_FUNC */