Commit 8ab81843 authored by Igor Babaev's avatar Igor Babaev

Fixed some bugs in the function that calculated the selectivity

of the table conditions. 
parent aab3c9fe
......@@ -3324,9 +3324,8 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
table->cond_selectivity= 1.0;
if (bitmap_is_clear_all(used_fields))
DBUG_RETURN(FALSE);
if (!bitmap_is_clear_all(used_fields))
{
PARAM param;
MEM_ROOT alloc;
init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0,
......@@ -3358,8 +3357,7 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
tree= get_mm_tree(&param, cond);
if (!tree)
goto end;
goto free_alloc;
for (key= tree->keys, end= key + param.keys; key != end; key++, idx++)
{
......@@ -3380,6 +3378,12 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
table->cond_selectivity*= table_field->cond_selectivity;
}
free_alloc:
thd->mem_root= param.old_root;
free_root(&alloc, MYF(0));
}
/* Calculate the selectivity of the range conditions supported by indexes */
bitmap_clear_all(used_fields);
......@@ -3411,18 +3415,19 @@ bool calculate_cond_selectivity_for_table(THD *thd, TABLE *table, Item *cond)
bitmap_set_bit(used_fields, key_part->fieldnr-1);
}
if (i)
{
table->cond_selectivity*= quick_cond_selectivity;
if (i != used_key_parts)
{
double f1= key_info->actual_rec_per_key(i-1);
double f2= key_info->actual_rec_per_key(i);
table->cond_selectivity*= quick_cond_selectivity * f1 / f2;
table->cond_selectivity*= f1 / f2;
}
}
}
}
}
end:
thd->mem_root= param.old_root;
free_root(&alloc, MYF(0));
DBUG_RETURN(FALSE);
}
......
......@@ -6987,9 +6987,12 @@ double table_cond_selectivity(JOIN *join, uint idx, JOIN_TAB *s,
/* Discount the selectivity of the access method used to join table s */
if (s->quick && s->quick->index != MAX_KEY)
{
if (!ref)
{
/* A range scan by index s->quick->index is used to access table s */
sel*= table->quick_rows[s->quick->index]/table_records;
sel*= table_records/table->quick_rows[s->quick->index];
}
}
else if (ref)
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment