93
73
UINT64_MAX Error: Could not calculate number of rows
94
74
# Multiplication of number of rows in all tables
96
77
static uint64_t get_exact_record_count(TableList *tables)
99
80
for (TableList *tl= tables; tl; tl= tl->next_leaf)
101
ha_rows tmp= tl->table->cursor->records();
82
ha_rows tmp= tl->table->file->records();
102
83
if ((tmp == HA_POS_ERROR))
104
84
return UINT64_MAX;
112
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)
114
114
List_iterator_fast<Item> it(all_fields);
115
115
int const_result= 1;
116
bool recalc_const_item= false;
116
bool recalc_const_item= 0;
117
117
uint64_t count= 1;
118
bool is_exact_count= true;
119
bool maybe_exact_count= true;
120
table_map removed_tables= 0;
121
table_map outer_tables= 0;
122
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;
123
120
table_map where_tables= 0;
129
125
where_tables= conds->used_tables();
133
Analyze outer join dependencies, and, if possible, compute the number
128
Analyze outer join dependencies, and, if possible, compute the number
136
131
for (TableList *tl= tables; tl; tl= tl->next_leaf)
138
TableList *embedded= NULL;
139
for (embedded= tl; embedded; embedded= embedded->getEmbedding())
134
for (embedded= tl ; embedded; embedded= embedded->embedding)
141
136
if (embedded->on_expr)
145
/* 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 */
147
142
outer_tables|= tl->table->map;
150
We can't optimise LEFT JOIN in cases where the WHERE condition
151
restricts the table that is used, like in:
152
SELECT MAX(t1.a) FROM t1 LEFT JOIN t2 join-condition
153
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;
155
150
if (tl->table->map & where_tables)
160
154
used_tables|= tl->table->map;
164
If the storage manager of 'tl' gives exact row count as part of
165
statistics (cheap), compute the total number of rows. If there are
166
no outer table dependencies, this count may be used as the real count.
167
Schema tables are filled after this function is invoked, so we can't
170
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) ||
172
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() &
173
169
is_exact_count= false;
174
count= 1; // ensure count != 0
170
count= 1; // ensure count != 0
178
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);
181
tl->table->print_error(error, MYF(ME_FATALERROR));
177
tl->table->file->print_error(error, MYF(ME_FATALERROR));
184
count*= tl->table->cursor->stats.records;
180
count*= tl->table->file->stats.records;
189
Iterate through all items in the SELECT clause and replace
190
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).
193
189
while ((item= it++))
195
191
if (item->type() == Item::SUM_FUNC_ITEM)
197
193
Item_sum *item_sum= (((Item_sum*) item));
198
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:
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)
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);
211
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)))
213
/* 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),
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;
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]);
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->getTable();
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->startIndexScan(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))
290
Closed interval: Either The MIN argument is non-nullable, or
291
we have a >= predicate for the MIN argument.
293
error= table->cursor->index_read_map(table->record[0],
295
make_prev_keypart_map(ref.key_parts),
296
HA_READ_KEY_OR_NEXT);
301
Open interval: There are two cases:
302
1) We have only MIN() and the argument column is nullable, or
303
2) there is a > predicate on it, nullability is irrelevant.
304
We need to scan the next bigger record first.
306
error= table->cursor->index_read_map(table->record[0],
308
make_prev_keypart_map(ref.key_parts),
311
If the found record is outside the group formed by the search
312
prefix, or there is no such record at all, check if all
313
records in that group have NULL in the MIN argument
314
column. If that is the case return that NULL.
316
Check if case 1 from above holds. If it does, we should read
319
if (item_field->field->real_maybe_null() &&
320
ref.key_buff[prefix_len] == 1 &&
322
Last keypart (i.e. the argument to MIN) is set to NULL by
323
find_key_for_maxmin only if all other keyparts are bound
324
to constants in a conjunction of equalities. Hence, we
325
can detect this by checking only if the last keypart is
328
(error == HA_ERR_KEY_NOT_FOUND ||
329
key_cmp_if_same(table, ref.key_buff, ref.key, prefix_len)))
331
assert(item_field->field->real_maybe_null());
332
error= table->cursor->index_read_map(table->record[0],
334
make_prev_keypart_map(ref.key_parts),
339
/* Verify that the read tuple indeed matches the search key */
348
error= HA_ERR_KEY_NOT_FOUND;
353
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
355
table->cursor->endIndexScan();
358
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
360
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
362
/* HA_ERR_LOCK_DEADLOCK or some other error */
363
table->print_error(error, MYF(0));
366
removed_tables|= table->map;
368
else if (! expr->const_item() || ! is_exact_count)
371
The optimization is not applicable in both cases:
372
(a) 'expr' is a non-constant expression. Then we can't
373
replace 'expr' by a constant.
374
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
375
NULL if the query does not return any rows. Thus, if we are not
376
able to determine if the query returns any rows, we can't apply
377
the optimization and replace MIN/MAX with a constant.
384
/* If count == 0, then we know that is_exact_count == true. */
385
((Item_sum_min*) item_sum)->clear(); /* Set to NULL. */
389
((Item_sum_min*) item_sum)->reset(); /* Set to the constant value. */
391
((Item_sum_min*) item_sum)->make_const();
392
recalc_const_item= 1;
395
case Item_sum::MAX_FUNC:
398
If MAX(expr) is the first part of a key or if all previous
399
parts of the key is found in the COND, then we can use
400
indexes to find the key.
402
Item *expr= item_sum->args[0];
403
if (expr->real_item()->type() == Item::FIELD_ITEM)
405
unsigned char key_buff[MAX_KEY_LENGTH];
406
table_reference_st ref;
407
uint32_t range_fl, prefix_len;
409
ref.key_buff= key_buff;
410
Item_field *item_field= (Item_field*) (expr->real_item());
411
Table *table= item_field->field->getTable();
414
Look for a partial key that can be used for optimization.
415
If we succeed, ref.key_length will contain the length of
416
this key, while prefix_len will contain the length of
417
the beginning of this key without field used in MAX().
418
Type of range for the key part for this field will be
419
returned in range_fl.
421
if (table->cursor->inited ||
422
(outer_tables & table->map) ||
423
! find_key_for_maxmin(1,
433
error= table->cursor->startIndexScan(static_cast<uint32_t>(ref.key), 1);
435
if (! ref.key_length)
437
error= table->cursor->index_last(table->record[0]);
441
error= table->cursor->index_read_map(table->record[0],
443
make_prev_keypart_map(ref.key_parts),
444
range_fl & NEAR_MAX ?
446
HA_READ_PREFIX_LAST_OR_PREV);
456
error= HA_ERR_KEY_NOT_FOUND;
461
table->cursor->extra(HA_EXTRA_NO_KEYREAD);
463
table->cursor->endIndexScan();
466
if (error == HA_ERR_KEY_NOT_FOUND || error == HA_ERR_END_OF_FILE)
468
return HA_ERR_KEY_NOT_FOUND; // No rows matching WHERE
470
/* HA_ERR_LOCK_DEADLOCK or some other error */
471
table->print_error(error, MYF(ME_FATALERROR));
474
removed_tables|= table->map;
476
else if (! expr->const_item() || ! is_exact_count)
479
The optimization is not applicable in both cases:
480
(a) 'expr' is a non-constant expression. Then we can't
481
replace 'expr' by a constant.
482
(b) 'expr' is a costant. According to ANSI, MIN/MAX must return
483
NULL if the query does not return any rows. Thus, if we are not
484
able to determine if the query returns any rows, we can't apply
485
the optimization and replace MIN/MAX with a constant.
492
/* If count != 1, then we know that is_exact_count == true. */
493
((Item_sum_max*) item_sum)->clear(); /* Set to NULL. */
497
((Item_sum_max*) item_sum)->reset(); /* Set to the constant value. */
499
((Item_sum_max*) item_sum)->make_const();
500
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;
508
460
else if (const_result)
510
462
if (recalc_const_item)
512
463
item->update_used_tables();
514
if (! item->const_item())
464
if (!item->const_item())
521
If we have a where clause, we can only ignore searching in the
522
tables if MIN/MAX optimisation replaced all used tables
523
We do not use replaced values in case of:
524
SELECT MIN(key) FROM table_1, empty_table
525
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().
527
475
if (removed_tables && used_tables != removed_tables)
529
476
const_result= 0; // We didn't remove all tables
531
477
return const_result;
535
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)
539
switch (func_item->argument_count())
500
switch (func_item->argument_count()) {
542
502
/* MULT_EQUAL_FUNC */