47
44
#include "drizzled/field/blob.h"
48
45
#include "drizzled/optimizer/position.h"
49
46
#include "drizzled/optimizer/sargable_param.h"
50
#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"
47
#include "mysys/my_bit.h"
62
49
#include <algorithm>
64
51
using namespace std;
69
extern plugin::StorageEngine *heap_engine;
70
extern std::bitset<12> test_flags;
52
using namespace drizzled;
72
54
/** 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,
82
optimizer::KeyUse *key);
83
static bool choose_plan(Join *join,table_map join_tables);
84
static void best_access_path(Join *join, JoinTable *s,
55
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
56
static void calc_group_buffer(JOIN *join,order_st *group);
57
static bool alloc_group_fields(JOIN *join,order_st *group);
58
static uint32_t cache_record_length(JOIN *join, uint32_t index);
59
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
60
static bool get_best_combination(JOIN *join);
61
static void set_position(JOIN *join,uint32_t index,JoinTable *table,KeyUse *key);
62
static bool choose_plan(JOIN *join,table_map join_tables);
63
static void best_access_path(JOIN *join, JoinTable *s,
86
65
table_map remaining_tables,
88
67
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,
69
static void optimize_straight_join(JOIN *join, table_map join_tables);
70
static bool greedy_search(JOIN *join, table_map remaining_tables, uint32_t depth, uint32_t prune_level);
71
static bool best_extension_by_limited_search(JOIN *join,
93
72
table_map remaining_tables,
95
74
double record_count,
98
77
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,
78
static uint32_t determine_search_depth(JOIN* join);
79
static bool make_simple_join(JOIN *join,Table *tmp_table);
80
static void make_outerjoin_info(JOIN *join);
81
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
82
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
83
static void update_depend_map(JOIN *join);
84
static void update_depend_map(JOIN *join, order_st *order);
85
static order_st *remove_constants(JOIN *join,order_st *first_order,COND *cond, bool change_list, bool *simple_order);
86
static int return_zero_rows(JOIN *join,
108
87
select_result *res,
109
88
TableList *tables,
110
89
List<Item> &fields,
121
100
List<Item> &fields,
122
101
List<Item> &all_fields,
126
105
bool *hidden_group_fields);
127
static bool make_join_statistics(Join *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
106
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
128
107
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);
108
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
130
109
static void reset_nj_counters(List<TableList> *join_list);
131
static bool test_if_subpart(Order *a,Order *b);
110
static bool test_if_subpart(order_st *a,order_st *b);
132
111
static void restore_prev_nj_state(JoinTable *last);
112
static uint32_t make_join_orderinfo(JOIN *join);
133
113
static bool add_ref_to_table_cond(Session *session, JoinTable *join_tab);
134
114
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
583
553
select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
585
555
zero_result_cause= "no matching row in const table";
586
goto setup_subq_exit;
588
559
if (!(session->options & OPTION_BIG_SELECTS) &&
589
560
best_read > (double) session->variables.max_join_size &&
590
561
!(select_options & SELECT_DESCRIBE))
562
{ /* purecov: inspected */
592
563
my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
596
567
if (const_tables && !(select_options & SELECT_NO_UNLOCK))
597
session->unlockSomeTables(table, const_tables);
568
mysql_unlock_some_tables(session, table, const_tables);
598
569
if (!conds && outer_join)
600
571
/* Handle the case where we have an OUTER JOIN without a WHERE */
601
572
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);
574
select= make_select(*table, const_table_map,
575
const_table_map, conds, 1, &error);
577
{ /* purecov: inspected */
578
error= -1; /* purecov: inspected */
696
666
We have found that grouping can be removed since groups correspond to
697
667
only one row anyway, but we still have to guarantee correct result
698
668
order. The line below effectively rewrites the query from GROUP BY
699
<fields> to ORDER BY <fields>. There are two exceptions:
669
<fields> to order_st BY <fields>. There are two exceptions:
700
670
- 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'
672
- we can only rewrite order_st BY if the order_st BY fields are 'compatible'
703
673
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
674
We only check if the order_st BY is a prefix of GROUP BY. In this case
705
675
test_if_subpart() copies the ASC/DESC attributes from the original
707
677
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))
680
if (!order || test_if_subpart(group_list, order))
711
681
order= skip_sort_order ? 0 : group_list;
713
683
If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1791
1753
is called after all rows are sent, but before EOF packet is sent.
1793
1755
For a simple SELECT with no subqueries this function performs a full
1794
cleanup of the Join and calls unlockReadTables to free used base
1756
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
1759
If a JOIN is executed for a subquery or if it has a subquery, we can't
1798
1760
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
1761
- If a JOIN is not the top level join, we must not unlock the tables
1800
1762
because the outer select may not have been evaluated yet, and we
1801
1763
can't unlock only selected tables of a query.
1802
- Additionally, if this Join corresponds to a correlated subquery, we
1764
- Additionally, if this JOIN corresponds to a correlated subquery, we
1803
1765
should not free quick selects and join buffers because they will be
1804
1766
needed for the next execution of the correlated subquery.
1805
- However, if this is a Join for a [sub]select, which is not
1767
- However, if this is a JOIN for a [sub]select, which is not
1806
1768
a correlated subquery itself, but has subqueries, we can free it
1807
fully and also free Joins of all its subqueries. The exception
1769
fully and also free JOINs of all its subqueries. The exception
1808
1770
is a subquery in SELECT list, e.g: @n
1809
1771
SELECT a, (select cmax(b) from t1) group by c @n
1810
1772
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
2381
Process one record of the nested loop join.
2805
2729
return NESTED_LOOP_OK;
2808
enum_nested_loop_state end_write(Join *join, JoinTable *, bool end_of_records)
2732
enum_nested_loop_state end_write(JOIN *join, JoinTable *, bool end_of_records)
2810
2734
Table *table= join->tmp_table;
2812
if (join->session->getKilled()) // Aborted by user
2736
if (join->session->killed) // Aborted by user
2814
2738
join->session->send_kill_message();
2815
return NESTED_LOOP_KILLED;
2739
return NESTED_LOOP_KILLED; /* purecov: inspected */
2817
2741
if (!end_of_records)
2819
2743
copy_fields(&join->tmp_table_param);
2820
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2821
return NESTED_LOOP_ERROR;
2744
copy_funcs(join->tmp_table_param.items_to_copy);
2822
2745
if (!join->having || join->having->val_int())
2825
2748
join->found_records++;
2826
if ((error=table->cursor->insertRecord(table->getInsertRecord())))
2749
if ((error=table->file->ha_write_row(table->record[0])))
2828
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
2751
if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
2753
if (create_myisam_from_heap(join->session, table,
2754
join->tmp_table_param.start_recinfo,
2755
&join->tmp_table_param.recinfo,
2757
return NESTED_LOOP_ERROR; // Not a table_is_full error
2758
table->s->uniques= 0; // To ensure rows are the same
2836
2760
if (++join->send_records >= join->tmp_table_param.end_write_records && join->do_send_rows)
2873
2797
if (item->maybe_null)
2874
2798
group->buff[-1]= (char) group->field->is_null();
2876
if (!table->cursor->index_read_map(table->getUpdateRecord(),
2800
if (!table->file->index_read_map(table->record[1],
2877
2801
join->tmp_table_param.group_buff,
2879
2803
HA_READ_KEY_EXACT))
2880
2804
{ /* Update old record */
2881
2805
table->restoreRecord();
2882
2806
update_tmptable_sum_func(join->sum_funcs,table);
2883
if ((error= table->cursor->updateRecord(table->getUpdateRecord(),
2884
table->getInsertRecord())))
2807
if ((error= table->file->ha_update_row(table->record[1],
2886
table->print_error(error,MYF(0));
2887
return NESTED_LOOP_ERROR;
2810
table->file->print_error(error,MYF(0)); /* purecov: inspected */
2811
return NESTED_LOOP_ERROR; /* purecov: inspected */
2889
2813
return NESTED_LOOP_OK;
2894
2818
We can't copy all data as the key may have different format
2895
2819
as the row data (for example as with VARCHAR keys)
2897
KeyPartInfo *key_part;
2821
KEY_PART_INFO *key_part;
2898
2822
for (group=table->group,key_part=table->key_info[0].key_part;
2900
2824
group=group->next,key_part++)
2902
2826
if (key_part->null_bit)
2903
memcpy(table->getInsertRecord()+key_part->offset, group->buff, 1);
2827
memcpy(table->record[0]+key_part->offset, group->buff, 1);
2905
2829
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())))
2830
copy_funcs(join->tmp_table_param.items_to_copy);
2831
if ((error=table->file->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
2833
if (create_myisam_from_heap(join->session, table,
2834
join->tmp_table_param.start_recinfo,
2835
&join->tmp_table_param.recinfo,
2837
return NESTED_LOOP_ERROR; // Not a table_is_full error
2838
/* Change method to update rows */
2839
table->file->ha_index_init(0, 0);
2840
join->join_tab[join->tables-1].next_select= end_unique_update;
2913
2842
join->send_records++;
2914
2843
return NESTED_LOOP_OK;
2917
2846
/** 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)
2847
enum_nested_loop_state end_unique_update(JOIN *join, JoinTable *, bool end_of_records)
2920
2849
Table *table= join->tmp_table;
2923
2852
if (end_of_records)
2924
2853
return NESTED_LOOP_OK;
2925
if (join->session->getKilled()) // Aborted by user
2854
if (join->session->killed) // Aborted by user
2927
2856
join->session->send_kill_message();
2928
return NESTED_LOOP_KILLED;
2857
return NESTED_LOOP_KILLED; /* purecov: inspected */
2931
2860
init_tmptable_sum_functions(join->sum_funcs);
2932
2861
copy_fields(&join->tmp_table_param); // Groups are copied twice.
2933
if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
2934
return NESTED_LOOP_ERROR;
2862
copy_funcs(join->tmp_table_param.items_to_copy);
2936
if (!(error= table->cursor->insertRecord(table->getInsertRecord())))
2864
if (!(error= table->file->ha_write_row(table->record[0])))
2937
2865
join->send_records++; // New group
2940
if ((int) table->get_dup_key(error) < 0)
2868
if ((int) table->file->get_dup_key(error) < 0)
2942
table->print_error(error,MYF(0));
2943
return NESTED_LOOP_ERROR;
2870
table->file->print_error(error,MYF(0)); /* purecov: inspected */
2871
return NESTED_LOOP_ERROR; /* purecov: inspected */
2945
if (table->cursor->rnd_pos(table->getUpdateRecord(),table->cursor->dup_ref))
2873
if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
2947
table->print_error(error,MYF(0));
2948
return NESTED_LOOP_ERROR;
2875
table->file->print_error(error,MYF(0)); /* purecov: inspected */
2876
return NESTED_LOOP_ERROR; /* purecov: inspected */
2950
2878
table->restoreRecord();
2951
2879
update_tmptable_sum_func(join->sum_funcs,table);
2952
if ((error= table->cursor->updateRecord(table->getUpdateRecord(),
2953
table->getInsertRecord())))
2880
if ((error= table->file->ha_update_row(table->record[1],
2955
table->print_error(error,MYF(0));
2956
return NESTED_LOOP_ERROR;
2883
table->file->print_error(error,MYF(0)); /* purecov: inspected */
2884
return NESTED_LOOP_ERROR; /* purecov: inspected */
2959
2887
return NESTED_LOOP_OK;
3016
2944
case REAL_RESULT:
3017
2945
key_length+= sizeof(double);
3020
2947
case INT_RESULT:
3021
2948
key_length+= sizeof(int64_t);
3024
2950
case DECIMAL_RESULT:
3025
2951
key_length+= my_decimal_get_binary_size(group_item->max_length -
3026
2952
(group_item->decimals ? 1 : 0),
3027
2953
group_item->decimals);
3030
2955
case STRING_RESULT:
3032
enum enum_field_types type= group_item->field_type();
2957
enum enum_field_types type= group_item->field_type();
2959
As items represented as DATE/TIME fields in the group buffer
2960
have STRING_RESULT result type, we increase the length
2961
by 8 as maximum pack length of such fields.
2963
if (type == DRIZZLE_TYPE_DATE ||
2964
type == DRIZZLE_TYPE_DATETIME ||
2965
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.
2972
Group strings are taken as varstrings and require an length field.
2973
A field is not yet created by create_tmp_field()
2974
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;
2976
key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
3057
2981
/* This case should never be choosen */
3059
2983
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
3065
2987
if (group_item->maybe_null)
3069
2990
join->tmp_table_param.group_length=key_length+null_parts;
3070
2991
join->tmp_table_param.group_parts=parts;
3071
2992
join->tmp_table_param.group_null_parts=null_parts;
3380
3298
{ /* Use key if possible */
3381
3299
Table *table= s->table;
3382
optimizer::KeyUse *keyuse= NULL;
3383
optimizer::KeyUse *start_key= NULL;
3300
KeyUse *keyuse,*start_key=0;
3384
3301
double best_records= DBL_MAX;
3385
3302
uint32_t max_key_part=0;
3387
3304
/* Test how we can use keys */
3388
3305
rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE; // Assumed records/key
3389
for (keyuse= s->keyuse; keyuse->getTable() == table; )
3306
for (keyuse=s->keyuse ; keyuse->table == table ;)
3391
3308
key_part_map found_part= 0;
3392
3309
table_map found_ref= 0;
3393
uint32_t key= keyuse->getKey();
3394
KeyInfo *keyinfo= table->key_info + key;
3310
uint32_t key= keyuse->key;
3311
KEY *keyinfo= table->key_info+key;
3395
3312
/* Bitmap of keyparts where the ref access is over 'keypart=const': */
3396
3313
key_part_map const_part= 0;
3397
3314
/* The or-null keypart in ref-or-null access: */
3413
3330
if 1. expression doesn't refer to forward tables
3414
3331
2. we won't get two ref-or-null's
3416
if (! (remaining_tables & keyuse->getUsedTables()) &&
3417
! (ref_or_null_part && (keyuse->getOptimizeFlags() &
3418
KEY_OPTIMIZE_REF_OR_NULL)))
3333
if (!(remaining_tables & keyuse->used_tables) &&
3334
!(ref_or_null_part && (keyuse->optimize &
3335
KEY_OPTIMIZE_REF_OR_NULL)))
3420
found_part|= keyuse->getKeypartMap();
3421
if (! (keyuse->getUsedTables() & ~join->const_table_map))
3422
const_part|= keyuse->getKeypartMap();
3337
found_part|= keyuse->keypart_map;
3338
if (!(keyuse->used_tables & ~join->const_table_map))
3339
const_part|= keyuse->keypart_map;
3424
3341
double tmp2= prev_record_reads(join, idx, (found_ref |
3425
keyuse->getUsedTables()));
3342
keyuse->used_tables));
3426
3343
if (tmp2 < best_prev_record_reads)
3428
best_part_found_ref= keyuse->getUsedTables() & ~join->const_table_map;
3345
best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
3429
3346
best_prev_record_reads= tmp2;
3431
if (rec > keyuse->getTableRows())
3432
rec= keyuse->getTableRows();
3348
if (rec > keyuse->ref_table_rows)
3349
rec= keyuse->ref_table_rows;
3434
3351
If there is one 'key_column IS NULL' expression, we can
3435
3352
use this ref_or_null optimisation of this field
3437
if (keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)
3438
ref_or_null_part|= keyuse->getKeypartMap();
3354
if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
3355
ref_or_null_part |= keyuse->keypart_map;
3442
} while (keyuse->getTable() == table && keyuse->getKey() == key &&
3443
keyuse->getKeypart() == keypart);
3444
found_ref|= best_part_found_ref;
3445
} while (keyuse->getTable() == table && keyuse->getKey() == key);
3359
} while (keyuse->table == table && keyuse->key == key &&
3360
keyuse->keypart == keypart);
3361
found_ref|= best_part_found_ref;
3362
} while (keyuse->table == table && keyuse->key == key);
3448
3365
Assume that that each key matches a proportional part of table.
4887
4796
true - Out of memory
4889
static bool make_join_readinfo(Join *join)
4798
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
4801
bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
4893
for (uint32_t i= join->const_tables ; i < join->tables ; i++)
4804
for (i=join->const_tables ; i < join->tables ; i++)
4895
4806
JoinTable *tab=join->join_tab+i;
4896
4807
Table *table=tab->table;
4808
bool using_join_cache;
4897
4809
tab->read_record.table= table;
4898
tab->read_record.cursor= table->cursor;
4810
tab->read_record.file=table->file;
4899
4811
tab->next_select=sub_select; /* normal select */
4901
4813
TODO: don't always instruct first table's ref/range access method to
4902
4814
produce sorted output.
4904
4816
tab->sorted= sorted;
4905
sorted= false; // only first must be sorted
4817
sorted= 0; // only first must be sorted
4907
4818
if (tab->insideout_match_tab)
4909
if (! (tab->insideout_buf= (unsigned char*) join->session->alloc(tab->table->key_info
4820
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.
4825
switch (tab->type) {
4826
case AM_SYSTEM: // Only happens with left join
4827
table->status=STATUS_NO_RECORD;
4828
tab->read_first_record= join_read_system;
4829
tab->read_record.read_record= join_no_more_records;
4831
case AM_CONST: // Only happens with left join
4832
table->status=STATUS_NO_RECORD;
4833
tab->read_first_record= join_read_const;
4834
tab->read_record.read_record= join_no_more_records;
4835
if (table->covering_keys.test(tab->ref.key) &&
4839
table->file->extra(HA_EXTRA_KEYREAD);
4843
table->status=STATUS_NO_RECORD;
4846
delete tab->select->quick;
4847
tab->select->quick=0;
4851
tab->read_first_record= join_read_key;
4852
tab->read_record.read_record= join_no_more_records;
4853
if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
4856
table->file->extra(HA_EXTRA_KEYREAD);
4859
case AM_REF_OR_NULL:
4861
table->status=STATUS_NO_RECORD;
4864
delete tab->select->quick;
4865
tab->select->quick=0;
4869
if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
4872
table->file->extra(HA_EXTRA_KEYREAD);
4874
if (tab->type == AM_REF)
4876
tab->read_first_record= join_read_always_key;
4877
tab->read_record.read_record= tab->insideout_match_tab?
4878
join_read_next_same_diff : join_read_next_same;
4882
tab->read_first_record= join_read_always_key_or_null;
4883
tab->read_record.read_record= join_read_next_same_or_null;
4888
If previous table use cache
4889
If the incoming data set is already sorted don't use cache.
4891
table->status=STATUS_NO_RECORD;
4892
using_join_cache= false;
4893
if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
4894
tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
4895
!tab->insideout_match_tab)
4897
if ((options & SELECT_DESCRIBE) ||
4898
!join_init_cache(join->session,join->join_tab+join->const_tables,
4899
i-join->const_tables))
4901
using_join_cache= true;
4902
tab[-1].next_select=sub_select_cache; /* Patch previous */
4905
/* These init changes read_record */
4906
if (tab->use_quick == 2)
4908
join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
4909
tab->read_first_record= join_init_quick_read_record;
4911
status_var_increment(join->session->status_var.select_range_check_count);
4915
tab->read_first_record= join_init_read_record;
4916
if (i == join->const_tables)
4918
if (tab->select && tab->select->quick)
4921
status_var_increment(join->session->status_var.select_range_count);
4925
join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
4927
status_var_increment(join->session->status_var.select_scan_count);
4932
if (tab->select && tab->select->quick)
4935
status_var_increment(join->session->status_var.select_full_range_join_count);
4939
join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
4941
status_var_increment(join->session->status_var.select_full_join_count);
4944
if (!table->no_keyread)
4946
if (tab->select && tab->select->quick &&
4947
tab->select->quick->index != MAX_KEY && //not index_merge
4948
table->covering_keys.test(tab->select->quick->index))
4951
table->file->extra(HA_EXTRA_KEYREAD);
4953
else if (!table->covering_keys.none() &&
4954
!(tab->select && tab->select->quick))
4955
{ // Only read index tree
4956
if (!tab->insideout_match_tab)
4959
See bug #26447: "Using the clustered index for a table scan
4960
is always faster than using a secondary index".
4962
if (table->s->primary_key != MAX_KEY &&
4963
table->file->primary_key_is_clustered())
4964
tab->index= table->s->primary_key;
4966
tab->index= table->find_shortest_key(&table->covering_keys);
4968
tab->read_first_record= join_read_first;
4969
tab->type= AM_NEXT; // Read with index_first / index_next
4975
break; /* purecov: deadcode */
4978
abort(); /* purecov: deadcode */
4928
access_method->getStats(table, tab);
4931
join->join_tab[join->tables-1].next_select= NULL; /* Set by do_select */
4981
join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
4936
4985
/** Update the dependency map for the tables. */
4937
static void update_depend_map(Join *join)
4986
static void update_depend_map(JOIN *join)
4939
4988
JoinTable *join_tab=join->join_tab, *end=join_tab+join->tables;
5319
5363
if (table->on_expr)
5321
table->setDepTables(table->getDepTables() | table->on_expr->used_tables());
5322
if (table->getEmbedding())
5365
table->dep_tables|= table->on_expr->used_tables();
5366
if (table->embedding)
5324
table->setDepTables(table->getDepTables() & ~table->getEmbedding()->getNestedJoin()->used_tables);
5368
table->dep_tables&= ~table->embedding->nested_join->used_tables;
5326
5370
Embedding table depends on tables used
5327
5371
in embedded on expressions.
5329
table->getEmbedding()->setOnExprDepTables(table->getEmbedding()->getOnExprDepTables() & table->on_expr->used_tables());
5373
table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
5332
table->setDepTables(table->getDepTables() & ~table->table->map);
5376
table->dep_tables&= ~table->table->map;
5335
5379
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);
5381
/* The order of tables is reverse: prev_table follows table */
5382
if (prev_table->straight)
5383
prev_table->dep_tables|= used_tables;
5342
5384
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 :
5386
prev_table->dep_tables|= table->on_expr_dep_tables;
5387
table_map prev_used_tables= prev_table->nested_join ?
5388
prev_table->nested_join->used_tables :
5347
5389
prev_table->table->map;
5349
5391
If on expression contains only references to inner tables
5405
5447
join->unit->select_limit_cnt= 1; // Only send first row
5408
Field **first_field=entry->getFields() + entry->getShare()->sizeFields() - field_count;
5450
Field **first_field=entry->field+entry->s->fields - field_count;
5409
5451
offset= (field_count ?
5410
entry->getField(entry->getShare()->sizeFields() - field_count)->offset(entry->getInsertRecord()) : 0);
5411
reclength= entry->getShare()->getRecordLength() - offset;
5452
entry->field[entry->s->fields - field_count]->
5453
offset(entry->record[0]) : 0);
5454
reclength= entry->s->reclength-offset;
5413
5456
entry->free_io_cache(); // Safety
5414
entry->cursor->info(HA_STATUS_VARIABLE);
5415
if (entry->getShare()->db_type() == heap_engine ||
5416
(!entry->getShare()->blob_fields &&
5417
((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->cursor->stats.records <
5418
session->variables.sortbuff_size)))
5457
entry->file->info(HA_STATUS_VARIABLE);
5458
if (entry->s->db_type() == heap_engine ||
5459
(!entry->s->blob_fields &&
5460
((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
5461
session->variables.sortbuff_size)))
5420
5462
error= remove_dup_with_hash_index(join->session, entry,
5421
field_count, first_field,
5463
field_count, first_field,
5426
error= remove_dup_with_compare(join->session, entry, first_field, offset, having);
5466
error= remove_dup_with_compare(join->session, entry, first_field, offset,
5429
5469
free_blobs(first_field);
5472
static bool make_join_statistics(Join *join, TableList *tables, COND *conds, DYNAMIC_ARRAY *keyuse_array)
5511
static bool make_join_statistics(JOIN *join, TableList *tables, COND *conds, DYNAMIC_ARRAY *keyuse_array)
5477
uint32_t table_count;
5478
uint32_t const_count;
5480
table_map found_const_table_map;
5481
table_map all_table_map;
5482
table_map found_ref;
5486
Table **table_vector= NULL;
5487
JoinTable *stat= NULL;
5488
JoinTable *stat_end= NULL;
5490
JoinTable **stat_ref= NULL;
5491
optimizer::KeyUse *keyuse= NULL;
5492
optimizer::KeyUse *start_keyuse= NULL;
5493
table_map outer_join= 0;
5515
uint32_t i,table_count,const_count,key;
5516
table_map found_const_table_map, all_table_map, found_ref, refs;
5517
key_map const_ref, eq_part;
5518
Table **table_vector;
5519
JoinTable *stat,*stat_end,*s,**stat_ref;
5520
KeyUse *keyuse,*start_keyuse;
5521
table_map outer_join=0;
5494
5522
vector<optimizer::SargableParam> sargables;
5495
5523
JoinTable *stat_vector[MAX_TABLES+1];
5496
5524
optimizer::Position *partial_pos;
5498
table_count= join->tables;
5499
stat= (JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5500
stat_ref= (JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
5501
table_vector= (Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5526
table_count=join->tables;
5527
stat=(JoinTable*) join->session->calloc(sizeof(JoinTable)*table_count);
5528
stat_ref=(JoinTable**) join->session->alloc(sizeof(JoinTable*)*MAX_TABLES);
5529
table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
5502
5530
if (! stat || ! stat_ref || ! table_vector)
5531
return 1; // Eom /* purecov: inspected */
5505
5533
join->best_ref=stat_vector;
5520
5548
s->needed_reg.reset();
5521
5549
table_vector[i]=s->table=table=tables->table;
5522
5550
table->pos_in_table_list= tables;
5523
assert(table->cursor);
5524
error= table->cursor->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5551
error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
5527
table->print_error(error, MYF(0));
5554
table->file->print_error(error, MYF(0));
5530
5557
table->quick_keys.reset();
5531
5558
table->reginfo.join_tab=s;
5532
5559
table->reginfo.not_exists_optimize=0;
5533
5560
memset(table->const_key_parts, 0,
5534
sizeof(key_part_map)*table->getShare()->sizeKeys());
5561
sizeof(key_part_map)*table->s->keys);
5535
5562
all_table_map|= table->map;
5537
5564
s->info=0; // For describe
5539
s->dependent= tables->getDepTables();
5566
s->dependent= tables->dep_tables;
5540
5567
s->key_dependent= 0;
5541
table->quick_condition_rows= table->cursor->stats.records;
5568
if (tables->schema_table)
5569
table->file->stats.records= 2;
5570
table->quick_condition_rows= table->file->stats.records;
5543
5572
s->on_expr_ref= &tables->on_expr;
5544
5573
if (*s->on_expr_ref)
5546
5575
/* s is the only inner table of an outer join */
5547
if (!table->cursor->stats.records && !embedding)
5576
if (!table->file->stats.records && !embedding)
5548
5577
{ // Empty table
5549
5578
s->dependent= 0; // Ignore LEFT JOIN depend.
5550
set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5579
set_position(join,const_count++,s,(KeyUse*) 0);
5553
5582
outer_join|= table->map;
5554
5583
s->embedding_map.reset();
5555
for (;embedding; embedding= embedding->getEmbedding())
5556
s->embedding_map|= embedding->getNestedJoin()->nj_map;
5584
for (;embedding; embedding= embedding->embedding)
5585
s->embedding_map|= embedding->nested_join->nj_map;
5559
if (embedding && !(false && ! embedding->getEmbedding()))
5588
if (embedding && !(false && ! embedding->embedding))
5561
5590
/* s belongs to a nested join, maybe to several embedded joins */
5562
5591
s->embedding_map.reset();
5565
nested_join_st *nested_join= embedding->getNestedJoin();
5594
nested_join_st *nested_join= embedding->nested_join;
5566
5595
s->embedding_map|= nested_join->nj_map;
5567
s->dependent|= embedding->getDepTables();
5568
embedding= embedding->getEmbedding();
5596
s->dependent|= embedding->dep_tables;
5597
embedding= embedding->embedding;
5569
5598
outer_join|= nested_join->used_tables;
5571
5600
while (embedding);
5574
if ((table->cursor->stats.records <= 1) && !s->dependent &&
5575
(table->cursor->getEngine()->check_flag(HTON_BIT_STATS_RECORDS_IS_EXACT)) &&
5603
if ((table->file->stats.records <= 1) && !s->dependent &&
5604
(table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
5576
5605
!join->no_const_tables)
5578
set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5607
set_position(join,const_count++,s,(KeyUse*) 0);
5581
5610
stat_vector[i]=0;
5676
5705
TODO. Apply single row substitution to null complemented inner tables
5677
5706
for nested outer join operations.
5679
while (keyuse->getTable() == table)
5708
while (keyuse->table == table)
5681
if (! (keyuse->getVal()->used_tables() & ~join->const_table_map) &&
5682
keyuse->getVal()->is_null() && keyuse->isNullRejected())
5710
if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
5711
keyuse->val->is_null() && keyuse->null_rejecting)
5684
5713
s->type= AM_CONST;
5685
5714
table->mark_as_null_row();
5686
5715
found_const_table_map|= table->map;
5687
5716
join->const_table_map|= table->map;
5688
set_position(join, const_count++, s, (optimizer::KeyUse*) 0);
5717
set_position(join,const_count++,s,(KeyUse*) 0);
5689
5718
goto more_const_tables_found;
5720
5749
if ((keyuse=s->keyuse))
5722
5751
s->type= AM_REF;
5723
while (keyuse->getTable() == table)
5752
while (keyuse->table == table)
5725
start_keyuse= keyuse;
5726
key= keyuse->getKey();
5754
start_keyuse=keyuse;
5727
5756
s->keys.set(key); // QQ: remove this ?
5731
5760
eq_part.reset();
5734
if (keyuse->getVal()->type() != Item::NULL_ITEM &&
5735
! keyuse->getOptimizeFlags())
5763
if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
5737
if (! ((~found_const_table_map) & keyuse->getUsedTables()))
5738
const_ref.set(keyuse->getKeypart());
5765
if (!((~found_const_table_map) & keyuse->used_tables))
5766
const_ref.set(keyuse->keypart);
5740
refs|= keyuse->getUsedTables();
5741
eq_part.set(keyuse->getKeypart());
5768
refs|=keyuse->used_tables;
5769
eq_part.set(keyuse->keypart);
5744
} while (keyuse->getTable() == table && keyuse->getKey() == key);
5772
} while (keyuse->table == table && keyuse->key == key);
5746
5774
if (is_keymap_prefix(eq_part, table->key_info[key].key_parts) &&
5747
! table->pos_in_table_list->getEmbedding())
5775
!table->pos_in_table_list->embedding)
5749
5777
if ((table->key_info[key].flags & (HA_NOSAME)) == HA_NOSAME)
6026
6049
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
6051
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
6052
joins counters and join->cur_embedding_map. It is ok to call this
6053
function for the first table in join order (for which
6071
6054
check_interleaving_with_nj has not been called)
6073
6056
@param last join table to remove, it is assumed to be the last in current
6074
6057
partial join order.
6077
6059
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)
6061
TableList *last_emb= last->table->pos_in_table_list->embedding;
6062
JOIN *join= last->join;
6065
if (last_emb->on_expr)
6067
if (!(--last_emb->nested_join->counter_))
6068
join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
6069
else if (last_emb->nested_join->join_list.elements-1 ==
6070
last_emb->nested_join->counter_)
6071
join->cur_embedding_map|= last_emb->nested_join->nj_map;
6075
last_emb= last_emb->embedding;
6080
Determine if the set is already ordered for order_st BY, so it can
6081
disable join cache because it will change the ordering of the results.
6082
Code handles sort table that is at any location (not only first after
6083
the const tables) despite the fact that it's currently prohibited.
6084
We must disable join cache if the first non-const table alone is
6085
ordered. If there is a temp table the ordering is done as a last
6086
operation and doesn't prevent join cache usage.
6088
static uint32_t make_join_orderinfo(JOIN *join)
6092
return join->tables;
6094
for (i=join->const_tables ; i < join->tables ; i++)
6096
JoinTable *tab= join->join_tab+i;
6097
Table *table= tab->table;
6098
if ((table == join->sort_by_table &&
6099
(!join->order || join->skip_sort_order)) ||
6100
(join->sort_by_table == (Table *) 1 && i != join->const_tables))
6093
join->cur_embedding_map|= nest->nj_map;