699
698
bool quick; // Don't calulate possible keys
701
700
uint32_t fields_bitmap_size;
702
bitset<MAX_FIELDS> needed_fields; /* bitmask of fields needed by the query */
703
bitset<MAX_FIELDS> tmp_covered_fields;
701
MY_BITMAP needed_fields; /* bitmask of fields needed by the query */
702
MY_BITMAP tmp_covered_fields;
705
704
key_map *needed_reg; /* ptr to SQL_SELECT::needed_reg */
1093
1092
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(Session *session, Table *table, uint32_t key_nr,
1094
bool no_alloc, MEM_ROOT *parent_alloc)
1093
bool no_alloc, MEM_ROOT *parent_alloc,
1095
1095
:free_file(0),cur_range(NULL),last_range(0),dont_free(0)
1097
my_bitmap_map *bitmap;
1097
1099
in_ror_merged_scan= 0;
1118
1120
save_read_set= head->read_set;
1119
1121
save_write_set= head->write_set;
1123
/* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */
1124
if (!(bitmap= (my_bitmap_map*) malloc(head->s->column_bitmap_size)))
1126
column_bitmap.bitmap= 0;
1130
bitmap_init(&column_bitmap, bitmap, head->s->fields, false);
1160
1170
delete_dynamic(&ranges); /* ranges are allocated in alloc */
1161
1171
free_root(&alloc,MYF(0));
1172
free((char*) column_bitmap.bitmap);
1163
1174
head->column_bitmaps_set(save_read_set, save_write_set);
1164
1175
if (mrr_buf_desc)
1345
1356
head->prepare_for_position();
1346
1357
head->file= org_file;
1347
column_bitmap= *(head->read_set);
1358
bitmap_copy(&column_bitmap, head->read_set);
1348
1359
head->column_bitmaps_set(&column_bitmap, &column_bitmap);
2085
2096
static int fill_used_fields_bitmap(PARAM *param)
2087
2098
Table *table= param->table;
2101
param->tmp_covered_fields.bitmap= 0;
2089
2102
param->fields_bitmap_size= table->s->column_bitmap_size;
2103
if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
2104
param->fields_bitmap_size)) ||
2105
bitmap_init(¶m->needed_fields, tmp, table->s->fields, false))
2091
param->needed_fields = *(table->read_set);
2092
param->needed_fields |= *(table->write_set);
2108
bitmap_copy(¶m->needed_fields, table->read_set);
2109
bitmap_union(¶m->needed_fields, table->write_set);
2094
2111
pk= param->table->s->primary_key;
2095
2112
if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
2099
2116
KEY_PART_INFO *key_part_end= key_part +
2100
2117
param->table->key_info[pk].key_parts;
2101
2118
for (;key_part != key_part_end; ++key_part)
2102
param->needed_fields.reset(key_part->fieldnr-1);
2119
bitmap_clear_bit(¶m->needed_fields, key_part->fieldnr-1);
2722
2739
SEL_ARG *sel_arg;
2724
2741
/* Fields used in the query and covered by this ROR scan. */
2725
bitset<MAX_FIELDS> covered_fields;
2742
MY_BITMAP covered_fields;
2726
2743
uint32_t used_fields_covered; /* # of set bits in covered_fields */
2727
2744
int key_rec_length; /* length of key record (including rowid) */
2773
2790
param->fields_bitmap_size)))
2776
ror_scan->covered_fields.reset();
2793
if (bitmap_init(&ror_scan->covered_fields, bitmap_buf,
2794
param->table->s->fields, false))
2796
bitmap_clear_all(&ror_scan->covered_fields);
2778
2798
KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
2779
2799
KEY_PART_INFO *key_part_end= key_part +
2780
2800
param->table->key_info[keynr].key_parts;
2781
2801
for (;key_part != key_part_end; ++key_part)
2783
if (param->needed_fields.test(key_part->fieldnr-1))
2784
ror_scan->covered_fields.set(key_part->fieldnr-1);
2803
if (bitmap_is_set(¶m->needed_fields, key_part->fieldnr-1))
2804
bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
2786
2806
double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
2787
2807
ror_scan->index_read_cost=
2851
2871
const PARAM *param;
2852
bitset<MAX_FIELDS> covered_fields; /* union of fields covered by all scans */
2872
MY_BITMAP covered_fields; /* union of fields covered by all scans */
2854
2874
Fraction of table records that satisfies conditions of all scans.
2855
2875
This is the number of full records that will be retrieved if a
2881
2901
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
2883
2903
ROR_INTERSECT_INFO *info;
2884
2905
if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
2885
2906
sizeof(ROR_INTERSECT_INFO))))
2887
2908
info->param= param;
2909
if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
2910
param->fields_bitmap_size)))
2912
if (bitmap_init(&info->covered_fields, buf, param->table->s->fields,
2888
2915
info->is_covering= false;
2889
2916
info->index_scan_costs= 0.0;
2890
2917
info->index_records= 0;
2891
2918
info->out_rows= (double) param->table->file->stats.records;
2892
info->covered_fields.reset();
2919
bitmap_clear_all(&info->covered_fields);
2896
2923
void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
2898
2925
dst->param= src->param;
2899
dst->covered_fields= src->covered_fields;
2926
memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap,
2927
no_bytes_in_map(&src->covered_fields));
2900
2928
dst->out_rows= src->out_rows;
2901
2929
dst->is_covering= src->is_covering;
2902
2930
dst->index_records= src->index_records;
3005
3033
SEL_ARG *sel_arg, *tuple_arg= NULL;
3006
3034
key_part_map keypart_map= 0;
3007
3035
bool cur_covered;
3008
bool prev_covered= test(info->covered_fields.test(key_part->fieldnr-1));
3036
bool prev_covered= test(bitmap_is_set(&info->covered_fields,
3037
key_part->fieldnr-1));
3009
3038
key_range min_range;
3010
3039
key_range max_range;
3011
3040
min_range.key= key_val;
3017
3046
for (sel_arg= scan->sel_arg; sel_arg;
3018
3047
sel_arg= sel_arg->next_key_part)
3020
cur_covered= test(info->covered_fields.test(key_part[sel_arg->part].fieldnr-1));
3049
cur_covered= test(bitmap_is_set(&info->covered_fields,
3050
key_part[sel_arg->part].fieldnr-1));
3021
3051
if (cur_covered != prev_covered)
3023
3053
/* create (part1val, ..., part{n-1}val) tuple. */
3130
3160
info->index_records += info->param->table->quick_rows[ror_scan->keynr];
3131
3161
info->index_scan_costs += ror_scan->index_read_cost;
3132
info->covered_fields |= ror_scan->covered_fields;
3133
if (!info->is_covering && isBitmapSubset(&info->param->needed_fields,
3134
&info->covered_fields))
3162
bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
3163
if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
3164
&info->covered_fields))
3136
3166
info->is_covering= true;
3419
3450
/*I=set of all covering indexes */
3420
3451
ror_scan_mark= tree->ror_scans;
3422
bitset<MAX_FIELDS> *covered_fields= ¶m->tmp_covered_fields;
3423
covered_fields->reset();
3453
MY_BITMAP *covered_fields= ¶m->tmp_covered_fields;
3454
if (!covered_fields->bitmap)
3455
covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
3456
param->fields_bitmap_size);
3457
if (!covered_fields->bitmap ||
3458
bitmap_init(covered_fields, covered_fields->bitmap,
3459
param->table->s->fields, false))
3461
bitmap_clear_all(covered_fields);
3425
3463
double total_cost= 0.0f;
3426
3464
ha_rows records=0;
3440
3478
for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
3442
/* subtract a bitset */
3443
(*scan)->covered_fields &= covered_fields->flip();
3444
covered_fields->flip();
3445
(*scan)->used_fields_covered= (*scan)->covered_fields.count();
3446
(*scan)->first_uncovered_field= getFirstBitPos((*scan)->covered_fields);
3480
bitmap_subtract(&(*scan)->covered_fields, covered_fields);
3481
(*scan)->used_fields_covered=
3482
bitmap_bits_set(&(*scan)->covered_fields);
3483
(*scan)->first_uncovered_field=
3484
bitmap_get_first(&(*scan)->covered_fields);
3449
3487
my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
3459
3497
if (total_cost > read_time)
3461
3499
/* F=F-covered by first(I) */
3462
*covered_fields |= (*ror_scan_mark)->covered_fields;
3463
all_covered= isBitmapSubset(¶m->needed_fields, covered_fields);
3500
bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
3501
all_covered= bitmap_is_subset(¶m->needed_fields, covered_fields);
3464
3502
} while ((++ror_scan_mark < ror_scans_end) && !all_covered);
3466
3504
if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
6445
6483
quick=new QUICK_RANGE_SELECT(param->session, param->table,
6446
6484
param->real_keynr[idx],
6447
test(parent_alloc), NULL);
6485
test(parent_alloc), NULL, &create_err);
6636
bool QUICK_SELECT_I::is_keys_used(const bitset<MAX_FIELDS> *fields)
6674
bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
6638
6676
return is_key_used(head, index, fields);
6641
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const bitset<MAX_FIELDS> *fields)
6643
QUICK_RANGE_SELECT *quick;
6644
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6645
while ((quick= it++))
6647
if (is_key_used(head, quick->index, fields))
6653
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const bitset<MAX_FIELDS> *fields)
6655
QUICK_RANGE_SELECT *quick;
6656
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6657
while ((quick= it++))
6659
if (is_key_used(head, quick->index, fields))
6665
bool QUICK_ROR_UNION_SELECT::is_keys_used(const bitset<MAX_FIELDS> *fields)
6679
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const MY_BITMAP *fields)
6681
QUICK_RANGE_SELECT *quick;
6682
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6683
while ((quick= it++))
6685
if (is_key_used(head, quick->index, fields))
6691
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
6693
QUICK_RANGE_SELECT *quick;
6694
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6695
while ((quick= it++))
6697
if (is_key_used(head, quick->index, fields))
6703
bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
6667
6705
QUICK_SELECT_I *quick;
6668
6706
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
6709
6747
old_root= session->mem_root;
6710
6748
/* The following call may change session->mem_root */
6711
quick= new QUICK_RANGE_SELECT(session, table, ref->key, 0, 0);
6749
quick= new QUICK_RANGE_SELECT(session, table, ref->key, 0, 0, &create_err);
6712
6750
/* save mem_root set by QUICK_RANGE_SELECT constructor */
6713
6751
alloc= session->mem_root;
8063
8101
If the field is used in the current query ensure that it's
8064
8102
part of 'cur_index'
8066
if (table->read_set->test(cur_field->field_index) &&
8104
if (bitmap_is_set(table->read_set, cur_field->field_index) &&
8067
8105
!cur_field->part_of_key_not_clustered.is_set(cur_index))
8068
8106
goto next_index; // Field was not part of key
8235
8273
(min_max_arg_part && (min_max_arg_part < last_part));
8236
8274
for (; cur_part != last_part; cur_part++)
8238
if (table->read_set->test(cur_part->field->field_index))
8276
if (bitmap_is_set(table->read_set, cur_part->field->field_index))
8239
8277
goto next_index;