96
73
UINT64_MAX Error: Could not calculate number of rows
97
74
# Multiplication of number of rows in all tables
99
77
static uint64_t get_exact_record_count(TableList *tables)
101
79
uint64_t count= 1;
102
80
for (TableList *tl= tables; tl; tl= tl->next_leaf)
104
ha_rows tmp= tl->table->cursor->records();
82
ha_rows tmp= tl->table->file->records();
105
83
if ((tmp == HA_POS_ERROR))
107
84
return UINT64_MAX;
115
int optimizer::sum_query(TableList *tables, List<Item> &all_fields, COND *conds)
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(TableList *tables, List<Item> &all_fields,COND *conds)
117
List<Item>::iterator it(all_fields.begin());
114
List_iterator_fast<Item> it(all_fields);
118
115
int const_result= 1;
119
bool recalc_const_item= false;
116
bool recalc_const_item= 0;
120
117
uint64_t count= 1;
121
bool is_exact_count= true;
122
bool maybe_exact_count= true;
123
table_map removed_tables= 0;
124
table_map outer_tables= 0;
125
table_map used_tables= 0;
118
bool is_exact_count= true, maybe_exact_count= true;
119
table_map removed_tables= 0, outer_tables= 0, used_tables= 0;
126
120
table_map where_tables= 0;
132
125
where_tables= conds->used_tables();
136
Analyze outer join dependencies, and, if possible, compute the number
128
Analyze outer join dependencies, and, if possible, compute the number
139
131
for (TableList *tl= tables; tl; tl= tl->next_leaf)
141
TableList *embedded= NULL;
142
for (embedded= tl; embedded; embedded= embedded->getEmbedding())
134
for (embedded= tl ; embedded; embedded= embedded->embedding)
144
136
if (embedded->on_expr)
148
/* Don't replace expression on a table that is part of an outer join */
140
/* Don't replace expression on a table that is part of an outer join */
150
142
outer_tables|= tl->table->map;
153
We can't optimise LEFT JOIN in cases where the WHERE condition
154
restricts the table that is used, like in:
155
SELECT MAX(t1.a) FROM t1 LEFT JOIN t2 join-condition
156
WHERE t2.field IS NULL;
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;
158
150
if (tl->table->map & where_tables)
163
154
used_tables|= tl->table->map;
167
If the storage manager of 'tl' gives exact row count as part of
168
statistics (cheap), compute the total number of rows. If there are
169
no outer table dependencies, this count may be used as the real count.
170
Schema tables are filled after this function is invoked, so we can't
173
if (! (tl->table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)))
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) ||
175
maybe_exact_count&= test(tl->table->cursor->getEngine()->check_flag(HTON_BIT_HAS_RECORDS));
166
maybe_exact_count&= test(!tl->schema_table &&
167
(tl->table->file->ha_table_flags() &
176
169
is_exact_count= false;
177
count= 1; // ensure count != 0
170
count= 1; // ensure count != 0
181
error= tl->table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
174
error= tl->table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
184
tl->table->print_error(error, MYF(ME_FATALERROR));
177
tl->table->file->print_error(error, MYF(ME_FATALERROR));
187
count*= tl->table->cursor->stats.records;
180
count*= tl->table->file->stats.records;
192
Iterate through all items in the SELECT clause and replace
193
COUNT(), MIN() and MAX() with constants (if possible).
185
Iterate through all items in the SELECT clause and replace
186
COUNT(), MIN() and MAX() with constants (if possible).
196
189
while ((item= it++))
198
191
if (item->type() == Item::SUM_FUNC_ITEM)
200
193
Item_sum *item_sum= (((Item_sum*) item));
201
switch (item_sum->sum_func())
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:
203
case Item_sum::COUNT_FUNC:
205
If the expr in COUNT(expr) can never be null we can change this
206
to the number of rows in the tables if this number is exact and
207
there are no outer joins.
209
if (! conds && ! ((Item_sum_count*) item)->args[0]->maybe_null &&
210
! outer_tables && maybe_exact_count)
212
if (! is_exact_count)
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
unsigned char key_buff[MAX_KEY_LENGTH];
232
uint32_t 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);
214
if ((count= get_exact_record_count(tables)) == UINT64_MAX)
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)))
216
/* Error from handler in counting rows. Don't optimize count() */
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),
220
is_exact_count= 1; // count is now exact
222
((Item_sum_count*) item)->make_const_count((int64_t) count);
223
recalc_const_item= 1;
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
unsigned char key_buff[MAX_KEY_LENGTH];
380
uint32_t 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]);
230
case Item_sum::MIN_FUNC:
233
If MIN(expr) is the first part of a key or if all previous
234
parts of the key is found in the COND, then we can use
235
indexes to find the key.
237
Item *expr=item_sum->args[0];
238
if (expr->real_item()->type() == Item::FIELD_ITEM)
240
unsigned char key_buff[MAX_KEY_LENGTH];
241
table_reference_st ref;
242
uint32_t range_fl, prefix_len;
244
ref.key_buff= key_buff;
245
Item_field *item_field= (Item_field*) (expr->real_item());
246
Table *table= item_field->field->getTable();
249
Look for a partial key that can be used for optimization.
250
If we succeed, ref.key_length will contain the length of
251
this key, while prefix_len will contain the length of
252
the beginning of this key without field used in MIN().
253
Type of range for the key part for this field will be
254
returned in range_fl.
256
if (table->cursor->inited ||
257
(outer_tables & table->map) ||
258
! find_key_for_maxmin(0,
268
error= table->cursor->startIndexScan(static_cast<uint32_t>(ref.key), 1);
274
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
276
table->print_error(error, MYF(0));
280
if (! ref.key_length)
282
error= table->cursor->index_first(table->record[0]);
287
Use index to replace MIN/MAX functions with their values
288
according to the following rules:
290
1) Insert the minimum non-null values where the WHERE clause still
292
2) a NULL value if there are only NULL values for key_part_k.
293
3) Fail, producing a row of nulls
295
Implementation: Read the smallest value using the search key. If
296
the interval is open, read the next value after the search
297
key. If read fails, and we're looking for a MIN() value for a
298
nullable column, test if there is an exact match for the key.
300
if (! (range_fl & NEAR_MIN))
303
Closed interval: Either The MIN argument is non-nullable, or
304
we have a >= predicate for the MIN argument.
306
error= table->cursor->index_read_map(table->record[0],
308
make_prev_keypart_map(ref.key_parts),
309
HA_READ_KEY_OR_NEXT);
314
Open interval: There are two cases:
315
1) We have only MIN() and the argument column is nullable, or
316
2) there is a > predicate on it, nullability is irrelevant.
317
We need to scan the next bigger record first.
319
error= table->cursor->index_read_map(table->record[0],
321
make_prev_keypart_map(ref.key_parts),
324
If the found record is outside the group formed by the search
325
prefix, or there is no such record at all, check if all
326
records in that group have NULL in the MIN argument
327
column. If that is the case return that NULL.
329
Check if case 1 from above holds. If it does, we should read
332
if (item_field->field->real_maybe_null() &&
333
ref.key_buff[prefix_len] == 1 &&
335
Last keypart (i.e. the argument to MIN) is set to NULL by
336
find_key_for_maxmin only if all other keyparts are bound
337
to constants in a conjunction of equalities. Hence, we
338
can detect this by checking only if the last keypart is
341
(error == HA_ERR_KEY_NOT_FOUND ||
342
key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
344
assert(item_field->field->real_maybe_null());
345
error= table->cursor->index_read_map(table->record[0],
347
make_prev_keypart_map(ref.key_parts),
352
/* Verify that the read tuple indeed matches the search key */
361
error= HA_ERR_KEY_NOT_FOUND;
366
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
368
table->cursor->endIndexScan();
371
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
373
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
375
/* HA_ERR_LOCK_DEADLOCK or some other error */
376
table->print_error(error, MYF(0));
379
removed_tables|= table->map;
381
else if (! expr->const_item() || ! is_exact_count)
384
The optimization is not applicable in both cases:
385
(a) 'expr' is a non-constant expression. Then we can't
386
replace 'expr' by a constant.
387
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
388
NULL if the query does not return any rows. Thus, if we are not
389
able to determine if the query returns any rows, we can't apply
390
the optimization and replace MIN/MAX with a constant.
397
/* If count == 0, then we know that is_exact_count == true. */
398
((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
402
((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
404
((Item_sum_min*) item_sum)->make_const();
405
recalc_const_item= 1;
408
case Item_sum::MAX_FUNC:
411
If MAX(expr) is the first part of a key or if all previous
412
parts of the key is found in the COND, then we can use
413
indexes to find the key.
415
Item *expr= item_sum->args[0];
416
if (expr->real_item()->type() == Item::FIELD_ITEM)
418
unsigned char key_buff[MAX_KEY_LENGTH];
419
table_reference_st ref;
420
uint32_t range_fl, prefix_len;
422
ref.key_buff= key_buff;
423
Item_field *item_field= (Item_field*) (expr->real_item());
424
Table *table= item_field->field->getTable();
427
Look for a partial key that can be used for optimization.
428
If we succeed, ref.key_length will contain the length of
429
this key, while prefix_len will contain the length of
430
the beginning of this key without field used in MAX().
431
Type of range for the key part for this field will be
432
returned in range_fl.
434
if (table->cursor->inited ||
435
(outer_tables & table->map) ||
436
! find_key_for_maxmin(1,
446
error= table->cursor->startIndexScan(static_cast<uint32_t>(ref.key), 1);
448
if (! ref.key_length)
450
error= table->cursor->index_last(table->record[0]);
454
error= table->cursor->index_read_map(table->record[0],
456
make_prev_keypart_map(ref.key_parts),
457
range_fl & NEAR_MAX ?
459
HA_READ_PREFIX_LAST_OR_PREV);
469
error= HA_ERR_KEY_NOT_FOUND;
474
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
476
table->cursor->endIndexScan();
479
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
481
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
483
/* HA_ERR_LOCK_DEADLOCK or some other error */
484
table->print_error(error, MYF(ME_FATALERROR));
487
removed_tables|= table->map;
489
else if (! expr->const_item() || ! is_exact_count)
492
The optimization is not applicable in both cases:
493
(a) 'expr' is a non-constant expression. Then we can't
494
replace 'expr' by a constant.
495
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
496
NULL if the query does not return any rows. Thus, if we are not
497
able to determine if the query returns any rows, we can't apply
498
the optimization and replace MIN/MAX with a constant.
505
/* If count != 1, then we know that is_exact_count == true. */
506
((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
510
((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
512
((Item_sum_max*) item_sum)->make_const();
513
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.
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;
521
460
else if (const_result)
523
462
if (recalc_const_item)
525
463
item->update_used_tables();
527
if (! item->const_item())
464
if (!item->const_item())
534
If we have a where clause, we can only ignore searching in the
535
tables if MIN/MAX optimisation replaced all used tables
536
We do not use replaced values in case of:
537
SELECT MIN(key) FROM table_1, empty_table
538
removed_tables is != 0 if we have used MIN() or MAX().
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().
540
475
if (removed_tables && used_tables != removed_tables)
542
476
const_result= 0; // We didn't remove all tables
544
477
return const_result;
548
bool optimizer::simple_pred(Item_func *func_item, Item **args, bool &inv_order)
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)
552
switch (func_item->argument_count())
500
switch (func_item->argument_count()) {
555
502
/* MULT_EQUAL_FUNC */