diff options
Diffstat (limited to 'sql/opt_subselect.cc')
-rw-r--r-- | sql/opt_subselect.cc | 45 |
1 files changed, 18 insertions, 27 deletions
diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc index 361da4e6279..a09826ac2f0 100644 --- a/sql/opt_subselect.cc +++ b/sql/opt_subselect.cc @@ -445,7 +445,7 @@ static bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred); static bool convert_subq_to_jtbm(JOIN *parent_join, Item_in_subselect *subq_pred, bool *remove); static TABLE_LIST *alloc_join_nest(THD *thd); -static uint get_tmp_table_rec_length(Item **p_list, uint elements); +static uint get_tmp_table_rec_length(Ref_ptr_array p_list, uint elements); static double get_tmp_table_lookup_cost(THD *thd, double row_count, uint row_size); static double get_tmp_table_write_cost(THD *thd, double row_count, @@ -512,6 +512,7 @@ bool is_materialization_applicable(THD *thd, Item_in_subselect *in_subs, (Subquery is correlated to the immediate outer query && Subquery !contains {GROUP BY, ORDER BY [LIMIT], aggregate functions}) && subquery predicate is not under "NOT IN")) + 5. Subquery does not contain recursive references A note about prepared statements: we want the if-branch to be taken on PREPARE and each EXECUTE. The rewrites are only done once, but we need @@ -528,7 +529,8 @@ bool is_materialization_applicable(THD *thd, Item_in_subselect *in_subs, OPTIMIZER_SWITCH_PARTIAL_MATCH_ROWID_MERGE) || //3 optimizer_flag(thd, OPTIMIZER_SWITCH_PARTIAL_MATCH_TABLE_SCAN)) && //3 - !in_subs->is_correlated) //4 + !in_subs->is_correlated && //4 + !in_subs->with_recursive_reference) //5 { return TRUE; } @@ -1342,8 +1344,8 @@ static bool replace_where_subcondition(JOIN *join, Item **expr, static int subq_sj_candidate_cmp(Item_in_subselect* el1, Item_in_subselect* el2, void *arg) { - return (el1->sj_convert_priority > el2->sj_convert_priority) ? 1 : - ( (el1->sj_convert_priority == el2->sj_convert_priority)? 0 : -1); + return (el1->sj_convert_priority > el2->sj_convert_priority) ? -1 : + ( (el1->sj_convert_priority == el2->sj_convert_priority)? 0 : 1); } @@ -2241,13 +2243,9 @@ bool optimize_semijoin_nests(JOIN *join, table_map all_table_map) JOIN_TAB *tab= join->best_positions[i].table; join->map2table[tab->table->tablenr]= tab; } - //List_iterator<Item> it(right_expr_list); - Item **ref_array= subq_select->ref_pointer_array; - Item **ref_array_end= ref_array + subq_select->item_list.elements; table_map map= 0; - //while ((item= it++)) - for (;ref_array < ref_array_end; ref_array++) - map |= (*ref_array)->used_tables(); + for (uint i=0; i < subq_select->item_list.elements; i++) + map|= subq_select->ref_pointer_array[i]->used_tables(); map= map & ~PSEUDO_TABLE_BITS; Table_map_iterator tm_it(map); int tableno; @@ -2311,15 +2309,14 @@ bool optimize_semijoin_nests(JOIN *join, table_map all_table_map) Length of the temptable record, in bytes */ -static uint get_tmp_table_rec_length(Item **p_items, uint elements) +static uint get_tmp_table_rec_length(Ref_ptr_array p_items, uint elements) { uint len= 0; Item *item; //List_iterator<Item> it(items); - Item **p_item; - for (p_item= p_items; p_item < p_items + elements ; p_item++) + for (uint i= 0; i < elements ; i++) { - item = *p_item; + item = p_items[i]; switch (item->result_type()) { case REAL_RESULT: len += sizeof(double); @@ -3568,13 +3565,10 @@ bool setup_sj_materialization_part1(JOIN_TAB *sjm_tab) */ sjm->sjm_table_param.init(); sjm->sjm_table_param.bit_fields_as_long= TRUE; - //List_iterator<Item> it(item_list); SELECT_LEX *subq_select= emb_sj_nest->sj_subq_pred->unit->first_select(); - Item **p_item= subq_select->ref_pointer_array; - Item **p_end= p_item + subq_select->item_list.elements; - //while((right_expr= it++)) - for(;p_item != p_end; p_item++) - sjm->sjm_table_cols.push_back(*p_item, thd->mem_root); + Ref_ptr_array p_items= subq_select->ref_pointer_array; + for (uint i= 0; i < subq_select->item_list.elements; i++) + sjm->sjm_table_cols.push_back(p_items[i], thd->mem_root); sjm->sjm_table_param.field_count= subq_select->item_list.elements; sjm->sjm_table_param.force_not_null_cols= TRUE; @@ -3730,13 +3724,13 @@ bool setup_sj_materialization_part2(JOIN_TAB *sjm_tab) */ sjm->copy_field= new Copy_field[sjm->sjm_table_cols.elements]; //it.rewind(); - Item **p_item= emb_sj_nest->sj_subq_pred->unit->first_select()->ref_pointer_array; + Ref_ptr_array p_items= emb_sj_nest->sj_subq_pred->unit->first_select()->ref_pointer_array; for (uint i=0; i < sjm->sjm_table_cols.elements; i++) { bool dummy; Item_equal *item_eq; //Item *item= (it++)->real_item(); - Item *item= (*(p_item++))->real_item(); + Item *item= p_items[i]->real_item(); DBUG_ASSERT(item->type() == Item::FIELD_ITEM); Field *copy_to= ((Item_field*)item)->field; /* @@ -3961,7 +3955,7 @@ SJ_TMP_TABLE::create_sj_weedout_tmp_table(THD *thd) { /* if we run out of slots or we are not using tempool */ sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid, - thd->thread_id, thd->tmp_table++); + (ulong) thd->thread_id, thd->tmp_table++); } fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME); @@ -4181,7 +4175,6 @@ SJ_TMP_TABLE::create_sj_weedout_tmp_table(THD *thd) field->null_ptr, field->null_bit))) goto err; - key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this? } keyinfo->key_length+= key_part_info->length; } @@ -4745,8 +4738,6 @@ int clear_sj_tmp_tables(JOIN *join) { if ((res= table->file->ha_delete_all_rows())) return res; /* purecov: inspected */ - free_io_cache(table); - filesort_free_buffers(table,0); } SJ_MATERIALIZATION_INFO *sjm; @@ -5611,7 +5602,7 @@ bool JOIN::choose_subquery_plan(table_map join_tables) */ /* C.1 Compute the cost of the materialization strategy. */ //uint rowlen= get_tmp_table_rec_length(unit->first_select()->item_list); - uint rowlen= get_tmp_table_rec_length(ref_pointer_array, + uint rowlen= get_tmp_table_rec_length(ref_ptrs, select_lex->item_list.elements); /* The cost of writing one row into the temporary table. */ double write_cost= get_tmp_table_write_cost(thd, inner_record_count_1, |