~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/opt_range.cc

MergedĀ fromĀ Padraig.

Show diffs side-by-side

added added

removed removed

Lines of Context:
619
619
  SEL_TREE(enum Type type_arg) :type(type_arg) {}
620
620
  SEL_TREE() :type(KEY)
621
621
  {
622
 
    keys_map.clear_all();
 
622
    keys_map.reset();
623
623
    memset(keys, 0, sizeof(keys));
624
624
  }
625
625
  /*
633
633
 
634
634
  /*
635
635
    Possible ways to read rows using index_merge. The list is non-empty only
636
 
    if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
 
636
    if type==KEY. Currently can be non empty only if keys_map.none().
637
637
  */
638
638
  List<SEL_IMERGE> merges;
639
639
 
1044
1044
 
1045
1045
SQL_SELECT::SQL_SELECT() :quick(0),cond(0),free_cond(0)
1046
1046
{
1047
 
  quick_keys.clear_all(); needed_reg.clear_all();
 
1047
  quick_keys.reset(); 
 
1048
  needed_reg.reset();
1048
1049
  my_b_clear(&file);
1049
1050
}
1050
1051
 
1073
1074
                             ha_rows limit)
1074
1075
{
1075
1076
  key_map tmp;
1076
 
  tmp.set_all();
 
1077
  tmp.set();
1077
1078
  return test_quick_select(session, tmp, 0, limit,
1078
1079
                           force_quick_range, false) < 0;
1079
1080
}
1825
1826
 
1826
1827
  for (idx= 0; idx < table->s->keys; idx++)
1827
1828
  {
1828
 
    if (!(table->keys_in_use_for_query.is_set(idx)))
 
1829
    if (!(table->keys_in_use_for_query.test(idx)))
1829
1830
      continue;
1830
1831
    KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
1831
1832
    uint32_t n_parts=  table->key_info[idx].key_parts;
2180
2181
  double scan_time;
2181
2182
  delete quick;
2182
2183
  quick=0;
2183
 
  needed_reg.clear_all();
2184
 
  quick_keys.clear_all();
2185
 
  if (keys_to_use.is_clear_all())
 
2184
  needed_reg.reset();
 
2185
  quick_keys.reset();
 
2186
  if (keys_to_use.none())
2186
2187
    return 0;
2187
2188
  records= head->file->stats.records;
2188
2189
  if (!records)
2196
2197
  else if (read_time <= 2.0 && !force_quick_range)
2197
2198
    return 0;                           /* No need for quick select */
2198
2199
 
2199
 
  keys_to_use.intersect(head->keys_in_use_for_query);
2200
 
  if (!keys_to_use.is_clear_all())
 
2200
  keys_to_use&= head->keys_in_use_for_query;
 
2201
  if (keys_to_use.any())
2201
2202
  {
2202
2203
    MEM_ROOT alloc;
2203
2204
    SEL_TREE *tree= NULL;
2211
2212
    /* set up parameter that is passed to all functions */
2212
2213
    param.session= session;
2213
2214
    param.baseflag= head->file->ha_table_flags();
2214
 
    param.prev_tables=prev_tables | const_tables;
2215
 
    param.read_tables=read_tables;
 
2215
    param.prev_tables= prev_tables | const_tables;
 
2216
    param.read_tables= read_tables;
2216
2217
    param.current_table= head->map;
2217
2218
    param.table=head;
2218
2219
    param.keys=0;
2246
2247
    for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
2247
2248
    {
2248
2249
      KEY_PART_INFO *key_part_info;
2249
 
      if (!keys_to_use.is_set(idx))
 
2250
      if (! keys_to_use.test(idx))
2250
2251
        continue;
2251
2252
 
2252
2253
      param.key[param.keys]=key_parts;
2270
2271
    param.alloced_sel_args= 0;
2271
2272
 
2272
2273
    /* Calculate cost of full index read for the shortest covering index */
2273
 
    if (!head->covering_keys.is_clear_all())
 
2274
    if (!head->covering_keys.none())
2274
2275
    {
2275
2276
      int key_for_use= head->find_shortest_key(&head->covering_keys);
2276
2277
      double key_read_time=
2755
2756
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
2756
2757
{
2757
2758
  ROR_SCAN_INFO *ror_scan;
2758
 
  my_bitmap_map *bitmap_buf;
2759
2759
  uint32_t keynr;
2760
2760
 
2761
2761
  if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
2768
2768
                             param->table->file->ref_length);
2769
2769
  ror_scan->sel_arg= sel_arg;
2770
2770
  ror_scan->records= param->table->quick_rows[keynr];
2771
 
 
2772
 
  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
2773
 
                                                param->fields_bitmap_size)))
2774
 
    return NULL;
2775
 
 
2776
2771
  ror_scan->covered_fields.reset();
2777
2772
 
2778
2773
  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
3245
3240
  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
3246
3241
  {
3247
3242
    ROR_SCAN_INFO *scan;
3248
 
    if (!tree->ror_scans_map.is_set(idx))
 
3243
    if (! tree->ror_scans_map.test(idx))
3249
3244
      continue;
3250
3245
    if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
3251
3246
      return NULL;
3550
3545
    is defined as "not null".
3551
3546
  */
3552
3547
  print_sel_tree(param, tree, &tree->keys_map, "tree scans");
3553
 
  tree->ror_scans_map.clear_all();
 
3548
  tree->ror_scans_map.reset();
3554
3549
  tree->n_ror_scans= 0;
3555
3550
  for (idx= 0,key=tree->keys, end=key+param->keys; key != end; key++,idx++)
3556
3551
  {
3563
3558
      uint32_t keynr= param->real_keynr[idx];
3564
3559
      if ((*key)->type == SEL_ARG::MAYBE_KEY ||
3565
3560
          (*key)->maybe_flag)
3566
 
        param->needed_reg->set_bit(keynr);
 
3561
        param->needed_reg->set(keynr);
3567
3562
 
3568
3563
      bool read_index_only= index_read_must_be_used ||
3569
 
                            param->table->covering_keys.is_set(keynr);
 
3564
                            param->table->covering_keys.test(keynr);
3570
3565
 
3571
3566
      found_records= check_quick_select(param, idx, read_index_only, *key,
3572
3567
                                        update_tbl_stats, &mrr_flags,
3575
3570
      if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
3576
3571
      {
3577
3572
        tree->n_ror_scans++;
3578
 
        tree->ror_scans_map.set_bit(idx);
 
3573
        tree->ror_scans_map.set(idx);
3579
3574
      }
3580
3575
      if (read_time > found_read_time && found_records != HA_POS_ERROR)
3581
3576
      {
3596
3591
                                                    best_mrr_flags)))
3597
3592
    {
3598
3593
      read_plan->records= best_records;
3599
 
      read_plan->is_ror= tree->ror_scans_map.is_set(idx);
 
3594
      read_plan->is_ror= tree->ror_scans_map.test(idx);
3600
3595
      read_plan->read_cost= read_time;
3601
3596
      read_plan->mrr_buf_size= best_buf_size;
3602
3597
    }
4303
4298
      }
4304
4299
      sel_arg->part=(unsigned char) key_part->part;
4305
4300
      tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
4306
 
      tree->keys_map.set_bit(key_part->key);
 
4301
      tree->keys_map.set(key_part->key);
4307
4302
    }
