135
132
every PS/SP execution new, we will not need reset this flag if
136
133
setup_tables_done_option changed for next rexecution
138
res= mysql_select(session,
139
&select_lex->ref_pointer_array,
135
res= mysql_select(session, &select_lex->ref_pointer_array,
140
136
(TableList*) select_lex->table_list.first,
141
select_lex->with_wild,
142
select_lex->item_list,
137
select_lex->with_wild, select_lex->item_list,
143
138
select_lex->where,
144
139
select_lex->order_list.elements +
145
140
select_lex->group_list.elements,
146
(Order*) select_lex->order_list.first,
147
(Order*) select_lex->group_list.first,
141
(order_st*) select_lex->order_list.first,
142
(order_st*) select_lex->group_list.first,
148
143
select_lex->having,
149
144
select_lex->options | session->options |
150
145
setup_tables_done_option,
649
657
use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
651
659
found_eq_constant= 0;
660
for (i= 0; i < keyuse->elements-1; i++, use++)
655
for (i= 0; i < keyuse->elements-1; i++, use++)
662
if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
663
use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
664
if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
657
if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
658
use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
659
if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
661
if (prev->getKeypart() + 1 < use->getKeypart() ||
662
((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
663
continue; /* remove */
665
else if (use->getKeypart() != 0) // First found must be 0
666
if (prev->getKeypart() + 1 < use->getKeypart() ||
667
((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
668
continue; /* remove */
670
else if (use->getKeypart() != 0) // First found must be 0
669
/* Valgrind complains about overlapped memcpy when save_pos==use. */
674
/* Valgrind complains about overlapped memcpy when save_pos==use. */
674
found_eq_constant= ! use->getUsedTables();
675
/* Save ptr to first use */
676
if (! use->getTable()->reginfo.join_tab->keyuse)
677
use->getTable()->reginfo.join_tab->keyuse= save_pos;
678
use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
681
i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
682
set_dynamic(keyuse, (unsigned char*) &key_end, i);
679
found_eq_constant= ! use->getUsedTables();
680
/* Save ptr to first use */
681
if (! use->getTable()->reginfo.join_tab->keyuse)
682
use->getTable()->reginfo.join_tab->keyuse= save_pos;
683
use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
686
i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
687
set_dynamic(keyuse, (unsigned char*) &key_end, i);
1216
Check if given expression uses only table fields covered by the given index
1219
uses_index_fields_only()
1220
item Expression to check
1221
tbl The table having the index
1222
keyno The index number
1223
other_tbls_ok true <=> Fields of other non-const tables are allowed
1226
Check if given expression only uses fields covered by index #keyno in the
1227
table tbl. The expression can use any fields in any other tables.
1229
The expression is guaranteed not to be AND or OR - those constructs are
1230
handled outside of this function.
1236
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
1238
if (item->const_item())
1242
Don't push down the triggered conditions. Nested outer joins execution
1243
code may need to evaluate a condition several times (both triggered and
1244
untriggered), and there is no way to put thi
1245
TODO: Consider cloning the triggered condition and using the copies for:
1246
1. push the first copy down, to have most restrictive index condition
1248
2. Put the second copy into tab->select_cond.
1250
if (item->type() == Item::FUNC_ITEM &&
1251
((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
1254
if (!(item->used_tables() & tbl->map))
1255
return other_tbls_ok;
1257
Item::Type item_type= item->type();
1258
switch (item_type) {
1259
case Item::FUNC_ITEM:
1261
/* This is a function, apply condition recursively to arguments */
1262
Item_func *item_func= (Item_func*)item;
1264
Item **item_end= (item_func->arguments()) + item_func->argument_count();
1265
for (child= item_func->arguments(); child != item_end; child++)
1267
if (!uses_index_fields_only(*child, tbl, keyno, other_tbls_ok))
1272
case Item::COND_ITEM:
1274
/* This is a function, apply condition recursively to arguments */
1275
List_iterator<Item> li(*((Item_cond*)item)->argument_list());
1277
while ((list_item=li++))
1279
if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
1284
case Item::FIELD_ITEM:
1286
Item_field *item_field= (Item_field*)item;
1287
if (item_field->field->table != tbl)
1289
return item_field->field->part_of_key.test(keyno);
1291
case Item::REF_ITEM:
1292
return uses_index_fields_only(item->real_item(), tbl, keyno,
1295
return false; /* Play it safe, don't push unknown non-const items */
1211
1299
#define ICP_COND_USES_INDEX_ONLY 10
1302
Get a part of the condition that can be checked using only index fields
1305
make_cond_for_index()
1306
cond The source condition
1307
table The table that is partially available
1308
keyno The index in the above table. Only fields covered by the index
1310
other_tbls_ok true <=> Fields of other non-const tables are allowed
1313
Get a part of the condition that can be checked when for the given table
1314
we have values only of fields covered by some index. The condition may
1315
refer to other tables, it is assumed that we have values of all of their
1319
make_cond_for_index(
1320
"cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
1323
"cond(t1.field) AND cond(t2.key2)"
1326
Index condition, or NULL if no condition could be inferred.
1328
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
1332
if (cond->type() == Item::COND_ITEM)
1334
uint32_t n_marked= 0;
1335
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1337
Item_cond_and *new_cond=new Item_cond_and;
1340
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1344
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1346
new_cond->argument_list()->push_back(fix);
1347
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1349
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1350
cond->marker= ICP_COND_USES_INDEX_ONLY;
1351
switch (new_cond->argument_list()->elements) {
1355
return new_cond->argument_list()->head();
1357
new_cond->quick_fix_field();
1363
Item_cond_or *new_cond=new Item_cond_or;
1366
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1370
Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
1373
new_cond->argument_list()->push_back(fix);
1374
n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
1376
if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
1377
cond->marker= ICP_COND_USES_INDEX_ONLY;
1378
new_cond->quick_fix_field();
1379
new_cond->top_level_item();
1384
if (!uses_index_fields_only(cond, table, keyno, other_tbls_ok))
1386
cond->marker= ICP_COND_USES_INDEX_ONLY;
1391
static Item *make_cond_remainder(Item *cond, bool exclude_index)
1393
if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
1394
return 0; /* Already checked */
1396
if (cond->type() == Item::COND_ITEM)
1398
table_map tbl_map= 0;
1399
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
1401
/* Create new top level AND item */
1402
Item_cond_and *new_cond=new Item_cond_and;
1405
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1409
Item *fix= make_cond_remainder(item, exclude_index);
1412
new_cond->argument_list()->push_back(fix);
1413
tbl_map |= fix->used_tables();
1416
switch (new_cond->argument_list()->elements) {
1420
return new_cond->argument_list()->head();
1422
new_cond->quick_fix_field();
1423
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
1429
Item_cond_or *new_cond=new Item_cond_or;
1432
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
1436
Item *fix= make_cond_remainder(item, false);
1439
new_cond->argument_list()->push_back(fix);
1440
tbl_map |= fix->used_tables();
1442
new_cond->quick_fix_field();
1443
((Item_cond*)new_cond)->used_tables_cache= tbl_map;
1444
new_cond->top_level_item();
1215
1452
cleanup JoinTable.
1259
Remove the following expressions from ORDER BY and GROUP BY:
1492
Remove the following expressions from order_st BY and GROUP BY:
1260
1493
Constant expressions @n
1261
1494
Expression that only uses tables that are of type EQ_REF and the reference
1262
1495
is in the order_st list or if all refereed tables are of the above type.
1264
1497
In the following, the X field can be removed:
1266
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
1267
SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
1499
SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
1500
SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
1270
1503
These can't be optimized:
1272
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
1273
SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
1274
SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
1505
SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
1506
SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
1507
SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
1277
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
1510
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab)
1279
1512
if (tab->cached_eq_ref_table) // If cached
1280
1513
return tab->eq_ref_table;
2638
2874
Do update counters for "pairs of brackets" that we've left (marked as
2639
2875
X,Y,Z in the above picture)
2641
for (;next_emb; next_emb= next_emb->getEmbedding())
2877
for (;next_emb; next_emb= next_emb->embedding)
2643
next_emb->getNestedJoin()->counter_++;
2644
if (next_emb->getNestedJoin()->counter_ == 1)
2879
next_emb->nested_join->counter_++;
2880
if (next_emb->nested_join->counter_ == 1)
2647
2883
next_emb is the first table inside a nested join we've "entered". In
2648
2884
the picture above, we're looking at the 'X' bracket. Don't exit yet as
2649
2885
X bracket might have Y pair bracket.
2651
join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
2887
join->cur_embedding_map |= next_emb->nested_join->nj_map;
2654
if (next_emb->getNestedJoin()->join_list.elements !=
2655
next_emb->getNestedJoin()->counter_)
2890
if (next_emb->nested_join->join_list.elements !=
2891
next_emb->nested_join->counter_)
2659
2895
We're currently at Y or Z-bracket as depicted in the above picture.
2660
2896
Mark that we've left it and continue walking up the brackets hierarchy.
2662
join->cur_embedding_map &= ~next_emb->getNestedJoin()->nj_map;
2898
join->cur_embedding_map &= ~next_emb->nested_join->nj_map;
2667
COND *optimize_cond(Join *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2903
COND *optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
2669
2905
Session *session= join->session;
5183
5410
uint32_t key_length,
5186
unsigned char *key_pos, *record=table->getInsertRecord();
5413
unsigned char *key_buffer, *key_pos, *record=table->record[0];
5188
5415
Cursor *cursor= table->cursor;
5189
5416
uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
5190
uint32_t *field_length;
5417
uint32_t *field_lengths,*field_length;
5192
std::vector<unsigned char> key_buffer;
5193
std::vector<uint32_t> field_lengths;
5195
key_buffer.resize((key_length + extra_length) * (long) cursor->stats.records);
5196
field_lengths.resize(field_count);
5420
if (! memory::multi_malloc(false,
5422
(uint32_t) ((key_length + extra_length) *
5423
(long) cursor->stats.records),
5425
(uint32_t) (field_count*sizeof(*field_lengths)),
5200
5431
uint32_t total_length= 0;
5202
for (ptr= first_field, field_length= &field_lengths[0] ; *ptr ; ptr++)
5432
for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
5204
5434
uint32_t length= (*ptr)->sort_length();
5205
5435
(*field_length++)= length;
5254
5485
if (hash_search(&hash, org_key_pos, key_length))
5256
5487
/* Duplicated found ; Remove the row */
5257
if ((error=cursor->deleteRecord(record)))
5488
if ((error=cursor->ha_delete_row(record)))
5261
5492
(void) my_hash_insert(&hash, org_key_pos);
5262
5493
key_pos+=extra_length;
5495
free((char*) key_buffer);
5264
5496
hash_free(&hash);
5265
5497
cursor->extra(HA_EXTRA_NO_CACHE);
5266
(void) cursor->endTableScan();
5498
(void) cursor->ha_rnd_end();
5502
free((char*) key_buffer);
5270
5503
hash_free(&hash);
5271
5504
cursor->extra(HA_EXTRA_NO_CACHE);
5272
(void) cursor->endTableScan();
5505
(void) cursor->ha_rnd_end();
5274
5507
table->print_error(error,MYF(0));
5278
SortField *make_unireg_sortorder(Order *order, uint32_t *length, SortField *sortorder)
5511
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
5280
5513
uint32_t count;
5281
SortField *sort,*pos;
5514
SORT_FIELD *sort,*pos;
5284
for (Order *tmp = order; tmp; tmp=tmp->next)
5517
for (order_st *tmp = order; tmp; tmp=tmp->next)
5286
5519
if (!sortorder)
5287
sortorder= (SortField*) memory::sql_alloc(sizeof(SortField) *
5520
sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
5288
5521
(max(count, *length) + 1));
5289
5522
pos= sort= sortorder;