48
45
#include "drizzled/optimizer/position.h"
49
46
#include "drizzled/optimizer/sargable_param.h"
50
47
#include "drizzled/optimizer/key_use.h"
51
#include "drizzled/optimizer/range.h"
52
#include "drizzled/optimizer/sum.h"
53
#include "drizzled/optimizer/explain_plan.h"
54
#include "drizzled/optimizer/access_method_factory.h"
55
#include "drizzled/optimizer/access_method.h"
56
#include "drizzled/records.h"
57
#include "drizzled/probes.h"
58
#include "drizzled/internal/my_bit.h"
59
#include "drizzled/internal/my_sys.h"
60
#include "drizzled/internal/iocache.h"
48
#include "mysys/my_bit.h"
62
50
#include <algorithm>
64
52
using namespace std;
69
extern plugin::StorageEngine *heap_engine;
70
extern std::bitset<12> test_flags;
53
using namespace drizzled;
72
55
/** Declarations of static functions used in this source file. */
73
static bool make_group_fields(Join *main_join, Join *curr_join);
74
static void calc_group_buffer(Join *join, Order *group);
75
static bool alloc_group_fields(Join *join, Order *group);
76
static uint32_t cache_record_length(Join *join, uint32_t index);
77
static double prev_record_reads(Join *join, uint32_t idx, table_map found_ref);
78
static bool get_best_combination(Join *join);
79
static void set_position(Join *join,
56
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
57
static void calc_group_buffer(JOIN *join,order_st *group);
58
static bool alloc_group_fields(JOIN *join,order_st *group);
59
static uint32_t cache_record_length(JOIN *join, uint32_t index);
60
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
61
static bool get_best_combination(JOIN *join);
62
static void set_position(JOIN *join,
82
65
optimizer::KeyUse *key);
83
static bool choose_plan(Join *join,table_map join_tables);
84
static void best_access_path(Join *join, JoinTable *s,
66
static bool choose_plan(JOIN *join,table_map join_tables);
67
static void best_access_path(JOIN *join, JoinTable *s,
86
69
table_map remaining_tables,
88
71
double record_count,
90
static void optimize_straight_join(Join *join, table_map join_tables);
91
static bool greedy_search(Join *join, table_map remaining_tables, uint32_t depth, uint32_t prune_level);
92
static bool best_extension_by_limited_search(Join *join,
73
static void optimize_straight_join(JOIN *join, table_map join_tables);
74
static bool greedy_search(JOIN *join, table_map remaining_tables, uint32_t depth, uint32_t prune_level);
75
static bool best_extension_by_limited_search(JOIN *join,
93
76
table_map remaining_tables,
95
78
double record_count,
98
81
uint32_t prune_level);
99
static uint32_t determine_search_depth(Join* join);
100
static bool make_simple_join(Join *join,Table *tmp_table);
101
static void make_outerjoin_info(Join *join);
102
static bool make_join_select(Join *join, optimizer::SqlSelect *select,COND *item);
103
static bool make_join_readinfo(Join *join);
104
static void update_depend_map(Join *join);
105
static void update_depend_map(Join *join, Order *order);
106
static Order *remove_constants(Join *join,Order *first_order,COND *cond, bool change_list, bool *simple_order);
107
static int return_zero_rows(Join *join,
82
static uint32_t determine_search_depth(JOIN* join);
83
static bool make_simple_join(JOIN *join,Table *tmp_table);
84
static void make_outerjoin_info(JOIN *join);
85
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
86
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
87
static void update_depend_map(JOIN *join);
88
static void update_depend_map(JOIN *join, order_st *order);
89
static order_st *remove_constants(JOIN *join,order_st *first_order,COND *cond, bool change_list, bool *simple_order);
90
static int return_zero_rows(JOIN *join,
108
91
select_result *res,
109
92
TableList *tables,
110
93
List<Item> &fields,
121
104
List<Item> &fields,
122
105
List<Item> &all_fields,
126
109
bool *hidden_group_fields);
127
static bool make_join_statistics(Join *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
110
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
128
111
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused);
129
static Table *get_sort_by_table(Order *a, Order *b,TableList *tables);
112
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
130
113
static void reset_nj_counters(List<TableList> *join_list);
131
static bool test_if_subpart(Order *a,Order *b);
114
static bool test_if_subpart(order_st *a,order_st *b);
132
115
static void restore_prev_nj_state(JoinTable *last);
116
static uint32_t make_join_orderinfo(JOIN *join);
133
117
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
134
118
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
531
507
zero_result_cause= "No matching min/max row";
533
goto setup_subq_exit;
535
511
zero_result_cause= "Select tables optimized away";
536
512
tables_list= 0; // All tables resolved
537
const_tables= tables;
539
514
Extract all table-independent conditions and replace the WHERE
540
clause with them. All other conditions were computed by optimizer::sum_query
515
clause with them. All other conditions were computed by opt_sum_query
541
516
and the MIN/MAX/COUNT function(s) have been replaced by constants,
542
517
so there is no need to compute the whole WHERE clause again.
543
518
Notice that make_cond_for_table() will always succeed to remove all
544
computed conditions, because optimizer::sum_query() is applicable only to
519
computed conditions, because opt_sum_query() is applicable only to
546
521
Preserve conditions for EXPLAIN.
596
571
if (const_tables && !(select_options & SELECT_NO_UNLOCK))
597
session->unlockSomeTables(table, const_tables);
572
mysql_unlock_some_tables(session, table, const_tables);
598
573
if (!conds && outer_join)
600
575
/* Handle the case where we have an OUTER JOIN without a WHERE */
601
576
conds=new Item_int((int64_t) 1,1); // Always true
603
select= optimizer::make_select(*table, const_table_map,
604
const_table_map, conds, 1, &error);
578
select= make_select(*table, const_table_map,
579
const_table_map, conds, 1, &error);
696
670
We have found that grouping can be removed since groups correspond to
697
671
only one row anyway, but we still have to guarantee correct result
698
672
order. The line below effectively rewrites the query from GROUP BY
699
<fields> to ORDER BY <fields>. There are two exceptions:
673
<fields> to order_st BY <fields>. There are two exceptions:
700
674
- if skip_sort_order is set (see above), then we can simply skip
702
- we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
676
- we can only rewrite order_st BY if the order_st BY fields are 'compatible'
703
677
with the GROUP BY ones, i.e. either one is a prefix of another.
704
We only check if the ORDER BY is a prefix of GROUP BY. In this case
678
We only check if the order_st BY is a prefix of GROUP BY. In this case
705
679
test_if_subpart() copies the ASC/DESC attributes from the original
707
681
If GROUP BY is a prefix of order_st BY, then it is safe to leave
710
if (! order || test_if_subpart(group_list, order))
684
if (!order || test_if_subpart(group_list, order))
711
685
order= skip_sort_order ? 0 : group_list;
713
687
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1791
1757
is called after all rows are sent, but before EOF packet is sent.
1793
1759
For a simple SELECT with no subqueries this function performs a full
1794
cleanup of the Join and calls unlockReadTables to free used base
1760
cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
1797
If a Join is executed for a subquery or if it has a subquery, we can't
1763
If a JOIN is executed for a subquery or if it has a subquery, we can't
1798
1764
do the full cleanup and need to do a partial cleanup only.
1799
- If a Join is not the top level join, we must not unlock the tables
1765
- If a JOIN is not the top level join, we must not unlock the tables
1800
1766
because the outer select may not have been evaluated yet, and we
1801
1767
can't unlock only selected tables of a query.
1802
- Additionally, if this Join corresponds to a correlated subquery, we
1768
- Additionally, if this JOIN corresponds to a correlated subquery, we
1803
1769
should not free quick selects and join buffers because they will be
1804
1770
needed for the next execution of the correlated subquery.
1805
- However, if this is a Join for a [sub]select, which is not
1771
- However, if this is a JOIN for a [sub]select, which is not
1806
1772
a correlated subquery itself, but has subqueries, we can free it
1807
fully and also free Joins of all its subqueries. The exception
1773
fully and also free JOINs of all its subqueries. The exception
1808
1774
is a subquery in SELECT list, e.g: @n
1809
1775
SELECT a, (select cmax(b) from t1) group by c @n
1810
1776
This subquery will not be evaluated at first sweep and its value will
2414
Cache constant expressions in WHERE, HAVING, ON conditions.
2417
void Join::cache_const_exprs()
2419
bool cache_flag= false;
2420
bool *analyzer_arg= &cache_flag;
2422
/* No need in cache if all tables are constant. */
2423
if (const_tables == tables)
2427
conds->compile(&Item::cache_const_expr_analyzer, (unsigned char **)&analyzer_arg,
2428
&Item::cache_const_expr_transformer, (unsigned char *)&cache_flag);
2431
having->compile(&Item::cache_const_expr_analyzer, (unsigned char **)&analyzer_arg,
2432
&Item::cache_const_expr_transformer, (unsigned char *)&cache_flag);
2434
for (JoinTable *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
2436
if (*tab->on_expr_ref)
2439
(*tab->on_expr_ref)->compile(&Item::cache_const_expr_analyzer,
2440
(unsigned char **)&analyzer_arg,
2441
&Item::cache_const_expr_transformer,
2442
(unsigned char *)&cache_flag);
2450
2385
Process one record of the nested loop join.
2817
2745
if (!end_of_records)
2819
2747
copy_fields(&join->tmp_table_param);
2820
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2821
return NESTED_LOOP_ERROR;
2748
copy_funcs(join->tmp_table_param.items_to_copy);
2822
2749
if (!join->having || join->having->val_int())
2825
2752
join->found_records++;
2826
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2753
if ((error=table->cursor->ha_write_row(table->record[0])))
2828
2755
if (!table->cursor->is_fatal_error(error, HA_CHECK_DUP))
2830
return NESTED_LOOP_OK;
2833
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2834
return NESTED_LOOP_ERROR; // Table is_full error
2757
if (create_myisam_from_heap(join->session, table,
2758
join->tmp_table_param.start_recinfo,
2759
&join->tmp_table_param.recinfo,
2761
return NESTED_LOOP_ERROR; // Not a table_is_full error
2762
table->s->uniques= 0; // To ensure rows are the same
2836
2764
if (++join->send_records >= join->tmp_table_param.end_write_records && join->do_send_rows)
2894
2822
We can't copy all data as the key may have different format
2895
2823
as the row data (for example as with VARCHAR keys)
2897
KeyPartInfo *key_part;
2825
KEY_PART_INFO *key_part;
2898
2826
for (group=table->group,key_part=table->key_info[0].key_part;
2900
2828
group=group->next,key_part++)
2902
2830
if (key_part->null_bit)
2903
memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
2831
memcpy(table->record[0]+key_part->offset, group->buff, 1);
2905
2833
init_tmptable_sum_functions(join->sum_funcs);
2906
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2907
return NESTED_LOOP_ERROR;
2908
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2834
copy_funcs(join->tmp_table_param.items_to_copy);
2835
if ((error=table->cursor->ha_write_row(table->record[0])))
2910
my_error(ER_USE_SQL_BIG_RESULT, MYF(0));
2911
return NESTED_LOOP_ERROR; // Table is_full error
2837
if (create_myisam_from_heap(join->session, table,
2838
join->tmp_table_param.start_recinfo,
2839
&join->tmp_table_param.recinfo,
2841
return NESTED_LOOP_ERROR; // Not a table_is_full error
2842
/* Change method to update rows */
2843
table->cursor->ha_index_init(0, 0);
2844
join->join_tab[join->tables-1].next_select= end_unique_update;
2913
2846
join->send_records++;
2914
2847
return NESTED_LOOP_OK;
2917
2850
/** Like end_update, but this is done with unique constraints instead of keys. */
2918
enum_nested_loop_state end_unique_update(Join *join, JoinTable *, bool end_of_records)
2851
enum_nested_loop_state end_unique_update(JOIN *join, JoinTable *, bool end_of_records)
2920
2853
Table *table= join->tmp_table;
2923
2856
if (end_of_records)
2924
2857
return NESTED_LOOP_OK;
2925
if (join->session->getKilled()) // Aborted by user
2858
if (join->session->killed) // Aborted by user
2927
2860
join->session->send_kill_message();
2928
2861
return NESTED_LOOP_KILLED;
2942
2874
table->print_error(error,MYF(0));
2943
2875
return NESTED_LOOP_ERROR;
2945
if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
2877
if (table->cursor->rnd_pos(table->record[1],table->cursor->dup_ref))
2947
2879
table->print_error(error,MYF(0));
2948
2880
return NESTED_LOOP_ERROR;
2950
2882
table->restoreRecord();
2951
2883
update_tmptable_sum_func(join->sum_funcs,table);
2952
if ((error= table->cursor->updateRecord(table->getUpdateRecord(),
2953
table->getInsertRecord())))
2884
if ((error= table->cursor->ha_update_row(table->record[1],
2955
2887
table->print_error(error,MYF(0));
2956
2888
return NESTED_LOOP_ERROR;
3016
2948
case REAL_RESULT:
3017
2949
key_length+= sizeof(double);
3020
2951
case INT_RESULT:
3021
2952
key_length+= sizeof(int64_t);
3024
2954
case DECIMAL_RESULT:
3025
2955
key_length+= my_decimal_get_binary_size(group_item->max_length -
3026
2956
(group_item->decimals ? 1 : 0),
3027
2957
group_item->decimals);
3030
2959
case STRING_RESULT:
3032
enum enum_field_types type= group_item->field_type();
2961
enum enum_field_types type= group_item->field_type();
2963
As items represented as DATE/TIME fields in the group buffer
2964
have STRING_RESULT result type, we increase the length
2965
by 8 as maximum pack length of such fields.
2967
if (type == DRIZZLE_TYPE_DATE ||
2968
type == DRIZZLE_TYPE_DATETIME ||
2969
type == DRIZZLE_TYPE_TIMESTAMP)
3034
As items represented as DATE/TIME fields in the group buffer
3035
have STRING_RESULT result type, we increase the length
3036
by 8 as maximum pack length of such fields.
2976
Group strings are taken as varstrings and require an length field.
2977
A field is not yet created by create_tmp_field()
2978
and the sizes should match up.
3038
if (type == DRIZZLE_TYPE_DATE ||
3039
type == DRIZZLE_TYPE_DATETIME ||
3040
type == DRIZZLE_TYPE_TIMESTAMP)
3047
Group strings are taken as varstrings and require an length field.
3048
A field is not yet created by create_tmp_field()
3049
and the sizes should match up.
3051
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
2980
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3057
2985
/* This case should never be choosen */
3059
2987
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3065
2991
if (group_item->maybe_null)
3069
2994
join->tmp_table_param.group_length=key_length+null_parts;
3070
2995
join->tmp_table_param.group_parts=parts;
3071
2996
join->tmp_table_param.group_null_parts=null_parts;
4902
4822
produce sorted output.
4904
4824
tab->sorted= sorted;
4905
sorted= false; // only first must be sorted
4825
sorted= 0; // only first must be sorted
4907
4826
if (tab->insideout_match_tab)
4909
if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
4828
if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
4915
optimizer::AccessMethodFactory &factory= optimizer::AccessMethodFactory::singleton();
4916
boost::shared_ptr<optimizer::AccessMethod> access_method(factory.createAccessMethod(tab->type));
4918
if (! access_method)
4922
* Is abort() the correct thing to call here? I call this here because it was what was called in
4923
* the default case for the switch statement that used to be here.
4833
switch (tab->type) {
4834
case AM_SYSTEM: // Only happens with left join
4835
table->status=STATUS_NO_RECORD;
4836
tab->read_first_record= join_read_system;
4837
tab->read_record.read_record= join_no_more_records;
4839
case AM_CONST: // Only happens with left join
4840
table->status=STATUS_NO_RECORD;
4841
tab->read_first_record= join_read_const;
4842
tab->read_record.read_record= join_no_more_records;
4843
if (table->covering_keys.test(tab->ref.key) &&
4847
table->cursor->extra(HA_EXTRA_KEYREAD);
4851
table->status=STATUS_NO_RECORD;
4854
delete tab->select->quick;
4855
tab->select->quick=0;
4859
tab->read_first_record= join_read_key;
4860
tab->read_record.read_record= join_no_more_records;
4861
if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
4864
table->cursor->extra(HA_EXTRA_KEYREAD);
4867
case AM_REF_OR_NULL:
4869
table->status=STATUS_NO_RECORD;
4872
delete tab->select->quick;
4873
tab->select->quick=0;
4877
if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
4880
table->cursor->extra(HA_EXTRA_KEYREAD);
4882
if (tab->type == AM_REF)
4884
tab->read_first_record= join_read_always_key;
4885
tab->read_record.read_record= tab->insideout_match_tab?
4886
join_read_next_same_diff : join_read_next_same;
4890
tab->read_first_record= join_read_always_key_or_null;
4891
tab->read_record.read_record= join_read_next_same_or_null;
4896
If previous table use cache
4897
If the incoming data set is already sorted don't use cache.
4899
table->status=STATUS_NO_RECORD;
4900
using_join_cache= false;
4901
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
4902
tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
4903
!tab->insideout_match_tab)
4905
if ((options & SELECT_DESCRIBE) ||
4906
!join_init_cache(join->session,join->join_tab+join->const_tables,
4907
i-join->const_tables))
4909
using_join_cache= true;
4910
tab[-1].next_select=sub_select_cache; /* Patch previous */
4913
/* These init changes read_record */
4914
if (tab->use_quick == 2)
4916
join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
4917
tab->read_first_record= join_init_quick_read_record;
4919
status_var_increment(join->session->status_var.select_range_check_count);
4923
tab->read_first_record= join_init_read_record;
4924
if (i == join->const_tables)
4926
if (tab->select && tab->select->quick)
4929
status_var_increment(join->session->status_var.select_range_count);
4933
join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
4935
status_var_increment(join->session->status_var.select_scan_count);
4940
if (tab->select && tab->select->quick)
4943
status_var_increment(join->session->status_var.select_full_range_join_count);
4947
join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
4949
status_var_increment(join->session->status_var.select_full_join_count);
4952
if (!table->no_keyread)
4954
if (tab->select && tab->select->quick &&
4955
tab->select->quick->index != MAX_KEY && //not index_merge
4956
table->covering_keys.test(tab->select->quick->index))
4959
table->cursor->extra(HA_EXTRA_KEYREAD);
4961
else if (!table->covering_keys.none() &&
4962
!(tab->select && tab->select->quick))
4963
{ // Only read index tree
4964
if (!tab->insideout_match_tab)
4967
See bug #26447: "Using the clustered index for a table scan
4968
is always faster than using a secondary index".
4970
if (table->s->primary_key != MAX_KEY &&
4971
table->cursor->primary_key_is_clustered())
4972
tab->index= table->s->primary_key;
4974
tab->index= table->find_shortest_key(&table->covering_keys);
4976
tab->read_first_record= join_read_first;
4977
tab->type= AM_NEXT; // Read with index_first / index_next
4928
access_method->getStats(table, tab);
4931
join->join_tab[join->tables-1].next_select= NULL; /* Set by do_select */
4989
join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
4936
4993
/** Update the dependency map for the tables. */
4937
static void update_depend_map(Join *join)
4994
static void update_depend_map(JOIN *join)
4939
4996
JoinTable *join_tab=join->join_tab, *end=join_tab+join->tables;
5319
5371
if (table->on_expr)
5321
table->setDepTables(table->getDepTables() | table->on_expr->used_tables());
5322
if (table->getEmbedding())
5373
table->dep_tables|= table->on_expr->used_tables();
5374
if (table->embedding)
5324
table->setDepTables(table->getDepTables() & ~table->getEmbedding()->getNestedJoin()->used_tables);
5376
table->dep_tables&= ~table->embedding->nested_join->used_tables;
5326
5378
Embedding table depends on tables used
5327
5379
in embedded on expressions.
5329
table->getEmbedding()->setOnExprDepTables(table->getEmbedding()->getOnExprDepTables() & table->on_expr->used_tables());
5381
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
5332
table->setDepTables(table->getDepTables() & ~table->table->map);
5384
table->dep_tables&= ~table->table->map;
5335
5387
if (prev_table)
5337
//If this is straight join, set prev table to be dependent on all tables
5338
//from this nested join, so that correct join order is selected.
5339
if ((test(join->select_options & SELECT_STRAIGHT_JOIN)) ||
5340
prev_table->straight)
5341
prev_table->setDepTables(prev_table->getDepTables() | used_tables);
5389
/* The order of tables is reverse: prev_table follows table */
5390
if (prev_table->straight)
5391
prev_table->dep_tables|= used_tables;
5342
5392
if (prev_table->on_expr)
5344
prev_table->setDepTables(prev_table->getDepTables() | table->getOnExprDepTables());
5345
table_map prev_used_tables= prev_table->getNestedJoin() ?
5346
prev_table->getNestedJoin()->used_tables :
5394
prev_table->dep_tables|= table->on_expr_dep_tables;
5395
table_map prev_used_tables= prev_table->nested_join ?
5396
prev_table->nested_join->used_tables :
5347
5397
prev_table->table->map;
5349
5399
If on expression contains only references to inner tables
5405
5455
join->unit->select_limit_cnt= 1; // Only send first row
5408
Field **first_field=entry->getFields() + entry->getShare()->sizeFields() - field_count;
5458
Field **first_field=entry->field+entry->s->fields - field_count;
5409
5459
offset= (field_count ?
5410
entry->getField(entry->getShare()->sizeFields() - field_count)->offset(entry->getInsertRecord()) : 0);
5411
reclength= entry->getShare()->getRecordLength() - offset;
5460
entry->field[entry->s->fields - field_count]->
5461
offset(entry->record[0]) : 0);
5462
reclength= entry->s->reclength-offset;
5413
5464
entry->free_io_cache(); // Safety
5414
5465
entry->cursor->info(HA_STATUS_VARIABLE);
5415
if (entry->getShare()->db_type() == heap_engine ||
5416
(!entry->getShare()->blob_fields &&
5466
if (entry->s->db_type() == heap_engine ||
5467
(!entry->s->blob_fields &&
5417
5468
((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->cursor->stats.records <
5418
session->variables.sortbuff_size)))
5469
session->variables.sortbuff_size)))
5420
5470
error= remove_dup_with_hash_index(join->session, entry,
5421
field_count, first_field,
5471
field_count, first_field,
5426
error= remove_dup_with_compare(join->session, entry, first_field, offset, having);
5474
error= remove_dup_with_compare(join->session, entry, first_field, offset,
5429
5477
free_blobs(first_field);
5553
5601
outer_join|= table->map;
5554
5602
s->embedding_map.reset();
5555
for (;embedding; embedding= embedding->getEmbedding())
5556
s->embedding_map|= embedding->getNestedJoin()->nj_map;
5603
for (;embedding; embedding= embedding->embedding)
5604
s->embedding_map|= embedding->nested_join->nj_map;
5559
if (embedding && !(false && ! embedding->getEmbedding()))
5607
if (embedding && !(false && ! embedding->embedding))
5561
5609
/* s belongs to a nested join, maybe to several embedded joins */
5562
5610
s->embedding_map.reset();
5565
nested_join_st *nested_join= embedding->getNestedJoin();
5613
nested_join_st *nested_join= embedding->nested_join;
5566
5614
s->embedding_map|= nested_join->nj_map;
5567
s->dependent|= embedding->getDepTables();
5568
embedding= embedding->getEmbedding();
5615
s->dependent|= embedding->dep_tables;
5616
embedding= embedding->embedding;
5569
5617
outer_join|= nested_join->used_tables;
5571
5619
while (embedding);
5574
5622
if ((table->cursor->stats.records <= 1) && !s->dependent &&
5575
(table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
5623
(table->cursor->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
5576
5624
!join->no_const_tables)
5578
5626
set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5831
5879
add_group_and_distinct_keys(join, s);
5833
5881
if (s->const_keys.any() &&
5834
!s->table->pos_in_table_list->getEmbedding())
5882
!s->table->pos_in_table_list->embedding)
5836
5884
ha_rows records;
5837
optimizer::SqlSelect *select= NULL;
5838
select= optimizer::make_select(s->table, found_const_table_map, found_const_table_map, *s->on_expr_ref ? *s->on_expr_ref : conds, 1, &error);
5886
select= make_select(s->table, found_const_table_map, found_const_table_map, *s->on_expr_ref ? *s->on_expr_ref : conds, 1, &error);
5841
5889
records= get_quick_record_count(join->session, select, s->table, &s->const_keys, join->row_limit);
6026
6069
Nested joins perspective: Remove the last table from the join order.
6028
The algorithm is the reciprocal of check_interleaving_with_nj(), hence
6029
parent join nest nodes are updated only when the last table in its child
6030
node is removed. The ASCII graphic below will clarify.
6032
%A table nesting such as <tt> t1 x [ ( t2 x t3 ) x ( t4 x t5 ) ] </tt>is
6033
represented by the below join nest tree.
6041
t1 x [ (t2 x t3) x (t4 x t5) ]
6044
At the point in time when check_interleaving_with_nj() adds the table t5 to
6045
the query execution plan, QEP, it also directs the node named NJ2 to mark
6046
the table as covered. NJ2 does so by incrementing its @c counter
6047
member. Since all of NJ2's tables are now covered by the QEP, the algorithm
6048
proceeds up the tree to NJ1, incrementing its counter as well. All join
6049
nests are now completely covered by the QEP.
6051
restore_prev_nj_state() does the above in reverse. As seen above, the node
6052
NJ1 contains the nodes t2, t3, and NJ2. Its counter being equal to 3 means
6053
that the plan covers t2, t3, and NJ2, @e and that the sub-plan (t4 x t5)
6054
completely covers NJ2. The removal of t5 from the partial plan will first
6055
decrement NJ2's counter to 1. It will then detect that NJ2 went from being
6056
completely to partially covered, and hence the algorithm must continue
6057
upwards to NJ1 and decrement its counter to 2. %A subsequent removal of t4
6058
will however not influence NJ1 since it did not un-cover the last table in
6062
restore_prev_nj_state()
6063
last join table to remove, it is assumed to be the last in current
6068
6071
Remove the last table from the partial join order and update the nested
6069
joins counters and join->cur_embedding_map. It is ok to call this
6070
function for the first table in join order (for which
6072
joins counters and join->cur_embedding_map. It is ok to call this
6073
function for the first table in join order (for which
6071
6074
check_interleaving_with_nj has not been called)
6073
6076
@param last join table to remove, it is assumed to be the last in current
6074
6077
partial join order.
6077
6079
static void restore_prev_nj_state(JoinTable *last)
6079
TableList *last_emb= last->table->pos_in_table_list->getEmbedding();
6080
Join *join= last->join;
6081
for (;last_emb != NULL; last_emb= last_emb->getEmbedding())
6083
nested_join_st *nest= last_emb->getNestedJoin();
6085
bool was_fully_covered= nest->is_fully_covered();
6087
if (--nest->counter_ == 0)
6088
join->cur_embedding_map&= ~nest->nj_map;
6090
if (!was_fully_covered)
6081
TableList *last_emb= last->table->pos_in_table_list->embedding;
6082
JOIN *join= last->join;
6085
if (last_emb->on_expr)
6087
if (!(--last_emb->nested_join->counter_))
6088
join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
6089
else if (last_emb->nested_join->join_list.elements-1 ==
6090
last_emb->nested_join->counter_)
6091
join->cur_embedding_map|= last_emb->nested_join->nj_map;
6095
last_emb= last_emb->embedding;
6100
Determine if the set is already ordered for order_st BY, so it can
6101
disable join cache because it will change the ordering of the results.
6102
Code handles sort table that is at any location (not only first after
6103
the const tables) despite the fact that it's currently prohibited.
6104
We must disable join cache if the first non-const table alone is
6105
ordered. If there is a temp table the ordering is done as a last
6106
operation and doesn't prevent join cache usage.
6108
static uint32_t make_join_orderinfo(JOIN *join)
6112
return join->tables;
6114
for (i=join->const_tables ; i < join->tables ; i++)
6116
JoinTable *tab= join->join_tab+i;
6117
Table *table= tab->table;
6118
if ((table == join->sort_by_table &&
6119
(!join->order || join->skip_sort_order)) ||
6120
(join->sort_by_table == (Table *) 1 && i != join->const_tables))
6093
join->cur_embedding_map|= nest->nj_map;