4308
4303
  }
4309
4304
 
4792
4787
    return(tree1);
4793
4788
  }
4794
4789
  key_map  result_keys;
4795
 
  result_keys.clear_all();
 
4790
  result_keys.reset();
4796
4791
 
4797
4792
  /* Join the trees key per key */
4798
4793
  SEL_ARG **key1,**key2,**end;
4812
4807
        tree1->type= SEL_TREE::IMPOSSIBLE;
4813
4808
        return(tree1);
4814
4809
      }
4815
 
      result_keys.set_bit(key1 - tree1->keys);
 
4810
      result_keys.set(key1 - tree1->keys);
4816
4811
#ifdef EXTRA_DEBUG
4817
4812
        if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4818
4813
          (*key1)->test_use_count(*key1);
4821
4816
  }
4822
4817
  tree1->keys_map= result_keys;
4823
4818
  /* dispose index_merge if there is a "range" option */
4824
 
  if (!result_keys.is_clear_all())
 
4819
  if (result_keys.any())
4825
4820
  {
4826
4821
    tree1->merges.empty();
4827
4822
    return(tree1);
4843
4838
                           RANGE_OPT_PARAM* param)
4844
4839
{
4845
4840
  key_map common_keys= tree1->keys_map;
4846
 
  common_keys.intersect(tree2->keys_map);
 
4841
  common_keys&= tree2->keys_map;
4847
4842
 
4848
 
  if (common_keys.is_clear_all())
 
4843
  if (common_keys.none())
4849
4844
    return false;
4850
4845
 
4851
4846
  /* trees have a common key, check if they refer to same key part */
4852
4847
  SEL_ARG **key1,**key2;
4853
4848
  for (uint32_t key_no=0; key_no < param->keys; key_no++)
4854
4849
  {
4855
 
    if (common_keys.is_set(key_no))
 
4850
    if (common_keys.test(key_no))
4856
4851
    {
4857
4852
      key1= tree1->keys + key_no;
4858
4853
      key2= tree2->keys + key_no;
4931
4926
      if (tree->keys[i]->part)
4932
4927
      {
4933
4928
        tree->keys[i]= NULL;
4934
 
        tree->keys_map.clear_bit(i);
 
4929
        tree->keys_map.reset(i);
4935
4930
      }
4936
4931
      else
4937
4932
        res= true;
4957
4952
 
4958
4953
  SEL_TREE *result= 0;
4959
4954
  key_map  result_keys;
4960
 
  result_keys.clear_all();
 
4955
  result_keys.reset();
4961
4956
  if (sel_trees_can_be_ored(tree1, tree2, param))
4962
4957
  {
4963
4958
    /* Join the trees key per key */
4969
4964
      if (*key1)
4970
4965
      {
4971
4966
        result=tree1;                           // Added to tree1
4972
 
        result_keys.set_bit(key1 - tree1->keys);
 
4967
        result_keys.set(key1 - tree1->keys);
4973
4968
#ifdef EXTRA_DEBUG
4974
4969
        if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4975
4970
          (*key1)->test_use_count(*key1);
6314
6309
    param->table->quick_rows[keynr]=rows;
6315
6310
    if (update_tbl_stats)
6316
6311
    {
6317
 
      param->table->quick_keys.set_bit(keynr);
 
6312
      param->table->quick_keys.set(keynr);
6318
6313
      param->table->quick_key_parts[keynr]=param->max_key_part+1;
6319
6314
      param->table->quick_n_ranges[keynr]= param->range_count;
6320
6315
      param->table->quick_condition_rows=
8046
8041
       cur_index_info++, cur_index++)
8047
8042
  {
8048
8043
    /* Check (B1) - if current index is covering. */
8049
 
    if (!table->covering_keys.is_set(cur_index))
 
8044
    if (!table->covering_keys.test(cur_index))
8050
8045
      goto next_index;
8051
8046
 
8052
8047
    /*
8070
8065
          part of 'cur_index'
8071
8066
        */
8072
8067
        if (table->read_set->test(cur_field->field_index) &&
8073
 
            !cur_field->part_of_key_not_clustered.is_set(cur_index))
 
8068
            !cur_field->part_of_key_not_clustered.test(cur_index))
8074
8069
          goto next_index;                  // Field was not part of key
8075
8070
      }
8076
8071
    }
8114
8109
    else if (join->select_distinct)
8115
8110
    {
8116
8111
      select_items_it.rewind();
8117
 
      cur_used_key_parts.clear_all();
 
8112
      cur_used_key_parts.reset();
8118
8113
      uint32_t max_key_part= 0;
8119
8114
      while ((item= select_items_it++))
8120
8115
      {
8125
8120
          Check if this attribute was already present in the select list.
8126
8121
          If it was present, then its corresponding key part was alredy used.
8127
8122
        */
8128
 
        if (cur_used_key_parts.is_set(key_part_nr))
 
8123
        if (cur_used_key_parts.test(key_part_nr))
8129
8124
          continue;
8130
8125
        if (key_part_nr < 1 || key_part_nr > join->fields_list.elements)
8131
8126
          goto next_index;
8132
8127
        cur_part= cur_index_info->key_part + key_part_nr - 1;
8133
8128
        cur_group_prefix_len+= cur_part->store_length;
8134
 
        cur_used_key_parts.set_bit(key_part_nr);
 
8129
        cur_used_key_parts.set(key_part_nr);
8135
8130
        ++cur_group_key_parts;
8136
8131
        max_key_part= cmax(max_key_part,key_part_nr);
8137
8132
      }
8141
8136
        all_parts have all bits set from 0 to (max_key_part-1).
8142
8137
        cur_parts have bits set for only used keyparts.
8143
8138
      */
8144
 
      uint64_t all_parts, cur_parts;
8145
 
      all_parts= (1<<max_key_part) - 1;
8146
 
      cur_parts= cur_used_key_parts.to_uint64_t() >> 1;
 
8139
      key_map all_parts, cur_parts;
 
8140
      for (uint32_t pos= 0; pos < max_key_part; pos++)
 
8141
        all_parts.set(pos);
 
8142
      cur_parts= cur_used_key_parts >> 1;
8147
8143
      if (all_parts != cur_parts)
8148
8144
        goto next_index;
8149
8145
    }
9845
9841
       key != end ;
9846
9842
       key++,idx++)
9847
9843
  {
9848
 
    if (tree_map->is_set(idx))
 
9844
    if (tree_map->test(idx))
9849
9845
    {
9850
9846
      uint32_t keynr= param->real_keynr[idx];
9851
9847
      if (tmp.length())