1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
Fix that MAYBE_KEY are stored in the tree so that we can detect use
19
of full hash keys for queries like:
21
select s.id, kws.keyword_id from sites as s,kws where s.id=kws.site_id and kws.keyword_id in (204,205);
29
A module that accepts a condition, index (or partitioning) description,
30
and builds lists of intervals (in index/partitioning space), such that
31
all possible records that match the condition are contained within the
33
The entry point for the range analysis module is get_mm_tree() function.
35
The lists are returned in form of complicated structure of interlinked
36
SEL_TREE/SEL_IMERGE/SEL_ARG objects.
37
See quick_range_seq_next, find_used_partitions for examples of how to walk
39
All direct "users" of this module are located within this file, too.
42
PartitionPruningModule
43
A module that accepts a partitioned table, condition, and finds which
44
partitions we will need to use in query execution. Search down for
45
"PartitionPruningModule" for description.
46
The module has single entry point - prune_partitions() function.
49
Range/index_merge/groupby-minmax optimizer module
50
A module that accepts a table, condition, and returns
51
- a QUICK_*_SELECT object that can be used to retrieve rows that match
52
the specified condition, or a "no records will match the condition"
55
The module entry points are
57
get_quick_select_for_ref()
60
Record retrieval code for range/index_merge/groupby-min-max.
61
Implementations of QUICK_*_SELECT classes.
65
The code in this file (and elsewhere) makes operations on key value tuples.
66
Those tuples are stored in the following format:
68
The tuple is a sequence of key part values. The length of key part value
69
depends only on its type (and not depends on the what value is stored)
71
KeyTuple: keypart1-data, keypart2-data, ...
73
The value of each keypart is stored in the following format:
75
keypart_data: [isnull_byte] keypart-value-bytes
77
If a keypart may have a NULL value (key_part->field->real_maybe_null() can
78
be used to check this), then the first byte is a NULL indicator with the
79
following valid values:
80
1 - keypart has NULL value.
81
0 - keypart has non-NULL value.
83
<questionable-statement> If isnull_byte==1 (NULL value), then the following
84
keypart->length bytes must be 0.
85
</questionable-statement>
87
keypart-value-bytes holds the value. Its format depends on the field type.
88
The length of keypart-value-bytes may or may not depend on the value being
89
stored. The default is that length is static and equal to
90
KEY_PART_INFO::length.
92
Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the
95
keypart-value-bytes: value_length value_bytes
97
The value_length part itself occupies HA_KEY_BLOB_LENGTH=2 bytes.
99
See key_copy() and key_restore() for code to move data between index tuple
102
CAUTION: the above description is only sergefp's understanding of the
103
subject and may omit some details.
106
#ifdef USE_PRAGMA_IMPLEMENTATION
107
#pragma implementation // gcc: Class implementation
110
#include "mysql_priv.h"
112
#include "sql_select.h"
115
#define test_rb_tree(A,B) {}
116
#define test_use_count(A) {}
120
Convert double value to #rows. Currently this does floor(), and we
121
might consider using round() instead.
123
#define double2rows(x) ((ha_rows)(x))
125
static int sel_cmp(Field *f,uchar *a,uchar *b,uint8 a_flag,uint8 b_flag);
127
static uchar is_null_string[2]= {1,0};
129
class RANGE_OPT_PARAM;
131
A construction block of the SEL_ARG-graph.
133
The following description only covers graphs of SEL_ARG objects with
134
sel_arg->type==KEY_RANGE:
136
One SEL_ARG object represents an "elementary interval" in form
138
min_value <=? table.keypartX <=? max_value
140
The interval is a non-empty interval of any kind: with[out] minimum/maximum
141
bound, [half]open/closed, single-point interval, etc.
143
1. SEL_ARG GRAPH STRUCTURE
145
SEL_ARG objects are linked together in a graph. The meaning of the graph
146
is better demostrated by an example:
151
| part=1 $ part=2 $ part=3
153
| +-------+ $ +-------+ $ +--------+
154
| | kp1<1 |--$-->| kp2=5 |--$-->| kp3=10 |
155
| +-------+ $ +-------+ $ +--------+
161
\->| kp1=2 |--$--------------$-+
162
+-------+ $ $ | +--------+
164
+-------+ $ $ | +--------+
165
| kp1=3 |--$--------------$-+ |
166
+-------+ $ $ +--------+
170
The entire graph is partitioned into "interval lists".
172
An interval list is a sequence of ordered disjoint intervals over the same
173
key part. SEL_ARG are linked via "next" and "prev" pointers. Additionally,
174
all intervals in the list form an RB-tree, linked via left/right/parent
175
pointers. The RB-tree root SEL_ARG object will be further called "root of the
178
In the example pic, there are 4 interval lists:
179
"kp<1 OR kp1=2 OR kp1=3", "kp2=5", "kp3=10 OR kp3=12", "kp3=11 OR kp3=13".
180
The vertical lines represent SEL_ARG::next/prev pointers.
182
In an interval list, each member X may have SEL_ARG::next_key_part pointer
183
pointing to the root of another interval list Y. The pointed interval list
184
must cover a key part with greater number (i.e. Y->part > X->part).
186
In the example pic, the next_key_part pointers are represented by
189
2. SEL_ARG GRAPH SEMANTICS
191
It represents a condition in a special form (we don't have a name for it ATM)
192
The SEL_ARG::next/prev is "OR", and next_key_part is "AND".
194
For example, the picture represents the condition in form:
195
(kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR
196
(kp1=2 AND (kp3=11 OR kp3=14)) OR
197
(kp1=3 AND (kp3=11 OR kp3=14))
202
Use get_mm_tree() to construct SEL_ARG graph from WHERE condition.
203
Then walk the SEL_ARG graph and get a list of dijsoint ordered key
204
intervals (i.e. intervals in form
206
(constA1, .., const1_K) < (keypart1,.., keypartK) < (constB1, .., constB_K)
208
Those intervals can be used to access the index. The uses are in:
209
- check_quick_select() - Walk the SEL_ARG graph and find an estimate of
210
how many table records are contained within all
212
- get_quick_select() - Walk the SEL_ARG, materialize the key intervals,
213
and create QUICK_RANGE_SELECT object that will
214
read records within these intervals.
216
4. SPACE COMPLEXITY NOTES
218
SEL_ARG graph is a representation of an ordered disjoint sequence of
219
intervals over the ordered set of index tuple values.
221
For multi-part keys, one can construct a WHERE expression such that its
222
list of intervals will be of combinatorial size. Here is an example:
224
(keypart1 IN (1,2, ..., n1)) AND
225
(keypart2 IN (1,2, ..., n2)) AND
226
(keypart3 IN (1,2, ..., n3))
228
For this WHERE clause the list of intervals will have n1*n2*n3 intervals
231
(keypart1, keypart2, keypart3) = (k1, k2, k3), where 1 <= k{i} <= n{i}
233
SEL_ARG graph structure aims to reduce the amount of required space by
234
"sharing" the elementary intervals when possible (the pic at the
235
beginning of this comment has examples of such sharing). The sharing may
236
prevent combinatorial blowup:
238
There are WHERE clauses that have combinatorial-size interval lists but
239
will be represented by a compact SEL_ARG graph.
241
(keypartN IN (1,2, ..., n1)) AND
243
(keypart2 IN (1,2, ..., n2)) AND
244
(keypart1 IN (1,2, ..., n3))
246
but not in all cases:
248
- There are WHERE clauses that do have a compact SEL_ARG-graph
249
representation but get_mm_tree() and its callees will construct a
250
graph of combinatorial size.
252
(keypart1 IN (1,2, ..., n1)) AND
253
(keypart2 IN (1,2, ..., n2)) AND
255
(keypartN IN (1,2, ..., n3))
257
- There are WHERE clauses for which the minimal possible SEL_ARG graph
258
representation will have combinatorial size.
260
By induction: Let's take any interval on some keypart in the middle:
264
Then let's AND it with this interval 'structure' from preceding and
267
(kp14=c1 AND kp16=c3) OR keypart14=c2) (*)
269
We will obtain this SEL_ARG graph:
273
+---------+ $ +---------+ $ +---------+
274
| kp14=c1 |--$-->| kp15=c0 |--$-->| kp16=c3 |
275
+---------+ $ +---------+ $ +---------+
277
+---------+ $ +---------+ $
278
| kp14=c2 |--$-->| kp15=c0 | $
279
+---------+ $ +---------+ $
282
Note that we had to duplicate "kp15=c0" and there was no way to avoid
284
The induction step: AND the obtained expression with another "wrapping"
286
When the process ends because of the limit on max. number of keyparts
289
WHERE clause length is O(3*#max_keyparts)
290
SEL_ARG graph size is O(2^(#max_keyparts/2))
292
(it is also possible to construct a case where instead of 2 in 2^n we
293
have a bigger constant, e.g. 4, and get a graph with 4^(31/2)= 2^31
296
We avoid consuming too much memory by setting a limit on the number of
297
SEL_ARG object we can construct during one range analysis invocation.
300
class SEL_ARG :public Sql_alloc
303
uint8 min_flag,max_flag,maybe_flag;
304
uint8 part; // Which key part
307
Number of children of this element in the RB-tree, plus 1 for this
312
Valid only for elements which are RB-tree roots: Number of times this
313
RB-tree is referred to (it is referred by SEL_ARG::next_key_part or by
314
SEL_TREE::keys[i] or by a temporary SEL_ARG* variable)
319
uchar *min_value,*max_value; // Pointer to range
322
eq_tree() requires that left == right == 0 if the type is MAYBE_KEY.
324
SEL_ARG *left,*right; /* R-B tree children */
325
SEL_ARG *next,*prev; /* Links for bi-directional interval list */
326
SEL_ARG *parent; /* R-B tree parent */
327
SEL_ARG *next_key_part;
328
enum leaf_color { BLACK,RED } color;
329
enum Type { IMPOSSIBLE, MAYBE, MAYBE_KEY, KEY_RANGE } type;
331
enum { MAX_SEL_ARGS = 16000 };
335
SEL_ARG(Field *,const uchar *, const uchar *);
336
SEL_ARG(Field *field, uint8 part, uchar *min_value, uchar *max_value,
337
uint8 min_flag, uint8 max_flag, uint8 maybe_flag);
338
SEL_ARG(enum Type type_arg)
339
:min_flag(0),elements(1),use_count(1),left(0),right(0),next_key_part(0),
340
color(BLACK), type(type_arg)
342
inline bool is_same(SEL_ARG *arg)
344
if (type != arg->type || part != arg->part)
346
if (type != KEY_RANGE)
348
return cmp_min_to_min(arg) == 0 && cmp_max_to_max(arg) == 0;
350
inline void merge_flags(SEL_ARG *arg) { maybe_flag|=arg->maybe_flag; }
351
inline void maybe_smaller() { maybe_flag=1; }
352
/* Return true iff it's a single-point null interval */
353
inline bool is_null_interval() { return maybe_null && max_value[0] == 1; }
354
inline int cmp_min_to_min(SEL_ARG* arg)
356
return sel_cmp(field,min_value, arg->min_value, min_flag, arg->min_flag);
358
inline int cmp_min_to_max(SEL_ARG* arg)
360
return sel_cmp(field,min_value, arg->max_value, min_flag, arg->max_flag);
362
inline int cmp_max_to_max(SEL_ARG* arg)
364
return sel_cmp(field,max_value, arg->max_value, max_flag, arg->max_flag);
366
inline int cmp_max_to_min(SEL_ARG* arg)
368
return sel_cmp(field,max_value, arg->min_value, max_flag, arg->min_flag);
370
SEL_ARG *clone_and(SEL_ARG* arg)
371
{ // Get overlapping range
372
uchar *new_min,*new_max;
373
uint8 flag_min,flag_max;
374
if (cmp_min_to_min(arg) >= 0)
376
new_min=min_value; flag_min=min_flag;
380
new_min=arg->min_value; flag_min=arg->min_flag; /* purecov: deadcode */
382
if (cmp_max_to_max(arg) <= 0)
384
new_max=max_value; flag_max=max_flag;
388
new_max=arg->max_value; flag_max=arg->max_flag;
390
return new SEL_ARG(field, part, new_min, new_max, flag_min, flag_max,
391
test(maybe_flag && arg->maybe_flag));
393
SEL_ARG *clone_first(SEL_ARG *arg)
394
{ // min <= X < arg->min
395
return new SEL_ARG(field,part, min_value, arg->min_value,
396
min_flag, arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX,
397
maybe_flag | arg->maybe_flag);
399
SEL_ARG *clone_last(SEL_ARG *arg)
400
{ // min <= X <= key_max
401
return new SEL_ARG(field, part, min_value, arg->max_value,
402
min_flag, arg->max_flag, maybe_flag | arg->maybe_flag);
404
SEL_ARG *clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next);
406
bool copy_min(SEL_ARG* arg)
407
{ // Get overlapping range
408
if (cmp_min_to_min(arg) > 0)
410
min_value=arg->min_value; min_flag=arg->min_flag;
411
if ((max_flag & (NO_MAX_RANGE | NO_MIN_RANGE)) ==
412
(NO_MAX_RANGE | NO_MIN_RANGE))
413
return 1; // Full range
415
maybe_flag|=arg->maybe_flag;
418
bool copy_max(SEL_ARG* arg)
419
{ // Get overlapping range
420
if (cmp_max_to_max(arg) <= 0)
422
max_value=arg->max_value; max_flag=arg->max_flag;
423
if ((max_flag & (NO_MAX_RANGE | NO_MIN_RANGE)) ==
424
(NO_MAX_RANGE | NO_MIN_RANGE))
425
return 1; // Full range
427
maybe_flag|=arg->maybe_flag;
431
void copy_min_to_min(SEL_ARG *arg)
433
min_value=arg->min_value; min_flag=arg->min_flag;
435
void copy_min_to_max(SEL_ARG *arg)
437
max_value=arg->min_value;
438
max_flag=arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX;
440
void copy_max_to_min(SEL_ARG *arg)
442
min_value=arg->max_value;
443
min_flag=arg->max_flag & NEAR_MAX ? 0 : NEAR_MIN;
445
/* returns a number of keypart values (0 or 1) appended to the key buffer */
446
int store_min(uint length, uchar **min_key,uint min_key_flag)
448
/* "(kp1 > c1) AND (kp2 OP c2) AND ..." -> (kp1 > c1) */
449
if ((!(min_flag & NO_MIN_RANGE) &&
450
!(min_key_flag & (NO_MIN_RANGE | NEAR_MIN))))
452
if (maybe_null && *min_value)
455
bzero(*min_key+1,length-1);
458
memcpy(*min_key,min_value,length);
464
/* returns a number of keypart values (0 or 1) appended to the key buffer */
465
int store_max(uint length, uchar **max_key, uint max_key_flag)
467
if (!(max_flag & NO_MAX_RANGE) &&
468
!(max_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
470
if (maybe_null && *max_value)
473
bzero(*max_key+1,length-1);
476
memcpy(*max_key,max_value,length);
483
/* returns a number of keypart values appended to the key buffer */
484
int store_min_key(KEY_PART *key, uchar **range_key, uint *range_key_flag)
486
SEL_ARG *key_tree= first();
487
uint res= key_tree->store_min(key[key_tree->part].store_length,
488
range_key, *range_key_flag);
489
*range_key_flag|= key_tree->min_flag;
491
if (key_tree->next_key_part &&
492
key_tree->next_key_part->part == key_tree->part+1 &&
493
!(*range_key_flag & (NO_MIN_RANGE | NEAR_MIN)) &&
494
key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
495
res+= key_tree->next_key_part->store_min_key(key, range_key,
500
/* returns a number of keypart values appended to the key buffer */
501
int store_max_key(KEY_PART *key, uchar **range_key, uint *range_key_flag)
503
SEL_ARG *key_tree= last();
504
uint res=key_tree->store_max(key[key_tree->part].store_length,
505
range_key, *range_key_flag);
506
(*range_key_flag)|= key_tree->max_flag;
507
if (key_tree->next_key_part &&
508
key_tree->next_key_part->part == key_tree->part+1 &&
509
!(*range_key_flag & (NO_MAX_RANGE | NEAR_MAX)) &&
510
key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
511
res+= key_tree->next_key_part->store_max_key(key, range_key,
516
SEL_ARG *insert(SEL_ARG *key);
517
SEL_ARG *tree_delete(SEL_ARG *key);
518
SEL_ARG *find_range(SEL_ARG *key);
519
SEL_ARG *rb_insert(SEL_ARG *leaf);
520
friend SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key, SEL_ARG *par);
522
friend int test_rb_tree(SEL_ARG *element,SEL_ARG *parent);
523
void test_use_count(SEL_ARG *root);
528
inline bool simple_key()
530
return !next_key_part && elements == 1;
532
void increment_use_count(long count)
536
next_key_part->use_count+=count;
537
count*= (next_key_part->use_count-count);
538
for (SEL_ARG *pos=next_key_part->first(); pos ; pos=pos->next)
539
if (pos->next_key_part)
540
pos->increment_use_count(count);
545
for (SEL_ARG *pos=first(); pos ; pos=pos->next)
546
if (pos->next_key_part)
548
pos->next_key_part->use_count--;
549
pos->next_key_part->free_tree();
553
inline SEL_ARG **parent_ptr()
555
return parent->left == this ? &parent->left : &parent->right;
560
Check if this SEL_ARG object represents a single-point interval
566
Check if this SEL_ARG object (not tree) represents a single-point
567
interval, i.e. if it represents a "keypart = const" or
571
TRUE This SEL_ARG object represents a singlepoint interval
575
bool is_singlepoint()
578
Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field)
579
flags, and the same for right edge.
581
if (min_flag || max_flag)
583
uchar *min_val= min_value;
584
uchar *max_val= max_value;
588
/* First byte is a NULL value indicator */
589
if (*min_val != *max_val)
593
return TRUE; /* This "x IS NULL" */
597
return !field->key_cmp(min_val, max_val);
599
SEL_ARG *clone_tree(RANGE_OPT_PARAM *param);
605
class SEL_TREE :public Sql_alloc
609
Starting an effort to document this field:
610
(for some i, keys[i]->type == SEL_ARG::IMPOSSIBLE) =>
611
(type == SEL_TREE::IMPOSSIBLE)
613
enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;
614
SEL_TREE(enum Type type_arg) :type(type_arg) {}
615
SEL_TREE() :type(KEY)
617
keys_map.clear_all();
618
bzero((char*) keys,sizeof(keys));
621
Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
622
keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
623
merit in range analyzer functions (e.g. get_mm_parts) returning a
624
pointer to such SEL_TREE instead of NULL)
626
SEL_ARG *keys[MAX_KEY];
627
key_map keys_map; /* bitmask of non-NULL elements in keys */
630
Possible ways to read rows using index_merge. The list is non-empty only
631
if type==KEY. Currently can be non empty only if keys_map.is_clear_all().
633
List<SEL_IMERGE> merges;
635
/* The members below are filled/used only after get_mm_tree is done */
636
key_map ror_scans_map; /* bitmask of ROR scan-able elements in keys */
637
uint n_ror_scans; /* number of set bits in ror_scans_map */
639
struct st_ror_scan_info **ror_scans; /* list of ROR key scans */
640
struct st_ror_scan_info **ror_scans_end; /* last ROR scan */
641
/* Note that #records for each key scan is stored in table->quick_rows */
644
class RANGE_OPT_PARAM
647
THD *thd; /* Current thread handle */
648
TABLE *table; /* Table being analyzed */
649
COND *cond; /* Used inside get_mm_tree(). */
650
table_map prev_tables;
651
table_map read_tables;
652
table_map current_table; /* Bit of the table being analyzed */
654
/* Array of parts of all keys for which range analysis is performed */
656
KEY_PART *key_parts_end;
657
MEM_ROOT *mem_root; /* Memory that will be freed when range analysis completes */
658
MEM_ROOT *old_root; /* Memory that will last until the query end */
660
Number of indexes used in range analysis (In SEL_TREE::keys only first
661
#keys elements are not empty)
666
If true, the index descriptions describe real indexes (and it is ok to
667
call field->optimize_range(real_keynr[...], ...).
668
Otherwise index description describes fake indexes.
670
bool using_real_indexes;
672
bool remove_jump_scans;
675
used_key_no -> table_key_no translation table. Only makes sense if
676
using_real_indexes==TRUE
678
uint real_keynr[MAX_KEY];
679
/* Number of SEL_ARG objects allocated by SEL_ARG::clone_tree operations */
680
uint alloced_sel_args;
681
bool force_default_mrr;
684
class PARAM : public RANGE_OPT_PARAM
687
KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
690
/* Number of ranges in the last checked tree->key */
692
uchar min_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH],
693
max_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH];
694
bool quick; // Don't calulate possible keys
696
uint fields_bitmap_size;
697
MY_BITMAP needed_fields; /* bitmask of fields needed by the query */
698
MY_BITMAP tmp_covered_fields;
700
key_map *needed_reg; /* ptr to SQL_SELECT::needed_reg */
702
uint *imerge_cost_buff; /* buffer for index_merge cost estimates */
703
uint imerge_cost_buff_size; /* size of the buffer */
705
/* TRUE if last checked tree->key can be used for ROR-scan */
707
/* Number of ranges in the last checked tree->key */
711
class TABLE_READ_PLAN;
713
class TRP_ROR_INTERSECT;
715
class TRP_ROR_INDEX_MERGE;
716
class TRP_GROUP_MIN_MAX;
718
struct st_ror_scan_info;
720
static SEL_TREE * get_mm_parts(RANGE_OPT_PARAM *param,COND *cond_func,Field *field,
721
Item_func::Functype type,Item *value,
722
Item_result cmp_type);
723
static SEL_ARG *get_mm_leaf(RANGE_OPT_PARAM *param,COND *cond_func,Field *field,
725
Item_func::Functype type,Item *value);
726
static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,COND *cond);
728
static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts);
729
static ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
730
SEL_ARG *tree, bool update_tbl_stats,
731
uint *mrr_flags, uint *bufsize,
733
//bool update_tbl_stats);
734
/*static ha_rows check_quick_keys(PARAM *param,uint index,SEL_ARG *key_tree,
735
uchar *min_key, uint min_key_flag, int,
736
uchar *max_key, uint max_key_flag, int);
739
QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint index,
740
SEL_ARG *key_tree, uint mrr_flags,
741
uint mrr_buf_size, MEM_ROOT *alloc);
742
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
743
bool index_read_must_be_used,
744
bool update_tbl_stats,
747
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
749
bool *are_all_covering);
751
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
755
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
758
TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree);
761
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
763
static void print_ror_scans_arr(TABLE *table, const char *msg,
764
struct st_ror_scan_info **start,
765
struct st_ror_scan_info **end);
766
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
769
static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
770
static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
771
static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
772
static SEL_ARG *key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2);
773
static SEL_ARG *key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
775
static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
776
bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
777
SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
778
uchar *max_key,uint max_key_flag);
779
static bool eq_tree(SEL_ARG* a,SEL_ARG *b);
781
static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
782
static bool null_part_in_key(KEY_PART *key_part, const uchar *key,
784
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, RANGE_OPT_PARAM* param);
788
SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
789
a condition in the following form:
790
(t_1||t_2||...||t_N) && (next)
792
where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
793
(t_i,t_j) contains SEL_ARGS for the same index.
795
SEL_TREE contained in SEL_IMERGE always has merges=NULL.
797
This class relies on memory manager to do the cleanup.
800
class SEL_IMERGE : public Sql_alloc
802
enum { PREALLOCED_TREES= 10};
804
SEL_TREE *trees_prealloced[PREALLOCED_TREES];
805
SEL_TREE **trees; /* trees used to do index_merge */
806
SEL_TREE **trees_next; /* last of these trees */
807
SEL_TREE **trees_end; /* end of allocated space */
809
SEL_ARG ***best_keys; /* best keys to read in SEL_TREEs */
812
trees(&trees_prealloced[0]),
814
trees_end(trees + PREALLOCED_TREES)
816
int or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree);
817
int or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree);
818
int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge);
823
Add SEL_TREE to this index_merge without any checks,
826
This function implements the following:
827
(x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are SEL_TREEs
834
int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
836
if (trees_next == trees_end)
838
const int realloc_ratio= 2; /* Double size for next round */
839
uint old_elements= (trees_end - trees);
840
uint old_size= sizeof(SEL_TREE**) * old_elements;
841
uint new_size= old_size * realloc_ratio;
842
SEL_TREE **new_trees;
843
if (!(new_trees= (SEL_TREE**)alloc_root(param->mem_root, new_size)))
845
memcpy(new_trees, trees, old_size);
847
trees_next= trees + old_elements;
848
trees_end= trees + old_elements * realloc_ratio;
850
*(trees_next++)= tree;
856
Perform OR operation on this SEL_IMERGE and supplied SEL_TREE new_tree,
857
combining new_tree with one of the trees in this SEL_IMERGE if they both
858
have SEL_ARGs for the same key.
861
or_sel_tree_with_checks()
862
param PARAM from SQL_SELECT::test_quick_select
863
new_tree SEL_TREE with type KEY or KEY_SMALLER.
866
This does the following:
867
(t_1||...||t_k)||new_tree =
869
= (t_1||...||t_k||new_tree)
871
= (t_1||....||(t_j|| new_tree)||...||t_k),
873
where t_i, y are SEL_TREEs.
874
new_tree is combined with the first t_j it has a SEL_ARG on common
875
key with. As a consequence of this, choice of keys to do index_merge
876
read may depend on the order of conditions in WHERE part of the query.
880
1 One of the trees was combined with new_tree to SEL_TREE::ALWAYS,
881
and (*this) should be discarded.
882
-1 An error occurred.
885
int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree)
887
for (SEL_TREE** tree = trees;
891
if (sel_trees_can_be_ored(*tree, new_tree, param))
893
*tree = tree_or(param, *tree, new_tree);
896
if (((*tree)->type == SEL_TREE::MAYBE) ||
897
((*tree)->type == SEL_TREE::ALWAYS))
899
/* SEL_TREE::IMPOSSIBLE is impossible here */
904
/* New tree cannot be combined with any of existing trees. */
905
return or_sel_tree(param, new_tree);
910
Perform OR operation on this index_merge and supplied index_merge list.
914
1 - One of conditions in result is always TRUE and this SEL_IMERGE
916
-1 - An error occurred
919
int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge)
921
for (SEL_TREE** tree= imerge->trees;
922
tree != imerge->trees_next;
925
if (or_sel_tree_with_checks(param, *tree))
933
Perform AND operation on two index_merge lists and store result in *im1.
936
inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
943
Perform OR operation on 2 index_merge lists, storing result in first list.
946
The following conversion is implemented:
947
(a_1 &&...&& a_N)||(b_1 &&...&& b_K) = AND_i,j(a_i || b_j) =>
950
i.e. all conjuncts except the first one are currently dropped.
951
This is done to avoid producing N*K ways to do index_merge.
953
If (a_1||b_1) produce a condition that is always TRUE, NULL is returned
954
and index_merge is discarded (while it is actually possible to try
957
As a consequence of this, choice of keys to do index_merge read may depend
958
on the order of conditions in WHERE part of the query.
961
0 OK, result is stored in *im1
962
other Error, both passed lists are unusable
965
int imerge_list_or_list(RANGE_OPT_PARAM *param,
966
List<SEL_IMERGE> *im1,
967
List<SEL_IMERGE> *im2)
969
SEL_IMERGE *imerge= im1->head();
971
im1->push_back(imerge);
973
return imerge->or_sel_imerge_with_checks(param, im2->head());
978
Perform OR operation on index_merge list and key tree.
981
0 OK, result is stored in *im1.
985
int imerge_list_or_tree(RANGE_OPT_PARAM *param,
986
List<SEL_IMERGE> *im1,
990
List_iterator<SEL_IMERGE> it(*im1);
991
while ((imerge= it++))
993
if (imerge->or_sel_tree_with_checks(param, tree))
996
return im1->is_empty();
1000
/***************************************************************************
1001
** Basic functions for SQL_SELECT and QUICK_RANGE_SELECT
1002
***************************************************************************/
1004
/* make a select from mysql info
1005
Error is set as following:
1007
1 = Got some error (out of memory?)
1010
SQL_SELECT *make_select(TABLE *head, table_map const_tables,
1011
table_map read_tables, COND *conds,
1012
bool allow_null_cond,
1016
DBUG_ENTER("make_select");
1020
if (!conds && !allow_null_cond)
1022
if (!(select= new SQL_SELECT))
1024
*error= 1; // out of memory
1025
DBUG_RETURN(0); /* purecov: inspected */
1027
select->read_tables=read_tables;
1028
select->const_tables=const_tables;
1032
if (head->sort.io_cache)
1034
select->file= *head->sort.io_cache;
1035
select->records=(ha_rows) (select->file.end_of_file/
1036
head->file->ref_length);
1037
my_free(head->sort.io_cache, MYF(0));
1038
head->sort.io_cache=0;
1040
DBUG_RETURN(select);
1044
SQL_SELECT::SQL_SELECT() :quick(0),cond(0),free_cond(0)
1046
quick_keys.clear_all(); needed_reg.clear_all();
1051
void SQL_SELECT::cleanup()
1061
close_cached_file(&file);
1065
SQL_SELECT::~SQL_SELECT()
1070
#undef index // Fix for Unixware 7
1072
QUICK_SELECT_I::QUICK_SELECT_I()
1073
:max_used_key_length(0),
1077
QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr,
1078
bool no_alloc, MEM_ROOT *parent_alloc,
1080
:free_file(0),cur_range(NULL),last_range(0),dont_free(0)
1082
my_bitmap_map *bitmap;
1083
DBUG_ENTER("QUICK_RANGE_SELECT::QUICK_RANGE_SELECT");
1085
in_ror_merged_scan= 0;
1089
key_part_info= head->key_info[index].key_part;
1090
my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16);
1092
/* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */
1093
mrr_buf_size= thd->variables.read_rnd_buff_size;
1096
if (!no_alloc && !parent_alloc)
1098
// Allocates everything through the internal memroot
1099
init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1100
thd->mem_root= &alloc;
1103
bzero((char*) &alloc,sizeof(alloc));
1105
record= head->record[0];
1106
save_read_set= head->read_set;
1107
save_write_set= head->write_set;
1109
/* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */
1110
if (!(bitmap= (my_bitmap_map*) my_malloc(head->s->column_bitmap_size,
1113
column_bitmap.bitmap= 0;
1117
bitmap_init(&column_bitmap, bitmap, head->s->fields, FALSE);
1122
int QUICK_RANGE_SELECT::init()
1124
DBUG_ENTER("QUICK_RANGE_SELECT::init");
1126
if (file->inited != handler::NONE)
1127
file->ha_index_or_rnd_end();
1128
DBUG_RETURN(file->ha_index_init(index, 1));
1132
void QUICK_RANGE_SELECT::range_end()
1134
if (file->inited != handler::NONE)
1135
file->ha_index_or_rnd_end();
1139
QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
1141
DBUG_ENTER("QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT");
1144
/* file is NULL for CPK scan on covering ROR-intersection */
1151
file->extra(HA_EXTRA_NO_KEYREAD);
1155
DBUG_PRINT("info", ("Freeing separate handler 0x%lx (free: %d)", (long) file,
1157
file->ha_external_lock(current_thd, F_UNLCK);
1162
delete_dynamic(&ranges); /* ranges are allocated in alloc */
1163
free_root(&alloc,MYF(0));
1164
my_free((char*) column_bitmap.bitmap, MYF(MY_ALLOW_ZERO_PTR));
1166
head->column_bitmaps_set(save_read_set, save_write_set);
1167
x_free(mrr_buf_desc);
1172
QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(THD *thd_param,
1174
:pk_quick_select(NULL), thd(thd_param)
1176
DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT");
1179
bzero(&read_record, sizeof(read_record));
1180
init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1184
int QUICK_INDEX_MERGE_SELECT::init()
1186
DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::init");
1190
int QUICK_INDEX_MERGE_SELECT::reset()
1192
DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::reset");
1193
DBUG_RETURN(read_keys_and_merge());
1197
QUICK_INDEX_MERGE_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick_sel_range)
1200
Save quick_select that does scan on clustered primary key as it will be
1201
processed separately.
1203
if (head->file->primary_key_is_clustered() &&
1204
quick_sel_range->index == head->s->primary_key)
1205
pk_quick_select= quick_sel_range;
1207
return quick_selects.push_back(quick_sel_range);
1211
QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT()
1213
List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
1214
QUICK_RANGE_SELECT* quick;
1215
DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT");
1217
while ((quick= quick_it++))
1219
quick_selects.delete_elements();
1220
delete pk_quick_select;
1221
free_root(&alloc,MYF(0));
1226
QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
1228
bool retrieve_full_rows,
1229
MEM_ROOT *parent_alloc)
1230
: cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows),
1235
record= head->record[0];
1237
init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1239
bzero(&alloc, sizeof(MEM_ROOT));
1240
last_rowid= (uchar*) alloc_root(parent_alloc? parent_alloc : &alloc,
1241
head->file->ref_length);
1246
Do post-constructor initialization.
1248
QUICK_ROR_INTERSECT_SELECT::init()
1255
int QUICK_ROR_INTERSECT_SELECT::init()
1257
DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init");
1258
/* Check if last_rowid was successfully allocated in ctor */
1259
DBUG_RETURN(!last_rowid);
1264
Initialize this quick select to be a ROR-merged scan.
1267
QUICK_RANGE_SELECT::init_ror_merged_scan()
1268
reuse_handler If TRUE, use head->file, otherwise create a separate
1272
This function creates and prepares for subsequent use a separate handler
1273
object if it can't reuse head->file. The reason for this is that during
1274
ROR-merge several key scans are performed simultaneously, and a single
1275
handler is only capable of preserving context of a single key scan.
1277
In ROR-merge the quick select doing merge does full records retrieval,
1278
merged quick selects read only keys.
1281
0 ROR child scan initialized, ok to use.
1285
int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler)
1287
handler *save_file= file, *org_file;
1289
DBUG_ENTER("QUICK_RANGE_SELECT::init_ror_merged_scan");
1291
in_ror_merged_scan= 1;
1294
DBUG_PRINT("info", ("Reusing handler 0x%lx", (long) file));
1295
if (init() || reset())
1299
head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1303
/* Create a separate handler object for this quick select */
1306
/* already have own 'handler' object. */
1311
if (!(file= head->file->clone(thd->mem_root)))
1314
Manually set the error flag. Note: there seems to be quite a few
1315
places where a failure could cause the server to "hang" the client by
1316
sending no response to a query. ATM those are not real errors because
1317
the storage engine calls in question happen to never fail with the
1318
existing storage engines.
1320
my_error(ER_OUT_OF_RESOURCES, MYF(0)); /* purecov: inspected */
1321
/* Caller will free the memory */
1322
goto failure; /* purecov: inspected */
1325
head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1327
if (file->ha_external_lock(thd, F_RDLCK))
1330
if (init() || reset())
1332
file->ha_external_lock(thd, F_UNLCK);
1337
last_rowid= file->ref;
1341
We are only going to read key fields and call position() on 'file'
1342
The following sets head->tmp_set to only use this key and then updates
1343
head->read_set and head->write_set to use this bitmap.
1344
The now bitmap is stored in 'column_bitmap' which is used in ::get_next()
1346
org_file= head->file;
1348
/* We don't have to set 'head->keyread' here as the 'file' is unique */
1349
if (!head->no_keyread)
1352
head->mark_columns_used_by_index(index);
1354
head->prepare_for_position();
1355
head->file= org_file;
1356
bitmap_copy(&column_bitmap, head->read_set);
1357
head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1362
head->column_bitmaps_set(save_read_set, save_write_set);
1370
Initialize this quick select to be a part of a ROR-merged scan.
1372
QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan()
1373
reuse_handler If TRUE, use head->file, otherwise create separate
1379
int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler)
1381
List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
1382
QUICK_RANGE_SELECT* quick;
1383
DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan");
1385
/* Initialize all merged "children" quick selects */
1386
DBUG_ASSERT(!need_to_fetch_row || reuse_handler);
1387
if (!need_to_fetch_row && reuse_handler)
1391
There is no use of this->file. Use it for the first of merged range
1394
if (quick->init_ror_merged_scan(TRUE))
1396
quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
1398
while ((quick= quick_it++))
1400
if (quick->init_ror_merged_scan(FALSE))
1402
quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
1403
/* All merged scans share the same record buffer in intersection. */
1404
quick->record= head->record[0];
1407
if (need_to_fetch_row && head->file->ha_rnd_init(1))
1409
DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
1417
Initialize quick select for row retrieval.
1425
int QUICK_ROR_INTERSECT_SELECT::reset()
1427
DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::reset");
1428
if (!scans_inited && init_ror_merged_scan(TRUE))
1431
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
1432
QUICK_RANGE_SELECT *quick;
1433
while ((quick= it++))
1440
Add a merged quick select to this ROR-intersection quick select.
1443
QUICK_ROR_INTERSECT_SELECT::push_quick_back()
1444
quick Quick select to be added. The quick select must return
1445
rows in rowid order.
1447
This call can only be made before init() is called.
1455
QUICK_ROR_INTERSECT_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick)
1457
return quick_selects.push_back(quick);
1460
QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT()
1462
DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT");
1463
quick_selects.delete_elements();
1465
free_root(&alloc,MYF(0));
1466
if (need_to_fetch_row && head->file->inited != handler::NONE)
1467
head->file->ha_rnd_end();
1472
QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
1474
: thd(thd_param), scans_inited(FALSE)
1478
rowid_length= table->file->ref_length;
1479
record= head->record[0];
1480
init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1481
thd_param->mem_root= &alloc;
1486
Do post-constructor initialization.
1488
QUICK_ROR_UNION_SELECT::init()
1495
int QUICK_ROR_UNION_SELECT::init()
1497
DBUG_ENTER("QUICK_ROR_UNION_SELECT::init");
1498
if (init_queue(&queue, quick_selects.elements, 0,
1499
FALSE , QUICK_ROR_UNION_SELECT::queue_cmp,
1502
bzero(&queue, sizeof(QUEUE));
1506
if (!(cur_rowid= (uchar*) alloc_root(&alloc, 2*head->file->ref_length)))
1508
prev_rowid= cur_rowid + head->file->ref_length;
1514
Comparison function to be used QUICK_ROR_UNION_SELECT::queue priority
1518
QUICK_ROR_UNION_SELECT::queue_cmp()
1519
arg Pointer to QUICK_ROR_UNION_SELECT
1520
val1 First merged select
1521
val2 Second merged select
1524
int QUICK_ROR_UNION_SELECT::queue_cmp(void *arg, uchar *val1, uchar *val2)
1526
QUICK_ROR_UNION_SELECT *self= (QUICK_ROR_UNION_SELECT*)arg;
1527
return self->head->file->cmp_ref(((QUICK_SELECT_I*)val1)->last_rowid,
1528
((QUICK_SELECT_I*)val2)->last_rowid);
1533
Initialize quick select for row retrieval.
1542
int QUICK_ROR_UNION_SELECT::reset()
1544
QUICK_SELECT_I *quick;
1546
DBUG_ENTER("QUICK_ROR_UNION_SELECT::reset");
1547
have_prev_rowid= FALSE;
1550
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
1551
while ((quick= it++))
1553
if (quick->init_ror_merged_scan(FALSE))
1558
queue_remove_all(&queue);
1560
Initialize scans for merged quick selects and put all merged quick
1561
selects into the queue.
1563
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
1564
while ((quick= it++))
1568
if ((error= quick->get_next()))
1570
if (error == HA_ERR_END_OF_FILE)
1574
quick->save_last_pos();
1575
queue_insert(&queue, (uchar*)quick);
1578
if (head->file->ha_rnd_init(1))
1580
DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
1589
QUICK_ROR_UNION_SELECT::push_quick_back(QUICK_SELECT_I *quick_sel_range)
1591
return quick_selects.push_back(quick_sel_range);
1594
QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT()
1596
DBUG_ENTER("QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT");
1597
delete_queue(&queue);
1598
quick_selects.delete_elements();
1599
if (head->file->inited != handler::NONE)
1600
head->file->ha_rnd_end();
1601
free_root(&alloc,MYF(0));
1606
QUICK_RANGE::QUICK_RANGE()
1607
:min_key(0),max_key(0),min_length(0),max_length(0),
1608
flag(NO_MIN_RANGE | NO_MAX_RANGE),
1609
min_keypart_map(0), max_keypart_map(0)
1612
SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
1615
min_flag=arg.min_flag;
1616
max_flag=arg.max_flag;
1617
maybe_flag=arg.maybe_flag;
1618
maybe_null=arg.maybe_null;
1621
min_value=arg.min_value;
1622
max_value=arg.max_value;
1623
next_key_part=arg.next_key_part;
1624
use_count=1; elements=1;
1628
inline void SEL_ARG::make_root()
1630
left=right= &null_element;
1633
use_count=0; elements=1;
1636
SEL_ARG::SEL_ARG(Field *f,const uchar *min_value_arg,
1637
const uchar *max_value_arg)
1638
:min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
1639
elements(1), use_count(1), field(f), min_value((uchar*) min_value_arg),
1640
max_value((uchar*) max_value_arg), next(0),prev(0),
1641
next_key_part(0),color(BLACK),type(KEY_RANGE)
1643
left=right= &null_element;
1646
SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
1647
uchar *min_value_, uchar *max_value_,
1648
uint8 min_flag_,uint8 max_flag_,uint8 maybe_flag_)
1649
:min_flag(min_flag_),max_flag(max_flag_),maybe_flag(maybe_flag_),
1650
part(part_),maybe_null(field_->real_maybe_null()), elements(1),use_count(1),
1651
field(field_), min_value(min_value_), max_value(max_value_),
1652
next(0),prev(0),next_key_part(0),color(BLACK),type(KEY_RANGE)
1654
left=right= &null_element;
1657
SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent,
1662
/* Bail out if we have already generated too many SEL_ARGs */
1663
if (++param->alloced_sel_args > MAX_SEL_ARGS)
1666
if (type != KEY_RANGE)
1668
if (!(tmp= new (param->mem_root) SEL_ARG(type)))
1669
return 0; // out of memory
1670
tmp->prev= *next_arg; // Link into next/prev chain
1671
(*next_arg)->next=tmp;
1676
if (!(tmp= new (param->mem_root) SEL_ARG(field,part, min_value,max_value,
1677
min_flag, max_flag, maybe_flag)))
1679
tmp->parent=new_parent;
1680
tmp->next_key_part=next_key_part;
1681
if (left != &null_element)
1682
if (!(tmp->left=left->clone(param, tmp, next_arg)))
1685
tmp->prev= *next_arg; // Link into next/prev chain
1686
(*next_arg)->next=tmp;
1689
if (right != &null_element)
1690
if (!(tmp->right= right->clone(param, tmp, next_arg)))
1693
increment_use_count(1);
1695
tmp->elements= this->elements;
1699
SEL_ARG *SEL_ARG::first()
1701
SEL_ARG *next_arg=this;
1702
if (!next_arg->left)
1703
return 0; // MAYBE_KEY
1704
while (next_arg->left != &null_element)
1705
next_arg=next_arg->left;
1709
SEL_ARG *SEL_ARG::last()
1711
SEL_ARG *next_arg=this;
1712
if (!next_arg->right)
1713
return 0; // MAYBE_KEY
1714
while (next_arg->right != &null_element)
1715
next_arg=next_arg->right;
1721
Check if a compare is ok, when one takes ranges in account
1722
Returns -2 or 2 if the ranges where 'joined' like < 2 and >= 2
1725
static int sel_cmp(Field *field, uchar *a, uchar *b, uint8 a_flag,
1729
/* First check if there was a compare to a min or max element */
1730
if (a_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
1732
if ((a_flag & (NO_MIN_RANGE | NO_MAX_RANGE)) ==
1733
(b_flag & (NO_MIN_RANGE | NO_MAX_RANGE)))
1735
return (a_flag & NO_MIN_RANGE) ? -1 : 1;
1737
if (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
1738
return (b_flag & NO_MIN_RANGE) ? 1 : -1;
1740
if (field->real_maybe_null()) // If null is part of key
1747
goto end; // NULL where equal
1748
a++; b++; // Skip NULL marker
1750
cmp=field->key_cmp(a , b);
1751
if (cmp) return cmp < 0 ? -1 : 1; // The values differed
1753
// Check if the compared equal arguments was defined with open/closed range
1755
if (a_flag & (NEAR_MIN | NEAR_MAX))
1757
if ((a_flag & (NEAR_MIN | NEAR_MAX)) == (b_flag & (NEAR_MIN | NEAR_MAX)))
1759
if (!(b_flag & (NEAR_MIN | NEAR_MAX)))
1760
return (a_flag & NEAR_MIN) ? 2 : -2;
1761
return (a_flag & NEAR_MIN) ? 1 : -1;
1763
if (b_flag & (NEAR_MIN | NEAR_MAX))
1764
return (b_flag & NEAR_MIN) ? -2 : 2;
1765
return 0; // The elements where equal
1769
SEL_ARG *SEL_ARG::clone_tree(RANGE_OPT_PARAM *param)
1771
SEL_ARG tmp_link,*next_arg,*root;
1772
next_arg= &tmp_link;
1773
if (!(root= clone(param, (SEL_ARG *) 0, &next_arg)))
1775
next_arg->next=0; // Fix last link
1776
tmp_link.next->prev=0; // Fix first link
1777
if (root) // If not OOM
1784
Find the best index to retrieve first N records in given order
1787
get_index_for_order()
1788
table Table to be accessed
1789
order Required ordering
1790
limit Number of records that will be retrieved
1793
Find the best index that allows to retrieve first #limit records in the
1794
given order cheaper then one would retrieve them using full table scan.
1797
Run through all table indexes and find the shortest index that allows
1798
records to be retrieved in given order. We look for the shortest index
1799
as we will have fewer index pages to read with it.
1801
This function is used only by UPDATE/DELETE, so we take into account how
1802
the UPDATE/DELETE code will work:
1803
* index can only be scanned in forward direction
1804
* HA_EXTRA_KEYREAD will not be used
1805
Perhaps these assumptions could be relaxed.
1808
Number of the index that produces the required ordering in the cheapest way
1809
MAX_KEY if no such index was found.
1812
uint get_index_for_order(TABLE *table, ORDER *order, ha_rows limit)
1815
uint match_key= MAX_KEY, match_key_len= MAX_KEY_LENGTH + 1;
1818
for (ord= order; ord; ord= ord->next)
1822
for (idx= 0; idx < table->s->keys; idx++)
1824
if (!(table->keys_in_use_for_query.is_set(idx)))
1826
KEY_PART_INFO *keyinfo= table->key_info[idx].key_part;
1827
uint n_parts= table->key_info[idx].key_parts;
1831
The below check is sufficient considering we now have either BTREE
1832
indexes (records are returned in order for any index prefix) or HASH
1833
indexes (records are not returned in order for any index prefix).
1835
if (!(table->file->index_flags(idx, 0, 1) & HA_READ_ORDER))
1837
for (ord= order; ord && partno < n_parts; ord= ord->next, partno++)
1839
Item *item= order->item[0];
1840
if (!(item->type() == Item::FIELD_ITEM &&
1841
((Item_field*)item)->field->eq(keyinfo[partno].field)))
1845
if (!ord && table->key_info[idx].key_length < match_key_len)
1848
Ok, the ordering is compatible and this key is shorter then
1849
previous match (we want shorter keys as we'll have to read fewer
1850
index pages for the same number of records)
1853
match_key_len= table->key_info[idx].key_length;
1857
if (match_key != MAX_KEY)
1860
Found an index that allows records to be retrieved in the requested
1861
order. Now we'll check if using the index is cheaper then doing a table
1864
double full_scan_time= table->file->scan_time();
1865
double index_scan_time= table->file->read_time(match_key, 1, limit);
1866
if (index_scan_time > full_scan_time)
1874
Table rows retrieval plan. Range optimizer creates QUICK_SELECT_I-derived
1875
objects from table read plans.
1877
class TABLE_READ_PLAN
1881
Plan read cost, with or without cost of full row retrieval, depending
1882
on plan creation parameters.
1885
ha_rows records; /* estimate of #rows to be examined */
1888
If TRUE, the scan returns rows in rowid order. This is used only for
1889
scans that can be both ROR and non-ROR.
1894
Create quick select for this plan.
1897
param Parameter from test_quick_select
1898
retrieve_full_rows If TRUE, created quick select will do full record
1900
parent_alloc Memory pool to use, if any.
1903
retrieve_full_rows is ignored by some implementations.
1906
created quick select
1909
virtual QUICK_SELECT_I *make_quick(PARAM *param,
1910
bool retrieve_full_rows,
1911
MEM_ROOT *parent_alloc=NULL) = 0;
1913
/* Table read plans are allocated on MEM_ROOT and are never deleted */
1914
static void *operator new(size_t size, MEM_ROOT *mem_root)
1915
{ return (void*) alloc_root(mem_root, (uint) size); }
1916
static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
1917
static void operator delete(void *ptr, MEM_ROOT *mem_root) { /* Never called */ }
1918
virtual ~TABLE_READ_PLAN() {} /* Remove gcc warning */
1922
class TRP_ROR_INTERSECT;
1923
class TRP_ROR_UNION;
1924
class TRP_INDEX_MERGE;
1928
Plan for a QUICK_RANGE_SELECT scan.
1929
TRP_RANGE::make_quick ignores retrieve_full_rows parameter because
1930
QUICK_RANGE_SELECT doesn't distinguish between 'index only' scans and full
1931
record retrieval scans.
1934
class TRP_RANGE : public TABLE_READ_PLAN
1937
SEL_ARG *key; /* set of intervals to be used in "range" method retrieval */
1938
uint key_idx; /* key number in PARAM::key */
1942
TRP_RANGE(SEL_ARG *key_arg, uint idx_arg, uint mrr_flags_arg)
1943
: key(key_arg), key_idx(idx_arg), mrr_flags(mrr_flags_arg)
1945
virtual ~TRP_RANGE() {} /* Remove gcc warning */
1947
QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
1948
MEM_ROOT *parent_alloc)
1950
DBUG_ENTER("TRP_RANGE::make_quick");
1951
QUICK_RANGE_SELECT *quick;
1952
if ((quick= get_quick_select(param, key_idx, key, mrr_flags, mrr_buf_size,
1955
quick->records= records;
1956
quick->read_time= read_cost;
1963
/* Plan for QUICK_ROR_INTERSECT_SELECT scan. */
1965
class TRP_ROR_INTERSECT : public TABLE_READ_PLAN
1968
TRP_ROR_INTERSECT() {} /* Remove gcc warning */
1969
virtual ~TRP_ROR_INTERSECT() {} /* Remove gcc warning */
1970
QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
1971
MEM_ROOT *parent_alloc);
1973
/* Array of pointers to ROR range scans used in this intersection */
1974
struct st_ror_scan_info **first_scan;
1975
struct st_ror_scan_info **last_scan; /* End of the above array */
1976
struct st_ror_scan_info *cpk_scan; /* Clustered PK scan, if there is one */
1977
bool is_covering; /* TRUE if no row retrieval phase is necessary */
1978
double index_scan_costs; /* SUM(cost(index_scan)) */
1983
Plan for QUICK_ROR_UNION_SELECT scan.
1984
QUICK_ROR_UNION_SELECT always retrieves full rows, so retrieve_full_rows
1985
is ignored by make_quick.
1988
class TRP_ROR_UNION : public TABLE_READ_PLAN
1991
TRP_ROR_UNION() {} /* Remove gcc warning */
1992
virtual ~TRP_ROR_UNION() {} /* Remove gcc warning */
1993
QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
1994
MEM_ROOT *parent_alloc);
1995
TABLE_READ_PLAN **first_ror; /* array of ptrs to plans for merged scans */
1996
TABLE_READ_PLAN **last_ror; /* end of the above array */
2001
Plan for QUICK_INDEX_MERGE_SELECT scan.
2002
QUICK_ROR_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
2003
is ignored by make_quick.
2006
class TRP_INDEX_MERGE : public TABLE_READ_PLAN
2009
TRP_INDEX_MERGE() {} /* Remove gcc warning */
2010
virtual ~TRP_INDEX_MERGE() {} /* Remove gcc warning */
2011
QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2012
MEM_ROOT *parent_alloc);
2013
TRP_RANGE **range_scans; /* array of ptrs to plans of merged scans */
2014
TRP_RANGE **range_scans_end; /* end of the array */
2019
Plan for a QUICK_GROUP_MIN_MAX_SELECT scan.
2022
class TRP_GROUP_MIN_MAX : public TABLE_READ_PLAN
2025
bool have_min, have_max;
2026
KEY_PART_INFO *min_max_arg_part;
2027
uint group_prefix_len;
2028
uint used_key_parts;
2029
uint group_key_parts;
2033
uchar key_infix[MAX_KEY_LENGTH];
2034
SEL_TREE *range_tree; /* Represents all range predicates in the query. */
2035
SEL_ARG *index_tree; /* The SEL_ARG sub-tree corresponding to index_info. */
2036
uint param_idx; /* Index of used key in param->key. */
2037
/* Number of records selected by the ranges in index_tree. */
2039
ha_rows quick_prefix_records;
2041
TRP_GROUP_MIN_MAX(bool have_min_arg, bool have_max_arg,
2042
KEY_PART_INFO *min_max_arg_part_arg,
2043
uint group_prefix_len_arg, uint used_key_parts_arg,
2044
uint group_key_parts_arg, KEY *index_info_arg,
2045
uint index_arg, uint key_infix_len_arg,
2046
uchar *key_infix_arg,
2047
SEL_TREE *tree_arg, SEL_ARG *index_tree_arg,
2048
uint param_idx_arg, ha_rows quick_prefix_records_arg)
2049
: have_min(have_min_arg), have_max(have_max_arg),
2050
min_max_arg_part(min_max_arg_part_arg),
2051
group_prefix_len(group_prefix_len_arg), used_key_parts(used_key_parts_arg),
2052
group_key_parts(group_key_parts_arg), index_info(index_info_arg),
2053
index(index_arg), key_infix_len(key_infix_len_arg), range_tree(tree_arg),
2054
index_tree(index_tree_arg), param_idx(param_idx_arg),
2055
quick_prefix_records(quick_prefix_records_arg)
2058
memcpy(this->key_infix, key_infix_arg, key_infix_len);
2060
virtual ~TRP_GROUP_MIN_MAX() {} /* Remove gcc warning */
2062
QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2063
MEM_ROOT *parent_alloc);
2068
Fill param->needed_fields with bitmap of fields used in the query.
2070
fill_used_fields_bitmap()
2071
param Parameter from test_quick_select function.
2074
Clustered PK members are not put into the bitmap as they are implicitly
2075
present in all keys (and it is impossible to avoid reading them).
2081
static int fill_used_fields_bitmap(PARAM *param)
2083
TABLE *table= param->table;
2086
param->tmp_covered_fields.bitmap= 0;
2087
param->fields_bitmap_size= table->s->column_bitmap_size;
2088
if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
2089
param->fields_bitmap_size)) ||
2090
bitmap_init(¶m->needed_fields, tmp, table->s->fields, FALSE))
2093
bitmap_copy(¶m->needed_fields, table->read_set);
2094
bitmap_union(¶m->needed_fields, table->write_set);
2096
pk= param->table->s->primary_key;
2097
if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
2099
/* The table uses clustered PK and it is not internally generated */
2100
KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
2101
KEY_PART_INFO *key_part_end= key_part +
2102
param->table->key_info[pk].key_parts;
2103
for (;key_part != key_part_end; ++key_part)
2104
bitmap_clear_bit(¶m->needed_fields, key_part->fieldnr-1);
2111
Test if a key can be used in different ranges
2114
SQL_SELECT::test_quick_select()
2116
keys_to_use Keys to use for range retrieval
2117
prev_tables Tables assumed to be already read when the scan is
2118
performed (but not read at the moment of this call)
2120
force_quick_range Prefer to use range (instead of full table scan) even
2121
if it is more expensive.
2124
Updates the following in the select parameter:
2125
needed_reg - Bits for keys with may be used if all prev regs are read
2126
quick - Parameter to use when reading records.
2128
In the table struct the following information is updated:
2129
quick_keys - Which keys can be used
2130
quick_rows - How many rows the key matches
2131
quick_condition_rows - E(# rows that will satisfy the table condition)
2134
quick_condition_rows value is obtained as follows:
2136
It is a minimum of E(#output rows) for all considered table access
2137
methods (range and index_merge accesses over various indexes).
2139
The obtained value is not a true E(#rows that satisfy table condition)
2140
but rather a pessimistic estimate. To obtain a true E(#...) one would
2141
need to combine estimates of various access methods, taking into account
2142
correlations between sets of rows they will return.
2144
For example, if values of tbl.key1 and tbl.key2 are independent (a right
2145
assumption if we have no information about their correlation) then the
2146
correct estimate will be:
2148
E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) =
2149
= E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)
2151
which is smaller than
2153
MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))
2155
which is currently produced.
2158
* Change the value returned in quick_condition_rows from a pessimistic
2159
estimate to true E(#rows that satisfy table condition).
2160
(we can re-use some of E(#rows) calcuation code from index_merge/intersection
2163
* Check if this function really needs to modify keys_to_use, and change the
2164
code to pass it by reference if it doesn't.
2166
* In addition to force_quick_range other means can be (an usually are) used
2167
to make this function prefer range over full table scan. Figure out if
2168
force_quick_range is really needed.
2171
-1 if impossible select (i.e. certainly no rows will be selected)
2172
0 if can't use quick_select
2173
1 if found usable ranges and quick select has been successfully created.
2176
int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
2177
table_map prev_tables,
2178
ha_rows limit, bool force_quick_range,
2179
bool ordered_output)
2183
DBUG_ENTER("SQL_SELECT::test_quick_select");
2184
DBUG_PRINT("enter",("keys_to_use: %lu prev_tables: %lu const_tables: %lu",
2185
(ulong) keys_to_use.to_ulonglong(), (ulong) prev_tables,
2186
(ulong) const_tables));
2187
DBUG_PRINT("info", ("records: %lu", (ulong) head->file->stats.records));
2190
needed_reg.clear_all();
2191
quick_keys.clear_all();
2192
if (keys_to_use.is_clear_all())
2194
records= head->file->stats.records;
2196
records++; /* purecov: inspected */
2197
scan_time= (double) records / TIME_FOR_COMPARE + 1;
2198
read_time= (double) head->file->scan_time() + scan_time + 1.1;
2199
if (head->force_index)
2200
scan_time= read_time= DBL_MAX;
2201
if (limit < records)
2202
read_time= (double) records + scan_time + 1; // Force to use index
2203
else if (read_time <= 2.0 && !force_quick_range)
2204
DBUG_RETURN(0); /* No need for quick select */
2206
DBUG_PRINT("info",("Time to scan table: %g", read_time));
2208
keys_to_use.intersect(head->keys_in_use_for_query);
2209
if (!keys_to_use.is_clear_all())
2212
SEL_TREE *tree= NULL;
2213
KEY_PART *key_parts;
2217
if (check_stack_overrun(thd, 2*STACK_MIN_SIZE, NULL))
2218
DBUG_RETURN(0); // Fatal error flag is set
2220
/* set up parameter that is passed to all functions */
2222
param.baseflag= head->file->ha_table_flags();
2223
param.prev_tables=prev_tables | const_tables;
2224
param.read_tables=read_tables;
2225
param.current_table= head->map;
2228
param.mem_root= &alloc;
2229
param.old_root= thd->mem_root;
2230
param.needed_reg= &needed_reg;
2231
param.imerge_cost_buff_size= 0;
2232
param.using_real_indexes= TRUE;
2233
param.remove_jump_scans= TRUE;
2234
param.force_default_mrr= ordered_output;
2236
thd->no_errors=1; // Don't warn about NULL
2237
init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
2238
if (!(param.key_parts= (KEY_PART*) alloc_root(&alloc,
2240
head->s->key_parts)) ||
2241
fill_used_fields_bitmap(¶m))
2244
free_root(&alloc,MYF(0)); // Return memory & allocator
2245
DBUG_RETURN(0); // Can't use range
2247
key_parts= param.key_parts;
2248
thd->mem_root= &alloc;
2251
Make an array with description of all key parts of all table keys.
2252
This is used in get_mm_parts function.
2254
key_info= head->key_info;
2255
for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
2257
KEY_PART_INFO *key_part_info;
2258
if (!keys_to_use.is_set(idx))
2261
param.key[param.keys]=key_parts;
2262
key_part_info= key_info->key_part;
2263
for (uint part=0 ; part < key_info->key_parts ;
2264
part++, key_parts++, key_part_info++)
2266
key_parts->key= param.keys;
2267
key_parts->part= part;
2268
key_parts->length= key_part_info->length;
2269
key_parts->store_length= key_part_info->store_length;
2270
key_parts->field= key_part_info->field;
2271
key_parts->null_bit= key_part_info->null_bit;
2272
key_parts->image_type = Field::itRAW;
2273
/* Only HA_PART_KEY_SEG is used */
2274
key_parts->flag= (uint8) key_part_info->key_part_flag;
2276
param.real_keynr[param.keys++]=idx;
2278
param.key_parts_end=key_parts;
2279
param.alloced_sel_args= 0;
2281
/* Calculate cost of full index read for the shortest covering index */
2282
if (!head->covering_keys.is_clear_all())
2284
int key_for_use= find_shortest_key(head, &head->covering_keys);
2285
double key_read_time=
2286
param.table->file->index_only_read_time(key_for_use,
2287
rows2double(records)) +
2288
(double) records / TIME_FOR_COMPARE;
2289
DBUG_PRINT("info", ("'all'+'using index' scan will be using key %d, "
2290
"read time %g", key_for_use, key_read_time));
2291
if (key_read_time < read_time)
2292
read_time= key_read_time;
2295
TABLE_READ_PLAN *best_trp= NULL;
2296
TRP_GROUP_MIN_MAX *group_trp;
2297
double best_read_time= read_time;
2301
if ((tree= get_mm_tree(¶m,cond)))
2303
if (tree->type == SEL_TREE::IMPOSSIBLE)
2305
records=0L; /* Return -1 from this function. */
2306
read_time= (double) HA_POS_ERROR;
2310
If the tree can't be used for range scans, proceed anyway, as we
2311
can construct a group-min-max quick select
2313
if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
2319
Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
2320
Notice that it can be constructed no matter if there is a range tree.
2322
group_trp= get_best_group_min_max(¶m, tree);
2325
param.table->quick_condition_rows= min(group_trp->records,
2326
head->file->stats.records);
2327
if (group_trp->read_cost < best_read_time)
2329
best_trp= group_trp;
2330
best_read_time= best_trp->read_cost;
2337
It is possible to use a range-based quick select (but it might be
2338
slower than 'all' table scan).
2340
if (tree->merges.is_empty())
2342
TRP_RANGE *range_trp;
2343
TRP_ROR_INTERSECT *rori_trp;
2344
bool can_build_covering= FALSE;
2346
/* Get best 'range' plan and prepare data for making other plans */
2347
if ((range_trp= get_key_scans_params(¶m, tree, FALSE, TRUE,
2350
best_trp= range_trp;
2351
best_read_time= best_trp->read_cost;
2355
Simultaneous key scans and row deletes on several handler
2356
objects are not allowed so don't use ROR-intersection for
2359
if ((thd->lex->sql_command != SQLCOM_DELETE))
2362
Get best non-covering ROR-intersection plan and prepare data for
2363
building covering ROR-intersection.
2365
if ((rori_trp= get_best_ror_intersect(¶m, tree, best_read_time,
2366
&can_build_covering)))
2369
best_read_time= best_trp->read_cost;
2371
Try constructing covering ROR-intersect only if it looks possible
2374
if (!rori_trp->is_covering && can_build_covering &&
2375
(rori_trp= get_best_covering_ror_intersect(¶m, tree,
2383
/* Try creating index_merge/ROR-union scan. */
2385
TABLE_READ_PLAN *best_conj_trp= NULL, *new_conj_trp;
2386
DBUG_PRINT("info",("No range reads possible,"
2387
" trying to construct index_merge"));
2388
List_iterator_fast<SEL_IMERGE> it(tree->merges);
2389
while ((imerge= it++))
2391
new_conj_trp= get_best_disjunct_quick(¶m, imerge, best_read_time);
2393
set_if_smaller(param.table->quick_condition_rows,
2394
new_conj_trp->records);
2395
if (!best_conj_trp || (new_conj_trp && new_conj_trp->read_cost <
2396
best_conj_trp->read_cost))
2397
best_conj_trp= new_conj_trp;
2400
best_trp= best_conj_trp;
2404
thd->mem_root= param.old_root;
2406
/* If we got a read plan, create a quick select from it. */
2409
records= best_trp->records;
2410
if (!(quick= best_trp->make_quick(¶m, TRUE)) || quick->init())
2418
free_root(&alloc,MYF(0)); // Return memory & allocator
2419
thd->mem_root= param.old_root;
2423
DBUG_EXECUTE("info", print_quick(quick, &needed_reg););
2426
Assume that if the user is using 'limit' we will only need to scan
2427
limit rows if we are using a key
2429
DBUG_RETURN(records ? test(quick) : -1);
2433
Get best plan for a SEL_IMERGE disjunctive expression.
2435
get_best_disjunct_quick()
2436
param Parameter from check_quick_select function
2437
imerge Expression to use
2438
read_time Don't create scans with cost > read_time
2441
index_merge cost is calculated as follows:
2443
cost(index_reads) + (see #1)
2444
cost(rowid_to_row_scan) + (see #2)
2445
cost(unique_use) (see #3)
2447
1. cost(index_reads) =SUM_i(cost(index_read_i))
2449
cost(index_read_i) = {cost of ordinary 'index only' scan}
2451
cost(index_read_i) = {cost of non-'index only' scan}
2453
2. cost(rowid_to_row_scan)
2454
If table PK is clustered then
2455
cost(rowid_to_row_scan) =
2456
{cost of ordinary clustered PK scan with n_ranges=n_rows}
2458
Otherwise, we use the following model to calculate costs:
2459
We need to retrieve n_rows rows from file that occupies n_blocks blocks.
2460
We assume that offsets of rows we need are independent variates with
2461
uniform distribution in [0..max_file_offset] range.
2463
We'll denote block as "busy" if it contains row(s) we need to retrieve
2464
and "empty" if doesn't contain rows we need.
2466
Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
2467
applies to any block in file). Let x_i be a variate taking value 1 if
2468
block #i is empty and 0 otherwise.
2470
Then E(x_i) = (1 - 1/n_blocks)^n_rows;
2472
E(n_empty_blocks) = E(sum(x_i)) = sum(E(x_i)) =
2473
= n_blocks * ((1 - 1/n_blocks)^n_rows) =
2474
~= n_blocks * exp(-n_rows/n_blocks).
2476
E(n_busy_blocks) = n_blocks*(1 - (1 - 1/n_blocks)^n_rows) =
2477
~= n_blocks * (1 - exp(-n_rows/n_blocks)).
2479
Average size of "hole" between neighbor non-empty blocks is
2480
E(hole_size) = n_blocks/E(n_busy_blocks).
2482
The total cost of reading all needed blocks in one "sweep" is:
2485
(DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*n_blocks/E(n_busy_blocks)).
2487
3. Cost of Unique use is calculated in Unique::get_use_cost function.
2489
ROR-union cost is calculated in the same way index_merge, but instead of
2490
Unique a priority queue is used.
2494
NULL - Out of memory or no read scan could be built.
2498
TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
2502
TRP_INDEX_MERGE *imerge_trp= NULL;
2503
uint n_child_scans= imerge->trees_next - imerge->trees;
2504
TRP_RANGE **range_scans;
2505
TRP_RANGE **cur_child;
2506
TRP_RANGE **cpk_scan= NULL;
2507
bool imerge_too_expensive= FALSE;
2508
double imerge_cost= 0.0;
2509
ha_rows cpk_scan_records= 0;
2510
ha_rows non_cpk_scan_records= 0;
2511
bool pk_is_clustered= param->table->file->primary_key_is_clustered();
2512
bool all_scans_ror_able= TRUE;
2513
bool all_scans_rors= TRUE;
2514
uint unique_calc_buff_size;
2515
TABLE_READ_PLAN **roru_read_plans;
2516
TABLE_READ_PLAN **cur_roru_plan;
2517
double roru_index_costs;
2518
ha_rows roru_total_records;
2519
double roru_intersect_part= 1.0;
2520
DBUG_ENTER("get_best_disjunct_quick");
2521
DBUG_PRINT("info", ("Full table scan cost: %g", read_time));
2523
if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
2528
Collect best 'range' scan for each of disjuncts, and, while doing so,
2529
analyze possibility of ROR scans. Also calculate some values needed by
2530
other parts of the code.
2532
for (ptree= imerge->trees, cur_child= range_scans;
2533
ptree != imerge->trees_next;
2534
ptree++, cur_child++)
2536
DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
2537
"tree in SEL_IMERGE"););
2538
if (!(*cur_child= get_key_scans_params(param, *ptree, TRUE, FALSE, read_time)))
2541
One of index scans in this index_merge is more expensive than entire
2542
table read for another available option. The entire index_merge (and
2543
any possible ROR-union) will be more expensive then, too. We continue
2544
here only to update SQL_SELECT members.
2546
imerge_too_expensive= TRUE;
2548
if (imerge_too_expensive)
2551
imerge_cost += (*cur_child)->read_cost;
2552
all_scans_ror_able &= ((*ptree)->n_ror_scans > 0);
2553
all_scans_rors &= (*cur_child)->is_ror;
2554
if (pk_is_clustered &&
2555
param->real_keynr[(*cur_child)->key_idx] ==
2556
param->table->s->primary_key)
2558
cpk_scan= cur_child;
2559
cpk_scan_records= (*cur_child)->records;
2562
non_cpk_scan_records += (*cur_child)->records;
2565
DBUG_PRINT("info", ("index_merge scans cost %g", imerge_cost));
2566
if (imerge_too_expensive || (imerge_cost > read_time) ||
2567
((non_cpk_scan_records+cpk_scan_records >= param->table->file->stats.records) && read_time != DBL_MAX))
2570
Bail out if it is obvious that both index_merge and ROR-union will be
2573
DBUG_PRINT("info", ("Sum of index_merge scans is more expensive than "
2574
"full table scan, bailing out"));
2579
roru_read_plans= (TABLE_READ_PLAN**)range_scans;
2580
goto skip_to_ror_scan;
2585
Add one ROWID comparison for each row retrieved on non-CPK scan. (it
2586
is done in QUICK_RANGE_SELECT::row_in_ranges)
2588
imerge_cost += non_cpk_scan_records / TIME_FOR_COMPARE_ROWID;
2591
/* Calculate cost(rowid_to_row_scan) */
2593
COST_VECT sweep_cost;
2594
JOIN *join= param->thd->lex->select_lex.join;
2595
bool is_interrupted= test(join && join->tables == 1);
2596
get_sweep_read_cost(param->table, non_cpk_scan_records, is_interrupted,
2598
imerge_cost += sweep_cost.total_cost();
2600
DBUG_PRINT("info",("index_merge cost with rowid-to-row scan: %g",
2602
if (imerge_cost > read_time)
2603
goto build_ror_index_merge;
2605
/* Add Unique operations cost */
2606
unique_calc_buff_size=
2607
Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
2608
param->table->file->ref_length,
2609
param->thd->variables.sortbuff_size);
2610
if (param->imerge_cost_buff_size < unique_calc_buff_size)
2612
if (!(param->imerge_cost_buff= (uint*)alloc_root(param->mem_root,
2613
unique_calc_buff_size)))
2615
param->imerge_cost_buff_size= unique_calc_buff_size;
2619
Unique::get_use_cost(param->imerge_cost_buff, (uint)non_cpk_scan_records,
2620
param->table->file->ref_length,
2621
param->thd->variables.sortbuff_size);
2622
DBUG_PRINT("info",("index_merge total cost: %g (wanted: less then %g)",
2623
imerge_cost, read_time));
2624
if (imerge_cost < read_time)
2626
if ((imerge_trp= new (param->mem_root)TRP_INDEX_MERGE))
2628
imerge_trp->read_cost= imerge_cost;
2629
imerge_trp->records= non_cpk_scan_records + cpk_scan_records;
2630
imerge_trp->records= min(imerge_trp->records,
2631
param->table->file->stats.records);
2632
imerge_trp->range_scans= range_scans;
2633
imerge_trp->range_scans_end= range_scans + n_child_scans;
2634
read_time= imerge_cost;
2638
build_ror_index_merge:
2639
if (!all_scans_ror_able || param->thd->lex->sql_command == SQLCOM_DELETE)
2640
DBUG_RETURN(imerge_trp);
2642
/* Ok, it is possible to build a ROR-union, try it. */
2644
if (!(roru_read_plans=
2645
(TABLE_READ_PLAN**)alloc_root(param->mem_root,
2646
sizeof(TABLE_READ_PLAN*)*
2648
DBUG_RETURN(imerge_trp);
2650
roru_index_costs= 0.0;
2651
roru_total_records= 0;
2652
cur_roru_plan= roru_read_plans;
2654
/* Find 'best' ROR scan for each of trees in disjunction */
2655
for (ptree= imerge->trees, cur_child= range_scans;
2656
ptree != imerge->trees_next;
2657
ptree++, cur_child++, cur_roru_plan++)
2660
Assume the best ROR scan is the one that has cheapest full-row-retrieval
2662
Also accumulate index_only scan costs as we'll need them to calculate
2663
overall index_intersection cost.
2666
if ((*cur_child)->is_ror)
2668
/* Ok, we have index_only cost, now get full rows scan cost */
2669
cost= param->table->file->
2670
read_time(param->real_keynr[(*cur_child)->key_idx], 1,
2671
(*cur_child)->records) +
2672
rows2double((*cur_child)->records) / TIME_FOR_COMPARE;
2677
TABLE_READ_PLAN *prev_plan= *cur_child;
2678
if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost,
2681
if (prev_plan->is_ror)
2682
*cur_roru_plan= prev_plan;
2684
DBUG_RETURN(imerge_trp);
2685
roru_index_costs += (*cur_roru_plan)->read_cost;
2689
((TRP_ROR_INTERSECT*)(*cur_roru_plan))->index_scan_costs;
2690
roru_total_records += (*cur_roru_plan)->records;
2691
roru_intersect_part *= (*cur_roru_plan)->records /
2692
param->table->file->stats.records;
2697
SUM(rows_in_scan_i) - table_rows * PROD(rows_in_scan_i / table_rows).
2698
This is valid because index_merge construction guarantees that conditions
2699
in disjunction do not share key parts.
2701
roru_total_records -= (ha_rows)(roru_intersect_part*
2702
param->table->file->stats.records);
2703
/* ok, got a ROR read plan for each of the disjuncts
2705
cost(index_union_scan(scan_1, ... scan_n)) =
2706
SUM_i(cost_of_index_only_scan(scan_i)) +
2707
queue_use_cost(rowid_len, n) +
2708
cost_of_row_retrieval
2709
See get_merge_buffers_cost function for queue_use_cost formula derivation.
2711
double roru_total_cost;
2713
COST_VECT sweep_cost;
2714
JOIN *join= param->thd->lex->select_lex.join;
2715
bool is_interrupted= test(join && join->tables == 1);
2716
get_sweep_read_cost(param->table, roru_total_records, is_interrupted,
2718
roru_total_cost= roru_index_costs +
2719
rows2double(roru_total_records)*log((double)n_child_scans) /
2720
(TIME_FOR_COMPARE_ROWID * M_LN2) +
2721
sweep_cost.total_cost();
2724
DBUG_PRINT("info", ("ROR-union: cost %g, %d members", roru_total_cost,
2726
TRP_ROR_UNION* roru;
2727
if (roru_total_cost < read_time)
2729
if ((roru= new (param->mem_root) TRP_ROR_UNION))
2731
roru->first_ror= roru_read_plans;
2732
roru->last_ror= roru_read_plans + n_child_scans;
2733
roru->read_cost= roru_total_cost;
2734
roru->records= roru_total_records;
2738
DBUG_RETURN(imerge_trp);
2742
typedef struct st_ror_scan_info
2744
uint idx; /* # of used key in param->keys */
2745
uint keynr; /* # of used key in table */
2746
ha_rows records; /* estimate of # records this scan will return */
2748
/* Set of intervals over key fields that will be used for row retrieval. */
2751
/* Fields used in the query and covered by this ROR scan. */
2752
MY_BITMAP covered_fields;
2753
uint used_fields_covered; /* # of set bits in covered_fields */
2754
int key_rec_length; /* length of key record (including rowid) */
2757
Cost of reading all index records with values in sel_arg intervals set
2758
(assuming there is no need to access full table records)
2760
double index_read_cost;
2761
uint first_uncovered_field; /* first unused bit in covered_fields */
2762
uint key_components; /* # of parts in the key */
2767
Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
2768
sel_arg set of intervals.
2772
param Parameter from test_quick_select function
2773
idx Index of key in param->keys
2774
sel_arg Set of intervals for a given key
2777
NULL - out of memory
2778
ROR scan structure containing a scan for {idx, sel_arg}
2782
ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
2784
ROR_SCAN_INFO *ror_scan;
2785
my_bitmap_map *bitmap_buf;
2787
DBUG_ENTER("make_ror_scan");
2789
if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
2790
sizeof(ROR_SCAN_INFO))))
2794
ror_scan->keynr= keynr= param->real_keynr[idx];
2795
ror_scan->key_rec_length= (param->table->key_info[keynr].key_length +
2796
param->table->file->ref_length);
2797
ror_scan->sel_arg= sel_arg;
2798
ror_scan->records= param->table->quick_rows[keynr];
2800
if (!(bitmap_buf= (my_bitmap_map*) alloc_root(param->mem_root,
2801
param->fields_bitmap_size)))
2804
if (bitmap_init(&ror_scan->covered_fields, bitmap_buf,
2805
param->table->s->fields, FALSE))
2807
bitmap_clear_all(&ror_scan->covered_fields);
2809
KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
2810
KEY_PART_INFO *key_part_end= key_part +
2811
param->table->key_info[keynr].key_parts;
2812
for (;key_part != key_part_end; ++key_part)
2814
if (bitmap_is_set(¶m->needed_fields, key_part->fieldnr-1))
2815
bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
2817
double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
2818
ror_scan->index_read_cost=
2819
param->table->file->index_only_read_time(ror_scan->keynr, rows);
2820
DBUG_RETURN(ror_scan);
2825
Compare two ROR_SCAN_INFO** by E(#records_matched) * key_record_length.
2828
a ptr to first compared value
2829
b ptr to second compared value
2837
static int cmp_ror_scan_info(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
2839
double val1= rows2double((*a)->records) * (*a)->key_rec_length;
2840
double val2= rows2double((*b)->records) * (*b)->key_rec_length;
2841
return (val1 < val2)? -1: (val1 == val2)? 0 : 1;
2845
Compare two ROR_SCAN_INFO** by
2846
(#covered fields in F desc,
2848
number of first not covered component asc)
2851
cmp_ror_scan_info_covering()
2852
a ptr to first compared value
2853
b ptr to second compared value
2861
static int cmp_ror_scan_info_covering(ROR_SCAN_INFO** a, ROR_SCAN_INFO** b)
2863
if ((*a)->used_fields_covered > (*b)->used_fields_covered)
2865
if ((*a)->used_fields_covered < (*b)->used_fields_covered)
2867
if ((*a)->key_components < (*b)->key_components)
2869
if ((*a)->key_components > (*b)->key_components)
2871
if ((*a)->first_uncovered_field < (*b)->first_uncovered_field)
2873
if ((*a)->first_uncovered_field > (*b)->first_uncovered_field)
2879
/* Auxiliary structure for incremental ROR-intersection creation */
2883
MY_BITMAP covered_fields; /* union of fields covered by all scans */
2885
Fraction of table records that satisfies conditions of all scans.
2886
This is the number of full records that will be retrieved if a
2887
non-index_only index intersection will be employed.
2890
/* TRUE if covered_fields is a superset of needed_fields */
2893
ha_rows index_records; /* sum(#records to look in indexes) */
2894
double index_scan_costs; /* SUM(cost of 'index-only' scans) */
2896
} ROR_INTERSECT_INFO;
2900
Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.
2903
ror_intersect_init()
2904
param Parameter from test_quick_select
2912
ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
2914
ROR_INTERSECT_INFO *info;
2916
if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
2917
sizeof(ROR_INTERSECT_INFO))))
2920
if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
2921
param->fields_bitmap_size)))
2923
if (bitmap_init(&info->covered_fields, buf, param->table->s->fields,
2926
info->is_covering= FALSE;
2927
info->index_scan_costs= 0.0;
2928
info->index_records= 0;
2929
info->out_rows= (double) param->table->file->stats.records;
2930
bitmap_clear_all(&info->covered_fields);
2934
void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
2936
dst->param= src->param;
2937
memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap,
2938
no_bytes_in_map(&src->covered_fields));
2939
dst->out_rows= src->out_rows;
2940
dst->is_covering= src->is_covering;
2941
dst->index_records= src->index_records;
2942
dst->index_scan_costs= src->index_scan_costs;
2943
dst->total_cost= src->total_cost;
2948
Get selectivity of a ROR scan wrt ROR-intersection.
2951
ror_scan_selectivity()
2952
info ROR-interection
2956
Suppose we have a condition on several keys
2957
cond=k_11=c_11 AND k_12=c_12 AND ... // parts of first key
2958
k_21=c_21 AND k_22=c_22 AND ... // parts of second key
2960
k_n1=c_n1 AND k_n3=c_n3 AND ... (1) //parts of the key used by *scan
2962
where k_ij may be the same as any k_pq (i.e. keys may have common parts).
2964
A full row is retrieved if entire condition holds.
2966
The recursive procedure for finding P(cond) is as follows:
2969
Pick 1st part of 1st key and break conjunction (1) into two parts:
2970
cond= (k_11=c_11 AND R)
2972
Here R may still contain condition(s) equivalent to k_11=c_11.
2973
Nevertheless, the following holds:
2975
P(k_11=c_11 AND R) = P(k_11=c_11) * P(R | k_11=c_11).
2977
Mark k_11 as fixed field (and satisfied condition) F, save P(F),
2978
save R to be cond and proceed to recursion step.
2981
We have a set of fixed fields/satisfied conditions) F, probability P(F),
2982
and remaining conjunction R
2983
Pick next key part on current key and its condition "k_ij=c_ij".
2984
We will add "k_ij=c_ij" into F and update P(F).
2985
Lets denote k_ij as t, R = t AND R1, where R1 may still contain t. Then
2987
P((t AND R1)|F) = P(t|F) * P(R1|t|F) = P(t|F) * P(R1|(t AND F)) (2)
2989
(where '|' mean conditional probability, not "or")
2991
Consider the first multiplier in (2). One of the following holds:
2992
a) F contains condition on field used in t (i.e. t AND F = F).
2995
b) F doesn't contain condition on field used in t. Then F and t are
2996
considered independent.
2998
P(t|F) = P(t|(fields_before_t_in_key AND other_fields)) =
2999
= P(t|fields_before_t_in_key).
3001
P(t|fields_before_t_in_key) = #records(fields_before_t_in_key) /
3002
#records(fields_before_t_in_key, t)
3004
The second multiplier is calculated by applying this step recursively.
3007
This function calculates the result of application of the "recursion step"
3008
described above for all fixed key members of a single key, accumulating set
3009
of covered fields, selectivity, etc.
3011
The calculation is conducted as follows:
3012
Lets denote #records(keypart1, ... keypartK) as n_k. We need to calculate
3015
--------- * --------- * .... (3)
3018
where k1,k2,... are key parts which fields were not yet marked as fixed
3019
( this is result of application of option b) of the recursion step for
3020
parts of a single key).
3021
Since it is reasonable to expect that most of the fields are not marked
3022
as fixed, we calculate (3) as
3025
(3) = n_{max_key_part} / ( --------- * --------- * .... )
3028
where i1,i2, .. are key parts that were already marked as fixed.
3030
In order to minimize number of expensive records_in_range calls we group
3031
and reduce adjacent fractions.
3034
Selectivity of given ROR scan.
3037
static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
3038
const ROR_SCAN_INFO *scan)
3040
double selectivity_mult= 1.0;
3041
KEY_PART_INFO *key_part= info->param->table->key_info[scan->keynr].key_part;
3042
uchar key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH]; /* key values tuple */
3043
uchar *key_ptr= key_val;
3044
SEL_ARG *sel_arg, *tuple_arg= NULL;
3045
key_part_map keypart_map= 0;
3047
bool prev_covered= test(bitmap_is_set(&info->covered_fields,
3048
key_part->fieldnr-1));
3049
key_range min_range;
3050
key_range max_range;
3051
min_range.key= key_val;
3052
min_range.flag= HA_READ_KEY_EXACT;
3053
max_range.key= key_val;
3054
max_range.flag= HA_READ_AFTER_KEY;
3055
ha_rows prev_records= info->param->table->file->stats.records;
3056
DBUG_ENTER("ror_scan_selectivity");
3058
for (sel_arg= scan->sel_arg; sel_arg;
3059
sel_arg= sel_arg->next_key_part)
3061
DBUG_PRINT("info",("sel_arg step"));
3062
cur_covered= test(bitmap_is_set(&info->covered_fields,
3063
key_part[sel_arg->part].fieldnr-1));
3064
if (cur_covered != prev_covered)
3066
/* create (part1val, ..., part{n-1}val) tuple. */
3070
tuple_arg= scan->sel_arg;
3071
/* Here we use the length of the first key part */
3072
tuple_arg->store_min(key_part->store_length, &key_ptr, 0);
3075
while (tuple_arg->next_key_part != sel_arg)
3077
tuple_arg= tuple_arg->next_key_part;
3078
tuple_arg->store_min(key_part[tuple_arg->part].store_length,
3080
keypart_map= (keypart_map << 1) | 1;
3082
min_range.length= max_range.length= (size_t) (key_ptr - key_val);
3083
min_range.keypart_map= max_range.keypart_map= keypart_map;
3084
records= (info->param->table->file->
3085
records_in_range(scan->keynr, &min_range, &max_range));
3088
/* uncovered -> covered */
3089
double tmp= rows2double(records)/rows2double(prev_records);
3090
DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
3091
selectivity_mult *= tmp;
3092
prev_records= HA_POS_ERROR;
3096
/* covered -> uncovered */
3097
prev_records= records;
3100
prev_covered= cur_covered;
3104
double tmp= rows2double(info->param->table->quick_rows[scan->keynr]) /
3105
rows2double(prev_records);
3106
DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
3107
selectivity_mult *= tmp;
3109
DBUG_PRINT("info", ("Returning multiplier: %g", selectivity_mult));
3110
DBUG_RETURN(selectivity_mult);
3115
Check if adding a ROR scan to a ROR-intersection reduces its cost of
3116
ROR-intersection and if yes, update parameters of ROR-intersection,
3121
param Parameter from test_quick_select
3122
info ROR-intersection structure to add the scan to.
3123
ror_scan ROR scan info to add.
3124
is_cpk_scan If TRUE, add the scan as CPK scan (this can be inferred
3125
from other parameters and is passed separately only to
3126
avoid duplicating the inference code)
3129
Adding a ROR scan to ROR-intersect "makes sense" iff the cost of ROR-
3130
intersection decreases. The cost of ROR-intersection is calculated as
3133
cost= SUM_i(key_scan_cost_i) + cost_of_full_rows_retrieval
3135
When we add a scan the first increases and the second decreases.
3137
cost_of_full_rows_retrieval=
3138
(union of indexes used covers all needed fields) ?
3139
cost_of_sweep_read(E(rows_to_retrieve), rows_in_table) :
3142
E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
3143
ror_scan_selectivity({scan1}, scan2) * ... *
3144
ror_scan_selectivity({scan1,...}, scanN).
3146
TRUE ROR scan added to ROR-intersection, cost updated.
3147
FALSE It doesn't make sense to add this ROR scan to this ROR-intersection.
3150
static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
3151
ROR_SCAN_INFO* ror_scan, bool is_cpk_scan)
3153
double selectivity_mult= 1.0;
3155
DBUG_ENTER("ror_intersect_add");
3156
DBUG_PRINT("info", ("Current out_rows= %g", info->out_rows));
3157
DBUG_PRINT("info", ("Adding scan on %s",
3158
info->param->table->key_info[ror_scan->keynr].name));
3159
DBUG_PRINT("info", ("is_cpk_scan: %d",is_cpk_scan));
3161
selectivity_mult = ror_scan_selectivity(info, ror_scan);
3162
if (selectivity_mult == 1.0)
3164
/* Don't add this scan if it doesn't improve selectivity. */
3165
DBUG_PRINT("info", ("The scan doesn't improve selectivity."));
3169
info->out_rows *= selectivity_mult;
3174
CPK scan is used to filter out rows. We apply filtering for
3175
each record of every scan. Assuming 1/TIME_FOR_COMPARE_ROWID
3176
per check this gives us:
3178
info->index_scan_costs += rows2double(info->index_records) /
3179
TIME_FOR_COMPARE_ROWID;
3183
info->index_records += info->param->table->quick_rows[ror_scan->keynr];
3184
info->index_scan_costs += ror_scan->index_read_cost;
3185
bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
3186
if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
3187
&info->covered_fields))
3189
DBUG_PRINT("info", ("ROR-intersect is covering now"));
3190
info->is_covering= TRUE;
3194
info->total_cost= info->index_scan_costs;
3195
DBUG_PRINT("info", ("info->total_cost: %g", info->total_cost));
3196
if (!info->is_covering)
3198
COST_VECT sweep_cost;
3199
JOIN *join= info->param->thd->lex->select_lex.join;
3200
bool is_interrupted= test(join && join->tables == 1);
3201
get_sweep_read_cost(info->param->table, double2rows(info->out_rows),
3202
is_interrupted, &sweep_cost);
3203
info->total_cost += sweep_cost.total_cost();
3204
DBUG_PRINT("info", ("info->total_cost= %g", info->total_cost));
3206
DBUG_PRINT("info", ("New out_rows: %g", info->out_rows));
3207
DBUG_PRINT("info", ("New cost: %g, %scovering", info->total_cost,
3208
info->is_covering?"" : "non-"));
3214
Get best ROR-intersection plan using non-covering ROR-intersection search
3215
algorithm. The returned plan may be covering.
3218
get_best_ror_intersect()
3219
param Parameter from test_quick_select function.
3220
tree Transformed restriction condition to be used to look
3222
read_time Do not return read plans with cost > read_time.
3223
are_all_covering [out] set to TRUE if union of all scans covers all
3224
fields needed by the query (and it is possible to build
3225
a covering ROR-intersection)
3228
get_key_scans_params must be called before this function can be called.
3230
When this function is called by ROR-union construction algorithm it
3231
assumes it is building an uncovered ROR-intersection (and thus # of full
3232
records to be retrieved is wrong here). This is a hack.
3235
The approximate best non-covering plan search algorithm is as follows:
3237
find_min_ror_intersection_scan()
3239
R= select all ROR scans;
3240
order R by (E(#records_matched) * key_record_length).
3242
S= first(R); -- set of scans that will be used for ROR-intersection
3245
min_scan= make_scan(S);
3246
while (R is not empty)
3248
firstR= R - first(R);
3249
if (!selectivity(S + firstR < selectivity(S)))
3253
if (cost(S) < min_cost)
3256
min_scan= make_scan(S);
3262
See ror_intersect_add function for ROR intersection costs.
3264
Special handling for Clustered PK scans
3265
Clustered PK contains all table fields, so using it as a regular scan in
3266
index intersection doesn't make sense: a range scan on CPK will be less
3267
expensive in this case.
3268
Clustered PK scan has special handling in ROR-intersection: it is not used
3269
to retrieve rows, instead its condition is used to filter row references
3270
we get from scans on other keys.
3273
ROR-intersection table read plan
3274
NULL if out of memory or no suitable plan found.
3278
TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
3280
bool *are_all_covering)
3283
double min_cost= DBL_MAX;
3284
DBUG_ENTER("get_best_ror_intersect");
3286
if ((tree->n_ror_scans < 2) || !param->table->file->stats.records)
3290
Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of
3291
them. Also find and save clustered PK scan if there is one.
3293
ROR_SCAN_INFO **cur_ror_scan;
3294
ROR_SCAN_INFO *cpk_scan= NULL;
3296
bool cpk_scan_used= FALSE;
3298
if (!(tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
3299
sizeof(ROR_SCAN_INFO*)*
3302
cpk_no= ((param->table->file->primary_key_is_clustered()) ?
3303
param->table->s->primary_key : MAX_KEY);
3305
for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
3307
ROR_SCAN_INFO *scan;
3308
if (!tree->ror_scans_map.is_set(idx))
3310
if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
3312
if (param->real_keynr[idx] == cpk_no)
3315
tree->n_ror_scans--;
3318
*(cur_ror_scan++)= scan;
3321
tree->ror_scans_end= cur_ror_scan;
3322
DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "original",
3324
tree->ror_scans_end););
3326
Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
3328
Step 2: Get best ROR-intersection using an approximate algorithm.
3330
my_qsort(tree->ror_scans, tree->n_ror_scans, sizeof(ROR_SCAN_INFO*),
3331
(qsort_cmp)cmp_ror_scan_info);
3332
DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "ordered",
3334
tree->ror_scans_end););
3336
ROR_SCAN_INFO **intersect_scans; /* ROR scans used in index intersection */
3337
ROR_SCAN_INFO **intersect_scans_end;
3338
if (!(intersect_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
3339
sizeof(ROR_SCAN_INFO*)*
3340
tree->n_ror_scans)))
3342
intersect_scans_end= intersect_scans;
3344
/* Create and incrementally update ROR intersection. */
3345
ROR_INTERSECT_INFO *intersect, *intersect_best;
3346
if (!(intersect= ror_intersect_init(param)) ||
3347
!(intersect_best= ror_intersect_init(param)))
3350
/* [intersect_scans,intersect_scans_best) will hold the best intersection */
3351
ROR_SCAN_INFO **intersect_scans_best;
3352
cur_ror_scan= tree->ror_scans;
3353
intersect_scans_best= intersect_scans;
3354
while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
3356
/* S= S + first(R); R= R - first(R); */
3357
if (!ror_intersect_add(intersect, *cur_ror_scan, FALSE))
3363
*(intersect_scans_end++)= *(cur_ror_scan++);
3365
if (intersect->total_cost < min_cost)
3367
/* Local minimum found, save it */
3368
ror_intersect_cpy(intersect_best, intersect);
3369
intersect_scans_best= intersect_scans_end;
3370
min_cost = intersect->total_cost;
3374
if (intersect_scans_best == intersect_scans)
3376
DBUG_PRINT("info", ("None of scans increase selectivity"));
3380
DBUG_EXECUTE("info",print_ror_scans_arr(param->table,
3381
"best ROR-intersection",
3383
intersect_scans_best););
3385
*are_all_covering= intersect->is_covering;
3386
uint best_num= intersect_scans_best - intersect_scans;
3387
ror_intersect_cpy(intersect, intersect_best);
3390
Ok, found the best ROR-intersection of non-CPK key scans.
3391
Check if we should add a CPK scan. If the obtained ROR-intersection is
3392
covering, it doesn't make sense to add CPK scan.
3394
if (cpk_scan && !intersect->is_covering)
3396
if (ror_intersect_add(intersect, cpk_scan, TRUE) &&
3397
(intersect->total_cost < min_cost))
3399
cpk_scan_used= TRUE;
3400
intersect_best= intersect; //just set pointer here
3404
/* Ok, return ROR-intersect plan if we have found one */
3405
TRP_ROR_INTERSECT *trp= NULL;
3406
if (min_cost < read_time && (cpk_scan_used || best_num > 1))
3408
if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
3410
if (!(trp->first_scan=
3411
(ROR_SCAN_INFO**)alloc_root(param->mem_root,
3412
sizeof(ROR_SCAN_INFO*)*best_num)))
3414
memcpy(trp->first_scan, intersect_scans, best_num*sizeof(ROR_SCAN_INFO*));
3415
trp->last_scan= trp->first_scan + best_num;
3416
trp->is_covering= intersect_best->is_covering;
3417
trp->read_cost= intersect_best->total_cost;
3418
/* Prevent divisons by zero */
3419
ha_rows best_rows = double2rows(intersect_best->out_rows);
3422
set_if_smaller(param->table->quick_condition_rows, best_rows);
3423
trp->records= best_rows;
3424
trp->index_scan_costs= intersect_best->index_scan_costs;
3425
trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
3426
DBUG_PRINT("info", ("Returning non-covering ROR-intersect plan:"
3427
"cost %g, records %lu",
3428
trp->read_cost, (ulong) trp->records));
3435
Get best covering ROR-intersection.
3437
get_best_covering_ror_intersect()
3438
param Parameter from test_quick_select function.
3439
tree SEL_TREE with sets of intervals for different keys.
3440
read_time Don't return table read plans with cost > read_time.
3443
Best covering ROR-intersection plan
3444
NULL if no plan found.
3447
get_best_ror_intersect must be called for a tree before calling this
3449
This function invalidates tree->ror_scans member values.
3451
The following approximate algorithm is used:
3452
I=set of all covering indexes
3453
F=set of all fields to cover
3458
Order I by (#covered fields in F desc,
3460
number of first not covered component asc);
3461
F=F-covered by first(I);
3464
} while F is not empty.
3468
TRP_ROR_INTERSECT *get_best_covering_ror_intersect(PARAM *param,
3472
ROR_SCAN_INFO **ror_scan_mark;
3473
ROR_SCAN_INFO **ror_scans_end= tree->ror_scans_end;
3474
DBUG_ENTER("get_best_covering_ror_intersect");
3476
for (ROR_SCAN_INFO **scan= tree->ror_scans; scan != ror_scans_end; ++scan)
3477
(*scan)->key_components=
3478
param->table->key_info[(*scan)->keynr].key_parts;
3481
Run covering-ROR-search algorithm.
3482
Assume set I is [ror_scan .. ror_scans_end)
3485
/*I=set of all covering indexes */
3486
ror_scan_mark= tree->ror_scans;
3488
MY_BITMAP *covered_fields= ¶m->tmp_covered_fields;
3489
if (!covered_fields->bitmap)
3490
covered_fields->bitmap= (my_bitmap_map*)alloc_root(param->mem_root,
3491
param->fields_bitmap_size);
3492
if (!covered_fields->bitmap ||
3493
bitmap_init(covered_fields, covered_fields->bitmap,
3494
param->table->s->fields, FALSE))
3496
bitmap_clear_all(covered_fields);
3498
double total_cost= 0.0f;
3502
DBUG_PRINT("info", ("Building covering ROR-intersection"));
3503
DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
3504
"building covering ROR-I",
3505
ror_scan_mark, ror_scans_end););
3509
Update changed sorting info:
3511
number of first not covered component
3512
Calculate and save these values for each of remaining scans.
3514
for (ROR_SCAN_INFO **scan= ror_scan_mark; scan != ror_scans_end; ++scan)
3516
bitmap_subtract(&(*scan)->covered_fields, covered_fields);
3517
(*scan)->used_fields_covered=
3518
bitmap_bits_set(&(*scan)->covered_fields);
3519
(*scan)->first_uncovered_field=
3520
bitmap_get_first(&(*scan)->covered_fields);
3523
my_qsort(ror_scan_mark, ror_scans_end-ror_scan_mark, sizeof(ROR_SCAN_INFO*),
3524
(qsort_cmp)cmp_ror_scan_info_covering);
3526
DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
3528
ror_scan_mark, ror_scans_end););
3531
total_cost += (*ror_scan_mark)->index_read_cost;
3532
records += (*ror_scan_mark)->records;
3533
DBUG_PRINT("info", ("Adding scan on %s",
3534
param->table->key_info[(*ror_scan_mark)->keynr].name));
3535
if (total_cost > read_time)
3537
/* F=F-covered by first(I) */
3538
bitmap_union(covered_fields, &(*ror_scan_mark)->covered_fields);
3539
all_covered= bitmap_is_subset(¶m->needed_fields, covered_fields);
3540
} while ((++ror_scan_mark < ror_scans_end) && !all_covered);
3542
if (!all_covered || (ror_scan_mark - tree->ror_scans) == 1)
3546
Ok, [tree->ror_scans .. ror_scan) holds covering index_intersection with
3549
DBUG_PRINT("info", ("Covering ROR-intersect scans cost: %g", total_cost));
3550
DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
3551
"creating covering ROR-intersect",
3552
tree->ror_scans, ror_scan_mark););
3554
/* Add priority queue use cost. */
3555
total_cost += rows2double(records)*
3556
log((double)(ror_scan_mark - tree->ror_scans)) /
3557
(TIME_FOR_COMPARE_ROWID * M_LN2);
3558
DBUG_PRINT("info", ("Covering ROR-intersect full cost: %g", total_cost));
3560
if (total_cost > read_time)
3563
TRP_ROR_INTERSECT *trp;
3564
if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
3566
uint best_num= (ror_scan_mark - tree->ror_scans);
3567
if (!(trp->first_scan= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
3568
sizeof(ROR_SCAN_INFO*)*
3571
memcpy(trp->first_scan, tree->ror_scans, best_num*sizeof(ROR_SCAN_INFO*));
3572
trp->last_scan= trp->first_scan + best_num;
3573
trp->is_covering= TRUE;
3574
trp->read_cost= total_cost;
3575
trp->records= records;
3576
trp->cpk_scan= NULL;
3577
set_if_smaller(param->table->quick_condition_rows, records);
3580
("Returning covering ROR-intersect plan: cost %g, records %lu",
3581
trp->read_cost, (ulong) trp->records));
3587
Get best "range" table read plan for given SEL_TREE, also update some info
3590
get_key_scans_params()
3591
param Parameters from test_quick_select
3592
tree Make range select for this SEL_TREE
3593
index_read_must_be_used TRUE <=> assume 'index only' option will be set
3594
(except for clustered PK indexes)
3595
update_tbl_stats TRUE <=> update table->quick_* with information
3596
about range scans we've evaluated.
3597
read_time Maximum cost. i.e. don't create read plans with
3601
Find the best "range" table read plan for given SEL_TREE.
3602
The side effects are
3603
- tree->ror_scans is updated to indicate which scans are ROR scans.
3604
- if update_tbl_stats=TRUE then table->quick_* is updated with info
3605
about every possible range scan.
3608
Best range read plan
3609
NULL if no plan found or error occurred
3612
static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
3613
bool index_read_must_be_used,
3614
bool update_tbl_stats,
3618
SEL_ARG **key,**end, **key_to_read= NULL;
3619
ha_rows best_records= 0;
3620
uint best_mrr_flags= 0, best_buf_size= 0;
3621
TRP_RANGE* read_plan= NULL;
3622
DBUG_ENTER("get_key_scans_params");
3624
Note that there may be trees that have type SEL_TREE::KEY but contain no
3625
key reads at all, e.g. tree for expression "key1 is not null" where key1
3626
is defined as "not null".
3628
DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->keys_map,
3630
tree->ror_scans_map.clear_all();
3631
tree->n_ror_scans= 0;
3632
for (idx= 0,key=tree->keys, end=key+param->keys; key != end; key++,idx++)
3636
ha_rows found_records;
3638
double found_read_time;
3639
uint mrr_flags, buf_size;
3640
uint keynr= param->real_keynr[idx];
3641
if ((*key)->type == SEL_ARG::MAYBE_KEY ||
3643
param->needed_reg->set_bit(keynr);
3645
bool read_index_only= index_read_must_be_used ||
3646
param->table->covering_keys.is_set(keynr);
3648
found_records= check_quick_select(param, idx, read_index_only, *key,
3649
update_tbl_stats, &mrr_flags,
3651
found_read_time= cost.total_cost();
3652
if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
3654
tree->n_ror_scans++;
3655
tree->ror_scans_map.set_bit(idx);
3657
if (read_time > found_read_time && found_records != HA_POS_ERROR)
3659
read_time= found_read_time;
3660
best_records= found_records;
3662
best_mrr_flags= mrr_flags;
3663
best_buf_size= buf_size;
3668
DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->ror_scans_map,
3672
idx= key_to_read - tree->keys;
3673
if ((read_plan= new (param->mem_root) TRP_RANGE(*key_to_read, idx,
3676
read_plan->records= best_records;
3677
read_plan->is_ror= tree->ror_scans_map.is_set(idx);
3678
read_plan->read_cost= read_time;
3679
read_plan->mrr_buf_size= best_buf_size;
3681
("Returning range plan for key %s, cost %g, records %lu",
3682
param->table->key_info[param->real_keynr[idx]].name,
3683
read_plan->read_cost, (ulong) read_plan->records));
3687
DBUG_PRINT("info", ("No 'range' table read plan found"));
3689
DBUG_RETURN(read_plan);
3693
QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
3694
bool retrieve_full_rows,
3695
MEM_ROOT *parent_alloc)
3697
QUICK_INDEX_MERGE_SELECT *quick_imerge;
3698
QUICK_RANGE_SELECT *quick;
3699
/* index_merge always retrieves full rows, ignore retrieve_full_rows */
3700
if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->thd, param->table)))
3703
quick_imerge->records= records;
3704
quick_imerge->read_time= read_cost;
3705
for (TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
3708
if (!(quick= (QUICK_RANGE_SELECT*)
3709
((*range_scan)->make_quick(param, FALSE, &quick_imerge->alloc)))||
3710
quick_imerge->push_quick_back(quick))
3713
delete quick_imerge;
3717
return quick_imerge;
3720
QUICK_SELECT_I *TRP_ROR_INTERSECT::make_quick(PARAM *param,
3721
bool retrieve_full_rows,
3722
MEM_ROOT *parent_alloc)
3724
QUICK_ROR_INTERSECT_SELECT *quick_intrsect;
3725
QUICK_RANGE_SELECT *quick;
3726
DBUG_ENTER("TRP_ROR_INTERSECT::make_quick");
3729
if ((quick_intrsect=
3730
new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
3731
(retrieve_full_rows? (!is_covering) :
3735
DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
3736
"creating ROR-intersect",
3737
first_scan, last_scan););
3738
alloc= parent_alloc? parent_alloc: &quick_intrsect->alloc;
3739
for (; first_scan != last_scan;++first_scan)
3741
if (!(quick= get_quick_select(param, (*first_scan)->idx,
3742
(*first_scan)->sel_arg,
3743
HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
3745
quick_intrsect->push_quick_back(quick))
3747
delete quick_intrsect;
3753
if (!(quick= get_quick_select(param, cpk_scan->idx,
3755
HA_MRR_USE_DEFAULT_IMPL | HA_MRR_SORTED,
3758
delete quick_intrsect;
3762
quick_intrsect->cpk_quick= quick;
3764
quick_intrsect->records= records;
3765
quick_intrsect->read_time= read_cost;
3767
DBUG_RETURN(quick_intrsect);
3771
QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
3772
bool retrieve_full_rows,
3773
MEM_ROOT *parent_alloc)
3775
QUICK_ROR_UNION_SELECT *quick_roru;
3776
TABLE_READ_PLAN **scan;
3777
QUICK_SELECT_I *quick;
3778
DBUG_ENTER("TRP_ROR_UNION::make_quick");
3780
It is impossible to construct a ROR-union that will not retrieve full
3781
rows, ignore retrieve_full_rows parameter.
3783
if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
3785
for (scan= first_ror; scan != last_ror; scan++)
3787
if (!(quick= (*scan)->make_quick(param, FALSE, &quick_roru->alloc)) ||
3788
quick_roru->push_quick_back(quick))
3791
quick_roru->records= records;
3792
quick_roru->read_time= read_cost;
3794
DBUG_RETURN(quick_roru);
3799
Build a SEL_TREE for <> or NOT BETWEEN predicate
3803
param PARAM from SQL_SELECT::test_quick_select
3804
cond_func item for the predicate
3805
field field in the predicate
3806
lt_value constant that field should be smaller
3807
gt_value constant that field should be greaterr
3808
cmp_type compare type for the field
3811
# Pointer to tree built tree
3815
static SEL_TREE *get_ne_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func,
3817
Item *lt_value, Item *gt_value,
3818
Item_result cmp_type)
3821
tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
3822
lt_value, cmp_type);
3825
tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
3827
gt_value, cmp_type));
3834
Build a SEL_TREE for a simple predicate
3838
param PARAM from SQL_SELECT::test_quick_select
3839
cond_func item for the predicate
3840
field field in the predicate
3841
value constant in the predicate
3842
cmp_type compare type for the field
3843
inv TRUE <> NOT cond_func is considered
3844
(makes sense only when cond_func is BETWEEN or IN)
3847
Pointer to the tree built tree
3850
static SEL_TREE *get_func_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func,
3851
Field *field, Item *value,
3852
Item_result cmp_type, bool inv)
3855
DBUG_ENTER("get_func_mm_tree");
3857
switch (cond_func->functype()) {
3859
case Item_func::NE_FUNC:
3860
tree= get_ne_mm_tree(param, cond_func, field, value, value, cmp_type);
3863
case Item_func::BETWEEN:
3869
tree= get_ne_mm_tree(param, cond_func, field, cond_func->arguments()[1],
3870
cond_func->arguments()[2], cmp_type);
3874
tree= get_mm_parts(param, cond_func, field, Item_func::GE_FUNC,
3875
cond_func->arguments()[1],cmp_type);
3878
tree= tree_and(param, tree, get_mm_parts(param, cond_func, field,
3880
cond_func->arguments()[2],
3886
tree= get_mm_parts(param, cond_func, field,
3888
(value == (Item*)1 ? Item_func::GT_FUNC :
3889
Item_func::LT_FUNC):
3890
(value == (Item*)1 ? Item_func::LE_FUNC :
3891
Item_func::GE_FUNC)),
3892
cond_func->arguments()[0], cmp_type);
3895
case Item_func::IN_FUNC:
3897
Item_func_in *func=(Item_func_in*) cond_func;
3900
Array for IN() is constructed when all values have the same result
3901
type. Tree won't be built for values with different result types,
3902
so we check it here to avoid unnecessary work.
3904
if (!func->arg_types_compatible)
3909
if (func->array && func->array->result_type() != ROW_RESULT)
3912
We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
3913
where c{i} are constants. Our goal is to produce a SEL_TREE that
3914
represents intervals:
3916
($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ... (*)
3918
where $MIN is either "-inf" or NULL.
3920
The most straightforward way to produce it is to convert NOT IN
3921
into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
3922
analyzer to build SEL_TREE from that. The problem is that the
3923
range analyzer will use O(N^2) memory (which is probably a bug),
3924
and people do use big NOT IN lists (e.g. see BUG#15872, BUG#21282),
3925
will run out of memory.
3927
Another problem with big lists like (*) is that a big list is
3928
unlikely to produce a good "range" access, while considering that
3929
range access will require expensive CPU calculations (and for
3930
MyISAM even index accesses). In short, big NOT IN lists are rarely
3933
Considering the above, we'll handle NOT IN as follows:
3934
* if the number of entries in the NOT IN list is less than
3935
NOT_IN_IGNORE_THRESHOLD, construct the SEL_TREE (*) manually.
3936
* Otherwise, don't produce a SEL_TREE.
3938
#define NOT_IN_IGNORE_THRESHOLD 1000
3939
MEM_ROOT *tmp_root= param->mem_root;
3940
param->thd->mem_root= param->old_root;
3942
Create one Item_type constant object. We'll need it as
3943
get_mm_parts only accepts constant values wrapped in Item_Type
3945
We create the Item on param->mem_root which points to
3946
per-statement mem_root (while thd->mem_root is currently pointing
3947
to mem_root local to range optimizer).
3949
Item *value_item= func->array->create_item();
3950
param->thd->mem_root= tmp_root;
3952
if (func->array->count > NOT_IN_IGNORE_THRESHOLD || !value_item)
3955
/* Get a SEL_TREE for "(-inf|NULL) < X < c_0" interval. */
3959
func->array->value_to_item(i, value_item);
3960
tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
3961
value_item, cmp_type);
3965
} while (i < func->array->count && tree->type == SEL_TREE::IMPOSSIBLE);
3967
if (!tree || tree->type == SEL_TREE::IMPOSSIBLE)
3969
/* We get here in cases like "t.unsigned NOT IN (-1,-2,-3) */
3974
for (; i < func->array->count; i++)
3976
if (func->array->compare_elems(i, i-1))
3978
/* Get a SEL_TREE for "-inf < X < c_i" interval */
3979
func->array->value_to_item(i, value_item);
3980
tree2= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
3981
value_item, cmp_type);
3988
/* Change all intervals to be "c_{i-1} < X < c_i" */
3989
for (uint idx= 0; idx < param->keys; idx++)
3991
SEL_ARG *new_interval, *last_val;
3992
if (((new_interval= tree2->keys[idx])) &&
3993
(tree->keys[idx]) &&
3994
((last_val= tree->keys[idx]->last())))
3996
new_interval->min_value= last_val->max_value;
3997
new_interval->min_flag= NEAR_MIN;
4001
The following doesn't try to allocate memory so no need to
4004
tree= tree_or(param, tree, tree2);
4008
if (tree && tree->type != SEL_TREE::IMPOSSIBLE)
4011
Get the SEL_TREE for the last "c_last < X < +inf" interval
4012
(value_item cotains c_last already)
4014
tree2= get_mm_parts(param, cond_func, field, Item_func::GT_FUNC,
4015
value_item, cmp_type);
4016
tree= tree_or(param, tree, tree2);
4021
tree= get_ne_mm_tree(param, cond_func, field,
4022
func->arguments()[1], func->arguments()[1],
4027
for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
4030
tree= tree_and(param, tree, get_ne_mm_tree(param, cond_func, field,
4031
*arg, *arg, cmp_type));
4038
tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
4039
func->arguments()[1], cmp_type);
4043
for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
4046
tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
4057
Here the function for the following predicates are processed:
4058
<, <=, =, >=, >, LIKE, IS NULL, IS NOT NULL.
4059
If the predicate is of the form (value op field) it is handled
4060
as the equivalent predicate (field rev_op value), e.g.
4061
2 <= a is handled as a >= 2.
4063
Item_func::Functype func_type=
4064
(value != cond_func->arguments()[0]) ? cond_func->functype() :
4065
((Item_bool_func2*) cond_func)->rev_functype();
4066
tree= get_mm_parts(param, cond_func, field, func_type, value, cmp_type);
4075
Build conjunction of all SEL_TREEs for a simple predicate applying equalities
4078
get_full_func_mm_tree()
4079
param PARAM from SQL_SELECT::test_quick_select
4080
cond_func item for the predicate
4081
field_item field in the predicate
4082
value constant in the predicate
4083
(for BETWEEN it contains the number of the field argument,
4084
for IN it's always 0)
4085
inv TRUE <> NOT cond_func is considered
4086
(makes sense only when cond_func is BETWEEN or IN)
4089
For a simple SARGable predicate of the form (f op c), where f is a field and
4090
c is a constant, the function builds a conjunction of all SEL_TREES that can
4091
be obtained by the substitution of f for all different fields equal to f.
4094
If the WHERE condition contains a predicate (fi op c),
4095
then not only SELL_TREE for this predicate is built, but
4096
the trees for the results of substitution of fi for
4097
each fj belonging to the same multiple equality as fi
4099
E.g. for WHERE t1.a=t2.a AND t2.a > 10
4100
a SEL_TREE for t2.a > 10 will be built for quick select from t2
4102
a SEL_TREE for t1.a > 10 will be built for quick select from t1.
4104
A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
4105
in a similar way: we build a conjuction of trees for the results
4106
of all substitutions of fi for equal fj.
4107
Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
4108
differently. It is considered as a conjuction of two SARGable
4109
predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
4110
is called for each of them separately producing trees for
4111
AND j (f1j <=c ) and AND j (f2j <= c)
4112
After this these two trees are united in one conjunctive tree.
4113
It's easy to see that the same tree is obtained for
4114
AND j,k (f1j <=c AND f2k<=c)
4115
which is equivalent to
4116
AND j,k (c BETWEEN f1j AND f2k).
4117
The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
4118
which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
4119
function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
4120
producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
4121
trees are united in one OR-tree. The expression
4122
(AND j (f1j > c) OR AND j (f2j < c)
4123
is equivalent to the expression
4124
AND j,k (f1j > c OR f2k < c)
4125
which is just a translation of
4126
AND j,k (c NOT BETWEEN f1j AND f2k)
4128
In the cases when one of the items f1, f2 is a constant c1 we do not create
4129
a tree for it at all. It works for BETWEEN predicates but does not
4130
work for NOT BETWEEN predicates as we have to evaluate the expression
4131
with it. If it is TRUE then the other tree can be completely ignored.
4132
We do not do it now and no trees are built in these cases for
4133
NOT BETWEEN predicates.
4135
As to IN predicates only ones of the form (f IN (c1,...,cn)),
4136
where f1 is a field and c1,...,cn are constant, are considered as
4137
SARGable. We never try to narrow the index scan using predicates of
4138
the form (c IN (c1,...,f,...,cn)).
4141
Pointer to the tree representing the built conjunction of SEL_TREEs
4144
static SEL_TREE *get_full_func_mm_tree(RANGE_OPT_PARAM *param,
4145
Item_func *cond_func,
4146
Item_field *field_item, Item *value,
4151
table_map ref_tables= 0;
4152
table_map param_comp= ~(param->prev_tables | param->read_tables |
4153
param->current_table);
4154
DBUG_ENTER("get_full_func_mm_tree");
4156
for (uint i= 0; i < cond_func->arg_count; i++)
4158
Item *arg= cond_func->arguments()[i]->real_item();
4159
if (arg != field_item)
4160
ref_tables|= arg->used_tables();
4162
Field *field= field_item->field;
4163
Item_result cmp_type= field->cmp_type();
4164
if (!((ref_tables | field->table->map) & param_comp))
4165
ftree= get_func_mm_tree(param, cond_func, field, value, cmp_type, inv);
4166
Item_equal *item_equal= field_item->item_equal;
4169
Item_equal_iterator it(*item_equal);
4171
while ((item= it++))
4173
Field *f= item->field;
4176
if (!((ref_tables | f->table->map) & param_comp))
4178
tree= get_func_mm_tree(param, cond_func, f, value, cmp_type, inv);
4179
ftree= !ftree ? tree : tree_and(param, ftree, tree);
4186
/* make a select tree of all keys in condition */
4188
static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,COND *cond)
4192
Item_field *field_item= 0;
4195
DBUG_ENTER("get_mm_tree");
4197
if (cond->type() == Item::COND_ITEM)
4199
List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
4201
if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4207
SEL_TREE *new_tree=get_mm_tree(param,item);
4208
if (param->thd->is_fatal_error ||
4209
param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
4210
DBUG_RETURN(0); // out of memory
4211
tree=tree_and(param,tree,new_tree);
4212
if (tree && tree->type == SEL_TREE::IMPOSSIBLE)
4218
tree=get_mm_tree(param,li++);
4224
SEL_TREE *new_tree=get_mm_tree(param,item);
4226
DBUG_RETURN(0); // out of memory
4227
tree=tree_or(param,tree,new_tree);
4228
if (!tree || tree->type == SEL_TREE::ALWAYS)
4235
/* Here when simple cond */
4236
if (cond->const_item())
4239
During the cond->val_int() evaluation we can come across a subselect
4240
item which may allocate memory on the thd->mem_root and assumes
4241
all the memory allocated has the same life span as the subselect
4242
item itself. So we have to restore the thread's mem_root here.
4244
MEM_ROOT *tmp_root= param->mem_root;
4245
param->thd->mem_root= param->old_root;
4246
tree= cond->val_int() ? new(tmp_root) SEL_TREE(SEL_TREE::ALWAYS) :
4247
new(tmp_root) SEL_TREE(SEL_TREE::IMPOSSIBLE);
4248
param->thd->mem_root= tmp_root;
4252
table_map ref_tables= 0;
4253
table_map param_comp= ~(param->prev_tables | param->read_tables |
4254
param->current_table);
4255
if (cond->type() != Item::FUNC_ITEM)
4256
{ // Should be a field
4257
ref_tables= cond->used_tables();
4258
if ((ref_tables & param->current_table) ||
4259
(ref_tables & ~(param->prev_tables | param->read_tables)))
4261
DBUG_RETURN(new SEL_TREE(SEL_TREE::MAYBE));
4264
Item_func *cond_func= (Item_func*) cond;
4265
if (cond_func->functype() == Item_func::BETWEEN ||
4266
cond_func->functype() == Item_func::IN_FUNC)
4267
inv= ((Item_func_opt_neg *) cond_func)->negated;
4268
else if (cond_func->select_optimize() == Item_func::OPTIMIZE_NONE)
4273
switch (cond_func->functype()) {
4274
case Item_func::BETWEEN:
4275
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
4277
field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
4278
ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
4282
Concerning the code below see the NOTES section in
4283
the comments for the function get_full_func_mm_tree()
4285
for (uint i= 1 ; i < cond_func->arg_count ; i++)
4287
if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
4289
field_item= (Item_field*) (cond_func->arguments()[i]->real_item());
4290
SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func,
4291
field_item, (Item*)(intptr)i, inv);
4293
tree= !tree ? tmp : tree_or(param, tree, tmp);
4295
tree= tree_and(param, tree, tmp);
4304
ftree = tree_and(param, ftree, tree);
4306
case Item_func::IN_FUNC:
4308
Item_func_in *func=(Item_func_in*) cond_func;
4309
if (func->key_item()->real_item()->type() != Item::FIELD_ITEM)
4311
field_item= (Item_field*) (func->key_item()->real_item());
4312
ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
4315
case Item_func::MULT_EQUAL_FUNC:
4317
Item_equal *item_equal= (Item_equal *) cond;
4318
if (!(value= item_equal->get_const()))
4320
Item_equal_iterator it(*item_equal);
4321
ref_tables= value->used_tables();
4322
while ((field_item= it++))
4324
Field *field= field_item->field;
4325
Item_result cmp_type= field->cmp_type();
4326
if (!((ref_tables | field->table->map) & param_comp))
4328
tree= get_mm_parts(param, cond, field, Item_func::EQ_FUNC,
4330
ftree= !ftree ? tree : tree_and(param, ftree, tree);
4337
if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
4339
field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
4340
value= cond_func->arg_count > 1 ? cond_func->arguments()[1] : 0;
4342
else if (cond_func->have_rev_func() &&
4343
cond_func->arguments()[1]->real_item()->type() ==
4346
field_item= (Item_field*) (cond_func->arguments()[1]->real_item());
4347
value= cond_func->arguments()[0];
4351
ftree= get_full_func_mm_tree(param, cond_func, field_item, value, inv);
4359
get_mm_parts(RANGE_OPT_PARAM *param, COND *cond_func, Field *field,
4360
Item_func::Functype type,
4361
Item *value, Item_result cmp_type)
4363
DBUG_ENTER("get_mm_parts");
4364
if (field->table != param->table)
4367
KEY_PART *key_part = param->key_parts;
4368
KEY_PART *end = param->key_parts_end;
4371
value->used_tables() & ~(param->prev_tables | param->read_tables))
4373
for (; key_part != end ; key_part++)
4375
if (field->eq(key_part->field))
4378
if (!tree && !(tree=new SEL_TREE()))
4379
DBUG_RETURN(0); // OOM
4380
if (!value || !(value->used_tables() & ~param->read_tables))
4382
sel_arg=get_mm_leaf(param,cond_func,
4383
key_part->field,key_part,type,value);
4386
if (sel_arg->type == SEL_ARG::IMPOSSIBLE)
4388
tree->type=SEL_TREE::IMPOSSIBLE;
4394
// This key may be used later
4395
if (!(sel_arg= new SEL_ARG(SEL_ARG::MAYBE_KEY)))
4396
DBUG_RETURN(0); // OOM
4398
sel_arg->part=(uchar) key_part->part;
4399
tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
4400
tree->keys_map.set_bit(key_part->key);
4409
get_mm_leaf(RANGE_OPT_PARAM *param, COND *conf_func, Field *field,
4410
KEY_PART *key_part, Item_func::Functype type,Item *value)
4412
uint maybe_null=(uint) field->real_maybe_null();
4413
bool optimize_range;
4415
MEM_ROOT *alloc= param->mem_root;
4417
ulong orig_sql_mode;
4419
DBUG_ENTER("get_mm_leaf");
4422
We need to restore the runtime mem_root of the thread in this
4423
function because it evaluates the value of its argument, while
4424
the argument can be any, e.g. a subselect. The subselect
4425
items, in turn, assume that all the memory allocated during
4426
the evaluation has the same life span as the item itself.
4427
TODO: opt_range.cc should not reset thd->mem_root at all.
4429
param->thd->mem_root= param->old_root;
4430
if (!value) // IS NULL or IS NOT NULL
4432
if (field->table->maybe_null) // Can't use a key on this
4434
if (!maybe_null) // Not null field
4436
if (type == Item_func::ISNULL_FUNC)
4437
tree= &null_element;
4440
if (!(tree= new (alloc) SEL_ARG(field,is_null_string,is_null_string)))
4441
goto end; // out of memory
4442
if (type == Item_func::ISNOTNULL_FUNC)
4444
tree->min_flag=NEAR_MIN; /* IS NOT NULL -> X > NULL */
4445
tree->max_flag=NO_MAX_RANGE;
4451
1. Usually we can't use an index if the column collation
4452
differ from the operation collation.
4454
2. However, we can reuse a case insensitive index for
4455
the binary searches:
4457
WHERE latin1_swedish_ci_column = 'a' COLLATE lati1_bin;
4459
WHERE latin1_swedish_ci_colimn = BINARY 'a '
4462
if (field->result_type() == STRING_RESULT &&
4463
value->result_type() == STRING_RESULT &&
4464
key_part->image_type == Field::itRAW &&
4465
((Field_str*)field)->charset() != conf_func->compare_collation() &&
4466
!(conf_func->compare_collation()->state & MY_CS_BINSORT))
4469
if (param->using_real_indexes)
4470
optimize_range= field->optimize_range(param->real_keynr[key_part->key],
4473
optimize_range= TRUE;
4475
if (type == Item_func::LIKE_FUNC)
4478
char buff1[MAX_FIELD_WIDTH];
4479
uchar *min_str,*max_str;
4480
String tmp(buff1,sizeof(buff1),value->collation.collation),*res;
4481
size_t length, offset, min_length, max_length;
4482
uint field_length= field->pack_length()+maybe_null;
4484
if (!optimize_range)
4486
if (!(res= value->val_str(&tmp)))
4488
tree= &null_element;
4494
Check if this was a function. This should have be optimized away
4495
in the sql_select.cc
4499
tmp.copy(*res); // Get own copy
4502
if (field->cmp_type() != STRING_RESULT)
4503
goto end; // Can only optimize strings
4506
length=key_part->store_length;
4508
if (length != key_part->length + maybe_null)
4510
/* key packed with length prefix */
4511
offset+= HA_KEY_BLOB_LENGTH;
4512
field_length= length - HA_KEY_BLOB_LENGTH;
4516
if (unlikely(length < field_length))
4519
This can only happen in a table created with UNIREG where one key
4520
overlaps many fields
4522
length= field_length;
4525
field_length= length;
4528
if (!(min_str= (uchar*) alloc_root(alloc, length*2)))
4531
max_str=min_str+length;
4533
max_str[0]= min_str[0]=0;
4535
field_length-= maybe_null;
4536
like_error= my_like_range(field->charset(),
4537
res->ptr(), res->length(),
4538
((Item_func_like*)(param->cond))->escape,
4539
wild_one, wild_many,
4541
(char*) min_str+offset, (char*) max_str+offset,
4542
&min_length, &max_length);
4543
if (like_error) // Can't optimize with LIKE
4546
if (offset != maybe_null) // BLOB or VARCHAR
4548
int2store(min_str+maybe_null,min_length);
4549
int2store(max_str+maybe_null,max_length);
4551
tree= new (alloc) SEL_ARG(field, min_str, max_str);
4555
if (!optimize_range &&
4556
type != Item_func::EQ_FUNC &&
4557
type != Item_func::EQUAL_FUNC)
4558
goto end; // Can't optimize this
4561
We can't always use indexes when comparing a string index to a number
4562
cmp_type() is checked to allow compare of dates to numbers
4564
if (field->result_type() == STRING_RESULT &&
4565
value->result_type() != STRING_RESULT &&
4566
field->cmp_type() != value->result_type())
4568
/* For comparison purposes allow invalid dates like 2000-01-32 */
4569
orig_sql_mode= field->table->in_use->variables.sql_mode;
4570
if (value->real_item()->type() == Item::STRING_ITEM &&
4571
(field->type() == MYSQL_TYPE_DATE ||
4572
field->type() == MYSQL_TYPE_DATETIME))
4573
field->table->in_use->variables.sql_mode|= MODE_INVALID_DATES;
4574
err= value->save_in_field_no_warnings(field, 1);
4577
if (field->cmp_type() != value->result_type())
4579
if ((type == Item_func::EQ_FUNC || type == Item_func::EQUAL_FUNC) &&
4580
value->result_type() == item_cmp_type(field->result_type(),
4581
value->result_type()))
4583
tree= new (alloc) SEL_ARG(field, 0, 0);
4584
tree->type= SEL_ARG::IMPOSSIBLE;
4590
TODO: We should return trees of the type SEL_ARG::IMPOSSIBLE
4591
for the cases like int_field > 999999999999999999999999 as well.
4594
if (err == 3 && field->type() == FIELD_TYPE_DATE &&
4595
(type == Item_func::GT_FUNC || type == Item_func::GE_FUNC ||
4596
type == Item_func::LT_FUNC || type == Item_func::LE_FUNC) )
4599
We were saving DATETIME into a DATE column, the conversion went ok
4600
but a non-zero time part was cut off.
4602
In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
4603
values. Index over a DATE column uses DATE comparison. Changing
4604
from one comparison to the other is possible:
4606
datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
4607
datetime(date_col)<='2007-12-10 12:34:55' -> date_col<='2007-12-10'
4609
datetime(date_col)> '2007-12-10 12:34:55' -> date_col>='2007-12-10'
4610
datetime(date_col)>='2007-12-10 12:34:55' -> date_col>='2007-12-10'
4612
but we'll need to convert '>' to '>=' and '<' to '<='. This will
4613
be done together with other types at the end of this function
4614
(grep for field_is_equal_to_item)
4623
guaranteed at this point: err > 0; field and const of same type
4624
If an integer got bounded (e.g. to within 0..255 / -128..127)
4625
for < or >, set flags as for <= or >= (no NEAR_MAX / NEAR_MIN)
4627
else if (err == 1 && field->result_type() == INT_RESULT)
4629
if (type == Item_func::LT_FUNC && (value->val_int() > 0))
4630
type = Item_func::LE_FUNC;
4631
else if (type == Item_func::GT_FUNC &&
4632
!((Field_num*)field)->unsigned_flag &&
4633
!((Item_int*)value)->unsigned_flag &&
4634
(value->val_int() < 0))
4635
type = Item_func::GE_FUNC;
4640
field->table->in_use->variables.sql_mode= orig_sql_mode;
4641
/* This happens when we try to insert a NULL field in a not null column */
4642
tree= &null_element; // cmp with NULL is never TRUE
4645
field->table->in_use->variables.sql_mode= orig_sql_mode;
4646
str= (uchar*) alloc_root(alloc, key_part->store_length+1);
4650
*str= (uchar) field->is_real_null(); // Set to 1 if null
4651
field->get_key_image(str+maybe_null, key_part->length,
4652
key_part->image_type);
4653
if (!(tree= new (alloc) SEL_ARG(field, str, str)))
4654
goto end; // out of memory
4657
Check if we are comparing an UNSIGNED integer with a negative constant.
4658
In this case we know that:
4659
(a) (unsigned_int [< | <=] negative_constant) == FALSE
4660
(b) (unsigned_int [> | >=] negative_constant) == TRUE
4661
In case (a) the condition is false for all values, and in case (b) it
4662
is true for all values, so we can avoid unnecessary retrieval and condition
4663
testing, and we also get correct comparison of unsinged integers with
4664
negative integers (which otherwise fails because at query execution time
4665
negative integers are cast to unsigned if compared with unsigned).
4667
if (field->result_type() == INT_RESULT &&
4668
value->result_type() == INT_RESULT &&
4669
((Field_num*)field)->unsigned_flag && !((Item_int*)value)->unsigned_flag)
4671
longlong item_val= value->val_int();
4674
if (type == Item_func::LT_FUNC || type == Item_func::LE_FUNC)
4676
tree->type= SEL_ARG::IMPOSSIBLE;
4679
if (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC)
4688
case Item_func::LT_FUNC:
4689
if (field_is_equal_to_item(field,value))
4690
tree->max_flag=NEAR_MAX;
4692
case Item_func::LE_FUNC:
4694
tree->min_flag=NO_MIN_RANGE; /* From start */
4697
tree->min_value=is_null_string;
4698
tree->min_flag=NEAR_MIN;
4701
case Item_func::GT_FUNC:
4702
/* Don't use open ranges for partial key_segments */
4703
if (field_is_equal_to_item(field,value) &&
4704
!(key_part->flag & HA_PART_KEY_SEG))
4705
tree->min_flag=NEAR_MIN;
4707
case Item_func::GE_FUNC:
4708
tree->max_flag=NO_MAX_RANGE;
4715
param->thd->mem_root= alloc;
4720
/******************************************************************************
4721
** Tree manipulation functions
4722
** If tree is 0 it means that the condition can't be tested. It refers
4723
** to a non existent table or to a field in current table with isn't a key.
4724
** The different tree flags:
4725
** IMPOSSIBLE: Condition is never TRUE
4726
** ALWAYS: Condition is always TRUE
4727
** MAYBE: Condition may exists when tables are read
4728
** MAYBE_KEY: Condition refers to a key that may be used in join loop
4729
** KEY_RANGE: Condition uses a key
4730
******************************************************************************/
4733
Add a new key test to a key when scanning through all keys
4734
This will never be called for same key parts.
4738
sel_add(SEL_ARG *key1,SEL_ARG *key2)
4740
SEL_ARG *root,**key_link;
4748
while (key1 && key2)
4750
if (key1->part < key2->part)
4753
key_link= &key1->next_key_part;
4754
key1=key1->next_key_part;
4759
key_link= &key2->next_key_part;
4760
key2=key2->next_key_part;
4763
*key_link=key1 ? key1 : key2;
4767
#define CLONE_KEY1_MAYBE 1
4768
#define CLONE_KEY2_MAYBE 2
4769
#define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)
4773
tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
4775
DBUG_ENTER("tree_and");
4780
if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
4782
if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
4784
if (tree1->type == SEL_TREE::MAYBE)
4786
if (tree2->type == SEL_TREE::KEY)
4787
tree2->type=SEL_TREE::KEY_SMALLER;
4790
if (tree2->type == SEL_TREE::MAYBE)
4792
tree1->type=SEL_TREE::KEY_SMALLER;
4795
key_map result_keys;
4796
result_keys.clear_all();
4798
/* Join the trees key per key */
4799
SEL_ARG **key1,**key2,**end;
4800
for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
4801
key1 != end ; key1++,key2++)
4806
if (*key1 && !(*key1)->simple_key())
4807
flag|=CLONE_KEY1_MAYBE;
4808
if (*key2 && !(*key2)->simple_key())
4809
flag|=CLONE_KEY2_MAYBE;
4810
*key1=key_and(param, *key1, *key2, flag);
4811
if (*key1 && (*key1)->type == SEL_ARG::IMPOSSIBLE)
4813
tree1->type= SEL_TREE::IMPOSSIBLE;
4816
result_keys.set_bit(key1 - tree1->keys);
4818
if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4819
(*key1)->test_use_count(*key1);
4823
tree1->keys_map= result_keys;
4824
/* dispose index_merge if there is a "range" option */
4825
if (!result_keys.is_clear_all())
4827
tree1->merges.empty();
4831
/* ok, both trees are index_merge trees */
4832
imerge_list_and_list(&tree1->merges, &tree2->merges);
4838
Check if two SEL_TREES can be combined into one (i.e. a single key range
4839
read can be constructed for "cond_of_tree1 OR cond_of_tree2" ) without
4843
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2,
4844
RANGE_OPT_PARAM* param)
4846
key_map common_keys= tree1->keys_map;
4847
DBUG_ENTER("sel_trees_can_be_ored");
4848
common_keys.intersect(tree2->keys_map);
4850
if (common_keys.is_clear_all())
4853
/* trees have a common key, check if they refer to same key part */
4854
SEL_ARG **key1,**key2;
4855
for (uint key_no=0; key_no < param->keys; key_no++)
4857
if (common_keys.is_set(key_no))
4859
key1= tree1->keys + key_no;
4860
key2= tree2->keys + key_no;
4861
if ((*key1)->part == (*key2)->part)
4872
Remove the trees that are not suitable for record retrieval.
4874
param Range analysis parameter
4875
tree Tree to be processed, tree->type is KEY or KEY_SMALLER
4878
This function walks through tree->keys[] and removes the SEL_ARG* trees
4879
that are not "maybe" trees (*) and cannot be used to construct quick range
4881
(*) - have type MAYBE or MAYBE_KEY. Perhaps we should remove trees of
4882
these types here as well.
4884
A SEL_ARG* tree cannot be used to construct quick select if it has
4885
tree->part != 0. (e.g. it could represent "keypart2 < const").
4887
WHY THIS FUNCTION IS NEEDED
4889
Normally we allow construction of SEL_TREE objects that have SEL_ARG
4890
trees that do not allow quick range select construction. For example for
4891
" keypart1=1 AND keypart2=2 " the execution will proceed as follows:
4892
tree1= SEL_TREE { SEL_ARG{keypart1=1} }
4893
tree2= SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
4895
call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
4898
There is an exception though: when we construct index_merge SEL_TREE,
4899
any SEL_ARG* tree that cannot be used to construct quick range select can
4900
be removed, because current range analysis code doesn't provide any way
4901
that tree could be later combined with another tree.
4902
Consider an example: we should not construct
4904
merges = SEL_IMERGE {
4905
SEL_TREE(t.key1part1 = 1),
4906
SEL_TREE(t.key2part2 = 2) -- (*)
4910
- (*) cannot be used to construct quick range select,
4911
- There is no execution path that would cause (*) to be converted to
4912
a tree that could be used.
4914
The latter is easy to verify: first, notice that the only way to convert
4915
(*) into a usable tree is to call tree_and(something, (*)).
4917
Second look at what tree_and/tree_or function would do when passed a
4918
SEL_TREE that has the structure like st1 tree has, and conlcude that
4919
tree_and(something, (*)) will not be called.
4922
0 Ok, some suitable trees left
4923
1 No tree->keys[] left.
4926
static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree)
4929
for (uint i=0; i < param->keys; i++)
4933
if (tree->keys[i]->part)
4935
tree->keys[i]= NULL;
4936
tree->keys_map.clear_bit(i);
4947
tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
4949
DBUG_ENTER("tree_or");
4950
if (!tree1 || !tree2)
4952
if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
4954
if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
4956
if (tree1->type == SEL_TREE::MAYBE)
4957
DBUG_RETURN(tree1); // Can't use this
4958
if (tree2->type == SEL_TREE::MAYBE)
4961
SEL_TREE *result= 0;
4962
key_map result_keys;
4963
result_keys.clear_all();
4964
if (sel_trees_can_be_ored(tree1, tree2, param))
4966
/* Join the trees key per key */
4967
SEL_ARG **key1,**key2,**end;
4968
for (key1= tree1->keys,key2= tree2->keys,end= key1+param->keys ;
4969
key1 != end ; key1++,key2++)
4971
*key1=key_or(param, *key1, *key2);
4974
result=tree1; // Added to tree1
4975
result_keys.set_bit(key1 - tree1->keys);
4977
if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
4978
(*key1)->test_use_count(*key1);
4983
result->keys_map= result_keys;
4987
/* ok, two trees have KEY type but cannot be used without index merge */
4988
if (tree1->merges.is_empty() && tree2->merges.is_empty())
4990
if (param->remove_jump_scans)
4992
bool no_trees= remove_nonrange_trees(param, tree1);
4993
no_trees= no_trees || remove_nonrange_trees(param, tree2);
4995
DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
4998
/* both trees are "range" trees, produce new index merge structure */
4999
if (!(result= new SEL_TREE()) || !(merge= new SEL_IMERGE()) ||
5000
(result->merges.push_back(merge)) ||
5001
(merge->or_sel_tree(param, tree1)) ||
5002
(merge->or_sel_tree(param, tree2)))
5005
result->type= tree1->type;
5007
else if (!tree1->merges.is_empty() && !tree2->merges.is_empty())
5009
if (imerge_list_or_list(param, &tree1->merges, &tree2->merges))
5010
result= new SEL_TREE(SEL_TREE::ALWAYS);
5016
/* one tree is index merge tree and another is range tree */
5017
if (tree1->merges.is_empty())
5018
swap_variables(SEL_TREE*, tree1, tree2);
5020
if (param->remove_jump_scans && remove_nonrange_trees(param, tree2))
5021
DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
5022
/* add tree2 to tree1->merges, checking if it collapses to ALWAYS */
5023
if (imerge_list_or_tree(param, &tree1->merges, tree2))
5024
result= new SEL_TREE(SEL_TREE::ALWAYS);
5029
DBUG_RETURN(result);
5033
/* And key trees where key1->part < key2 -> part */
5036
and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
5040
ulong use_count=key1->use_count;
5042
if (key1->elements != 1)
5044
key2->use_count+=key1->elements-1; //psergey: why we don't count that key1 has n-k-p?
5045
key2->increment_use_count((int) key1->elements-1);
5047
if (key1->type == SEL_ARG::MAYBE_KEY)
5049
key1->right= key1->left= &null_element;
5050
key1->next= key1->prev= 0;
5052
for (next=key1->first(); next ; next=next->next)
5054
if (next->next_key_part)
5056
SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
5057
if (tmp && tmp->type == SEL_ARG::IMPOSSIBLE)
5059
key1=key1->tree_delete(next);
5062
next->next_key_part=tmp;
5064
next->increment_use_count(use_count);
5065
if (param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
5069
next->next_key_part=key2;
5072
return &null_element; // Impossible ranges
5079
Produce a SEL_ARG graph that represents "key1 AND key2"
5083
param Range analysis context (needed to track if we have allocated
5085
key1 First argument, root of its RB-tree
5086
key2 Second argument, root of its RB-tree
5089
RB-tree root of the resulting SEL_ARG graph.
5090
NULL if the result of AND operation is an empty interval {0}.
5094
key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
5100
if (key1->part != key2->part)
5102
if (key1->part > key2->part)
5104
swap_variables(SEL_ARG *, key1, key2);
5105
clone_flag=swap_clone_flag(clone_flag);
5107
// key1->part < key2->part
5109
if (key1->use_count > 0)
5110
if (!(key1= key1->clone_tree(param)))
5112
return and_all_keys(param, key1, key2, clone_flag);
5115
if (((clone_flag & CLONE_KEY2_MAYBE) &&
5116
!(clone_flag & CLONE_KEY1_MAYBE) &&
5117
key2->type != SEL_ARG::MAYBE_KEY) ||
5118
key1->type == SEL_ARG::MAYBE_KEY)
5119
{ // Put simple key in key2
5120
swap_variables(SEL_ARG *, key1, key2);
5121
clone_flag=swap_clone_flag(clone_flag);
5124
/* If one of the key is MAYBE_KEY then the found region may be smaller */
5125
if (key2->type == SEL_ARG::MAYBE_KEY)
5127
if (key1->use_count > 1)
5130
if (!(key1=key1->clone_tree(param)))
5134
if (key1->type == SEL_ARG::MAYBE_KEY)
5135
{ // Both are maybe key
5136
key1->next_key_part=key_and(param, key1->next_key_part,
5137
key2->next_key_part, clone_flag);
5138
if (key1->next_key_part &&
5139
key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
5144
key1->maybe_smaller();
5145
if (key2->next_key_part)
5147
key1->use_count--; // Incremented in and_all_keys
5148
return and_all_keys(param, key1, key2, clone_flag);
5150
key2->use_count--; // Key2 doesn't have a tree
5157
SEL_ARG *e1=key1->first(), *e2=key2->first(), *new_tree=0;
5161
int cmp=e1->cmp_min_to_min(e2);
5164
if (get_range(&e1,&e2,key1))
5167
else if (get_range(&e2,&e1,key2))
5169
SEL_ARG *next=key_and(param, e1->next_key_part, e2->next_key_part,
5171
e1->increment_use_count(1);
5172
e2->increment_use_count(1);
5173
if (!next || next->type != SEL_ARG::IMPOSSIBLE)
5175
SEL_ARG *new_arg= e1->clone_and(e2);
5177
return &null_element; // End of memory
5178
new_arg->next_key_part=next;
5184
new_tree=new_tree->insert(new_arg);
5186
if (e1->cmp_max_to_max(e2) < 0)
5187
e1=e1->next; // e1 can't overlapp next e2
5194
return &null_element; // Impossible range
5200
get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1)
5202
(*e1)=root1->find_range(*e2); // first e1->min < e2->min
5203
if ((*e1)->cmp_max_to_min(*e2) < 0)
5205
if (!((*e1)=(*e1)->next))
5207
if ((*e1)->cmp_min_to_max(*e2) > 0)
5218
key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1,SEL_ARG *key2)
5238
if (key1->part != key2->part)
5242
return 0; // Can't optimize this
5245
// If one of the key is MAYBE_KEY then the found region may be bigger
5246
if (key1->type == SEL_ARG::MAYBE_KEY)
5252
if (key2->type == SEL_ARG::MAYBE_KEY)
5259
if (key1->use_count > 0)
5261
if (key2->use_count == 0 || key1->elements > key2->elements)
5263
swap_variables(SEL_ARG *,key1,key2);
5265
if (key1->use_count > 0 || !(key1=key1->clone_tree(param)))
5269
// Add tree at key2 to tree at key1
5270
bool key2_shared=key2->use_count != 0;
5271
key1->maybe_flag|=key2->maybe_flag;
5273
for (key2=key2->first(); key2; )
5275
SEL_ARG *tmp=key1->find_range(key2); // Find key1.min <= key2.min
5280
tmp=key1->first(); // tmp.min > key2.min
5283
else if ((cmp=tmp->cmp_max_to_min(key2)) < 0)
5284
{ // Found tmp.max < key2.min
5285
SEL_ARG *next=tmp->next;
5286
if (cmp == -2 && eq_tree(tmp->next_key_part,key2->next_key_part))
5288
// Join near ranges like tmp.max < 0 and key2.min >= 0
5289
SEL_ARG *key2_next=key2->next;
5292
if (!(key2=new SEL_ARG(*key2)))
5293
return 0; // out of memory
5294
key2->increment_use_count(key1->use_count+1);
5295
key2->next=key2_next; // New copy of key2
5297
key2->copy_min(tmp);
5298
if (!(key1=key1->tree_delete(tmp)))
5299
{ // Only one key in tree
5306
if (!(tmp=next)) // tmp.min > key2.min
5307
break; // Copy rest of key2
5310
{ // tmp.min > key2.min
5312
if ((tmp_cmp=tmp->cmp_min_to_max(key2)) > 0) // if tmp.min > key2.max
5314
if (tmp_cmp == 2 && eq_tree(tmp->next_key_part,key2->next_key_part))
5315
{ // ranges are connected
5316
tmp->copy_min_to_min(key2);
5317
key1->merge_flags(key2);
5318
if (tmp->min_flag & NO_MIN_RANGE &&
5319
tmp->max_flag & NO_MAX_RANGE)
5321
if (key1->maybe_flag)
5322
return new SEL_ARG(SEL_ARG::MAYBE_KEY);
5325
key2->increment_use_count(-1); // Free not used tree
5331
SEL_ARG *next=key2->next; // Keys are not overlapping
5334
SEL_ARG *cpy= new SEL_ARG(*key2); // Must make copy
5337
key1=key1->insert(cpy);
5338
key2->increment_use_count(key1->use_count+1);
5341
key1=key1->insert(key2); // Will destroy key2_root
5348
// tmp.max >= key2.min && tmp.min <= key.max (overlapping ranges)
5349
if (eq_tree(tmp->next_key_part,key2->next_key_part))
5351
if (tmp->is_same(key2))
5353
tmp->merge_flags(key2); // Copy maybe flags
5354
key2->increment_use_count(-1); // Free not used tree
5359
while (last->next && last->next->cmp_min_to_max(key2) <= 0 &&
5360
eq_tree(last->next->next_key_part,key2->next_key_part))
5364
key1=key1->tree_delete(save);
5366
last->copy_min(tmp);
5367
if (last->copy_min(key2) || last->copy_max(key2))
5370
for (; key2 ; key2=key2->next)
5371
key2->increment_use_count(-1); // Free not used tree
5372
if (key1->maybe_flag)
5373
return new SEL_ARG(SEL_ARG::MAYBE_KEY);
5381
if (cmp >= 0 && tmp->cmp_min_to_min(key2) < 0)
5382
{ // tmp.min <= x < key2.min
5383
SEL_ARG *new_arg=tmp->clone_first(key2);
5386
if ((new_arg->next_key_part= key1->next_key_part))
5387
new_arg->increment_use_count(key1->use_count+1);
5388
tmp->copy_min_to_min(key2);
5389
key1=key1->insert(new_arg);
5392
// tmp.min >= key2.min && tmp.min <= key2.max
5393
SEL_ARG key(*key2); // Get copy we can modify
5396
if (tmp->cmp_min_to_min(&key) > 0)
5397
{ // key.min <= x < tmp.min
5398
SEL_ARG *new_arg=key.clone_first(tmp);
5401
if ((new_arg->next_key_part=key.next_key_part))
5402
new_arg->increment_use_count(key1->use_count+1);
5403
key1=key1->insert(new_arg);
5405
if ((cmp=tmp->cmp_max_to_max(&key)) <= 0)
5406
{ // tmp.min. <= x <= tmp.max
5407
tmp->maybe_flag|= key.maybe_flag;
5408
key.increment_use_count(key1->use_count+1);
5409
tmp->next_key_part= key_or(param, tmp->next_key_part, key.next_key_part);
5410
if (!cmp) // Key2 is ready
5412
key.copy_max_to_min(tmp);
5413
if (!(tmp=tmp->next))
5415
SEL_ARG *tmp2= new SEL_ARG(key);
5418
key1=key1->insert(tmp2);
5422
if (tmp->cmp_min_to_max(&key) > 0)
5424
SEL_ARG *tmp2= new SEL_ARG(key);
5427
key1=key1->insert(tmp2);
5433
SEL_ARG *new_arg=tmp->clone_last(&key); // tmp.min <= x <= key.max
5436
tmp->copy_max_to_min(&key);
5437
tmp->increment_use_count(key1->use_count+1);
5438
/* Increment key count as it may be used for next loop */
5439
key.increment_use_count(1);
5440
new_arg->next_key_part= key_or(param, tmp->next_key_part, key.next_key_part);
5441
key1=key1->insert(new_arg);
5451
SEL_ARG *next=key2->next;
5454
SEL_ARG *tmp=new SEL_ARG(*key2); // Must make copy
5457
key2->increment_use_count(key1->use_count+1);
5458
key1=key1->insert(tmp);
5461
key1=key1->insert(key2); // Will destroy key2_root
5469
/* Compare if two trees are equal */
5471
static bool eq_tree(SEL_ARG* a,SEL_ARG *b)
5475
if (!a || !b || !a->is_same(b))
5477
if (a->left != &null_element && b->left != &null_element)
5479
if (!eq_tree(a->left,b->left))
5482
else if (a->left != &null_element || b->left != &null_element)
5484
if (a->right != &null_element && b->right != &null_element)
5486
if (!eq_tree(a->right,b->right))
5489
else if (a->right != &null_element || b->right != &null_element)
5491
if (a->next_key_part != b->next_key_part)
5493
if (!a->next_key_part != !b->next_key_part ||
5494
!eq_tree(a->next_key_part, b->next_key_part))
5502
SEL_ARG::insert(SEL_ARG *key)
5504
SEL_ARG *element, **par= NULL, *last_element= NULL;
5506
for (element= this; element != &null_element ; )
5508
last_element=element;
5509
if (key->cmp_min_to_min(element) > 0)
5511
par= &element->right; element= element->right;
5515
par = &element->left; element= element->left;
5519
key->parent=last_element;
5521
if (par == &last_element->left)
5523
key->next=last_element;
5524
if ((key->prev=last_element->prev))
5525
key->prev->next=key;
5526
last_element->prev=key;
5530
if ((key->next=last_element->next))
5531
key->next->prev=key;
5532
key->prev=last_element;
5533
last_element->next=key;
5535
key->left=key->right= &null_element;
5536
SEL_ARG *root=rb_insert(key); // rebalance tree
5537
root->use_count=this->use_count; // copy root info
5538
root->elements= this->elements+1;
5539
root->maybe_flag=this->maybe_flag;
5545
** Find best key with min <= given key
5546
** Because the call context this should never return 0 to get_range
5550
SEL_ARG::find_range(SEL_ARG *key)
5552
SEL_ARG *element=this,*found=0;
5556
if (element == &null_element)
5558
int cmp=element->cmp_min_to_min(key);
5564
element=element->right;
5567
element=element->left;
5573
Remove a element from the tree
5577
key Key that is to be deleted from tree (this)
5580
This also frees all sub trees that is used by the element
5583
root of new tree (with key deleted)
5587
SEL_ARG::tree_delete(SEL_ARG *key)
5589
enum leaf_color remove_color;
5590
SEL_ARG *root,*nod,**par,*fix_par;
5591
DBUG_ENTER("tree_delete");
5596
/* Unlink from list */
5598
key->prev->next=key->next;
5600
key->next->prev=key->prev;
5601
key->increment_use_count(-1);
5605
par=key->parent_ptr();
5607
if (key->left == &null_element)
5609
*par=nod=key->right;
5610
fix_par=key->parent;
5611
if (nod != &null_element)
5612
nod->parent=fix_par;
5613
remove_color= key->color;
5615
else if (key->right == &null_element)
5617
*par= nod=key->left;
5618
nod->parent=fix_par=key->parent;
5619
remove_color= key->color;
5623
SEL_ARG *tmp=key->next; // next bigger key (exist!)
5624
nod= *tmp->parent_ptr()= tmp->right; // unlink tmp from tree
5625
fix_par=tmp->parent;
5626
if (nod != &null_element)
5627
nod->parent=fix_par;
5628
remove_color= tmp->color;
5630
tmp->parent=key->parent; // Move node in place of key
5631
(tmp->left=key->left)->parent=tmp;
5632
if ((tmp->right=key->right) != &null_element)
5633
tmp->right->parent=tmp;
5634
tmp->color=key->color;
5636
if (fix_par == key) // key->right == key->next
5637
fix_par=tmp; // new parent of nod
5640
if (root == &null_element)
5641
DBUG_RETURN(0); // Maybe root later
5642
if (remove_color == BLACK)
5643
root=rb_delete_fixup(root,nod,fix_par);
5644
test_rb_tree(root,root->parent);
5646
root->use_count=this->use_count; // Fix root counters
5647
root->elements=this->elements-1;
5648
root->maybe_flag=this->maybe_flag;
5653
/* Functions to fix up the tree after insert and delete */
5655
static void left_rotate(SEL_ARG **root,SEL_ARG *leaf)
5657
SEL_ARG *y=leaf->right;
5658
leaf->right=y->left;
5659
if (y->left != &null_element)
5660
y->left->parent=leaf;
5661
if (!(y->parent=leaf->parent))
5664
*leaf->parent_ptr()=y;
5669
static void right_rotate(SEL_ARG **root,SEL_ARG *leaf)
5671
SEL_ARG *y=leaf->left;
5672
leaf->left=y->right;
5673
if (y->right != &null_element)
5674
y->right->parent=leaf;
5675
if (!(y->parent=leaf->parent))
5678
*leaf->parent_ptr()=y;
5685
SEL_ARG::rb_insert(SEL_ARG *leaf)
5687
SEL_ARG *y,*par,*par2,*root;
5688
root= this; root->parent= 0;
5691
while (leaf != root && (par= leaf->parent)->color == RED)
5692
{ // This can't be root or 1 level under
5693
if (par == (par2= leaf->parent->parent)->left)
5696
if (y->color == RED)
5701
leaf->color=RED; /* And the loop continues */
5705
if (leaf == par->right)
5707
left_rotate(&root,leaf->parent);
5708
par=leaf; /* leaf is now parent to old leaf */
5712
right_rotate(&root,par2);
5719
if (y->color == RED)
5724
leaf->color=RED; /* And the loop continues */
5728
if (leaf == par->left)
5730
right_rotate(&root,par);
5735
left_rotate(&root,par2);
5741
test_rb_tree(root,root->parent);
5746
SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key,SEL_ARG *par)
5752
while (x != root && x->color == SEL_ARG::BLACK)
5757
if (w->color == SEL_ARG::RED)
5759
w->color=SEL_ARG::BLACK;
5760
par->color=SEL_ARG::RED;
5761
left_rotate(&root,par);
5764
if (w->left->color == SEL_ARG::BLACK && w->right->color == SEL_ARG::BLACK)
5766
w->color=SEL_ARG::RED;
5771
if (w->right->color == SEL_ARG::BLACK)
5773
w->left->color=SEL_ARG::BLACK;
5774
w->color=SEL_ARG::RED;
5775
right_rotate(&root,w);
5778
w->color=par->color;
5779
par->color=SEL_ARG::BLACK;
5780
w->right->color=SEL_ARG::BLACK;
5781
left_rotate(&root,par);
5789
if (w->color == SEL_ARG::RED)
5791
w->color=SEL_ARG::BLACK;
5792
par->color=SEL_ARG::RED;
5793
right_rotate(&root,par);
5796
if (w->right->color == SEL_ARG::BLACK && w->left->color == SEL_ARG::BLACK)
5798
w->color=SEL_ARG::RED;
5803
if (w->left->color == SEL_ARG::BLACK)
5805
w->right->color=SEL_ARG::BLACK;
5806
w->color=SEL_ARG::RED;
5807
left_rotate(&root,w);
5810
w->color=par->color;
5811
par->color=SEL_ARG::BLACK;
5812
w->left->color=SEL_ARG::BLACK;
5813
right_rotate(&root,par);
5820
x->color=SEL_ARG::BLACK;
5825
/* Test that the properties for a red-black tree hold */
5828
int test_rb_tree(SEL_ARG *element,SEL_ARG *parent)
5830
int count_l,count_r;
5832
if (element == &null_element)
5833
return 0; // Found end of tree
5834
if (element->parent != parent)
5836
sql_print_error("Wrong tree: Parent doesn't point at parent");
5839
if (element->color == SEL_ARG::RED &&
5840
(element->left->color == SEL_ARG::RED ||
5841
element->right->color == SEL_ARG::RED))
5843
sql_print_error("Wrong tree: Found two red in a row");
5846
if (element->left == element->right && element->left != &null_element)
5848
sql_print_error("Wrong tree: Found right == left");
5851
count_l=test_rb_tree(element->left,element);
5852
count_r=test_rb_tree(element->right,element);
5853
if (count_l >= 0 && count_r >= 0)
5855
if (count_l == count_r)
5856
return count_l+(element->color == SEL_ARG::BLACK);
5857
sql_print_error("Wrong tree: Incorrect black-count: %d - %d",
5860
return -1; // Error, no more warnings
5865
Count how many times SEL_ARG graph "root" refers to its part "key"
5868
count_key_part_usage()
5869
root An RB-Root node in a SEL_ARG graph.
5870
key Another RB-Root node in that SEL_ARG graph.
5873
The passed "root" node may refer to "key" node via root->next_key_part,
5876
This function counts how many times the node "key" is referred (via
5877
SEL_ARG::next_key_part) by
5878
- intervals of RB-tree pointed by "root",
5879
- intervals of RB-trees that are pointed by SEL_ARG::next_key_part from
5880
intervals of RB-tree pointed by "root",
5883
Here is an example (horizontal links represent next_key_part pointers,
5884
vertical links - next/prev prev pointers):
5887
|root|-----------------+
5891
+----+ +---+ $ | +---+ Here the return value
5892
| |- ... -| |---$-+--+->|key| will be 4.
5893
+----+ +---+ $ | | +---+
5898
| |---| |---------+ |
5905
Number of links to "key" from nodes reachable from "root".
5908
static ulong count_key_part_usage(SEL_ARG *root, SEL_ARG *key)
5911
for (root=root->first(); root ; root=root->next)
5913
if (root->next_key_part)
5915
if (root->next_key_part == key)
5917
if (root->next_key_part->part < key->part)
5918
count+=count_key_part_usage(root->next_key_part,key);
5926
Check if SEL_ARG::use_count value is correct
5929
SEL_ARG::test_use_count()
5930
root The root node of the SEL_ARG graph (an RB-tree root node that
5931
has the least value of sel_arg->part in the entire graph, and
5932
thus is the "origin" of the graph)
5935
Check if SEL_ARG::use_count value is correct. See the definition of
5936
use_count for what is "correct".
5939
void SEL_ARG::test_use_count(SEL_ARG *root)
5942
if (this == root && use_count != 1)
5944
sql_print_information("Use_count: Wrong count %lu for root",use_count);
5947
if (this->type != SEL_ARG::KEY_RANGE)
5949
for (SEL_ARG *pos=first(); pos ; pos=pos->next)
5952
if (pos->next_key_part)
5954
ulong count=count_key_part_usage(root,pos->next_key_part);
5955
if (count > pos->next_key_part->use_count)
5957
sql_print_information("Use_count: Wrong count for key at 0x%lx, %lu "
5958
"should be %lu", (long unsigned int)pos,
5959
pos->next_key_part->use_count, count);
5962
pos->next_key_part->test_use_count(root);
5965
if (e_count != elements)
5966
sql_print_warning("Wrong use count: %u (should be %u) for tree at 0x%lx",
5967
e_count, elements, (long unsigned int) this);
5972
/****************************************************************************
5973
MRR Range Sequence Interface implementation that walks a SEL_ARG* tree.
5974
****************************************************************************/
5976
/* MRR range sequence, SEL_ARG* implementation: stack entry */
5977
typedef struct st_range_seq_entry
5980
Pointers in min and max keys. They point to right-after-end of key
5981
images. The 0-th entry has these pointing to key tuple start.
5983
uchar *min_key, *max_key;
5986
Flags, for {keypart0, keypart1, ... this_keypart} subtuple.
5987
min_key_flag may have NULL_RANGE set.
5989
uint min_key_flag, max_key_flag;
5991
/* Number of key parts */
5992
uint min_key_parts, max_key_parts;
5998
MRR range sequence, SEL_ARG* implementation: SEL_ARG graph traversal context
6000
typedef struct st_sel_arg_range_seq
6002
uint keyno; /* index of used tree in SEL_TREE structure */
6003
uint real_keyno; /* Number of the index in tables */
6005
SEL_ARG *start; /* Root node of the traversed SEL_ARG* graph */
6007
RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];
6008
int i; /* Index of last used element in the above array */
6010
bool at_start; /* TRUE <=> The traversal has just started */
6011
} SEL_ARG_RANGE_SEQ;
6015
Range sequence interface, SEL_ARG* implementation: Initialize the traversal
6019
init_params SEL_ARG tree traversal context
6020
n_ranges [ignored] The number of ranges obtained
6021
flags [ignored] HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
6027
range_seq_t sel_arg_range_seq_init(void *init_param, uint n_ranges, uint flags)
6029
SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)init_param;
6030
seq->at_start= TRUE;
6031
seq->stack[0].key_tree= NULL;
6032
seq->stack[0].min_key= seq->param->min_key;
6033
seq->stack[0].min_key_flag= 0;
6034
seq->stack[0].min_key_parts= 0;
6036
seq->stack[0].max_key= seq->param->max_key;
6037
seq->stack[0].max_key_flag= 0;
6038
seq->stack[0].max_key_parts= 0;
6044
static void step_down_to(SEL_ARG_RANGE_SEQ *arg, SEL_ARG *key_tree)
6046
RANGE_SEQ_ENTRY *cur= &arg->stack[arg->i+1];
6047
RANGE_SEQ_ENTRY *prev= &arg->stack[arg->i];
6049
cur->key_tree= key_tree;
6050
cur->min_key= prev->min_key;
6051
cur->max_key= prev->max_key;
6052
cur->min_key_parts= prev->min_key_parts;
6053
cur->max_key_parts= prev->max_key_parts;
6055
uint16 stor_length= arg->param->key[arg->keyno][key_tree->part].store_length;
6056
cur->min_key_parts += key_tree->store_min(stor_length, &cur->min_key,
6057
prev->min_key_flag);
6058
cur->max_key_parts += key_tree->store_max(stor_length, &cur->max_key,
6059
prev->max_key_flag);
6061
cur->min_key_flag= prev->min_key_flag | key_tree->min_flag;
6062
cur->max_key_flag= prev->max_key_flag | key_tree->max_flag;
6064
if (key_tree->is_null_interval())
6065
cur->min_key_flag |= NULL_RANGE;
6071
Range sequence interface, SEL_ARG* implementation: get the next interval
6074
sel_arg_range_seq_next()
6075
rseq Value returned from sel_arg_range_seq_init
6076
range OUT Store information about the range here
6079
This is "get_next" function for Range sequence interface implementation
6083
The traversal also updates those param members:
6090
1 No more ranges in the sequence
6093
//psergey-merge-todo: support check_quick_keys:max_keypart
6094
uint sel_arg_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
6097
SEL_ARG_RANGE_SEQ *seq= (SEL_ARG_RANGE_SEQ*)rseq;
6100
key_tree= seq->start;
6101
seq->at_start= FALSE;
6102
goto walk_up_n_right;
6105
key_tree= seq->stack[seq->i].key_tree;
6106
/* Ok, we're at some "full tuple" position in the tree */
6108
/* Step down if we can */
6109
if (key_tree->next && key_tree->next != &null_element)
6111
//step down; (update the tuple, we'll step right and stay there)
6113
step_down_to(seq, key_tree->next);
6114
key_tree= key_tree->next;
6115
seq->param->is_ror_scan= FALSE;
6116
goto walk_right_n_up;
6119
/* Ok, can't step down, walk left until we can step down */
6122
if (seq->i == 1) // can't step left
6126
key_tree= seq->stack[seq->i].key_tree;
6128
/* Step down if we can */
6129
if (key_tree->next && key_tree->next != &null_element)
6131
// Step down; update the tuple
6133
step_down_to(seq, key_tree->next);
6134
key_tree= key_tree->next;
6140
Ok, we've stepped down from the path to previous tuple.
6141
Walk right-up while we can
6144
while (key_tree->next_key_part && key_tree->next_key_part != &null_element &&
6145
key_tree->next_key_part->part == key_tree->part + 1 &&
6146
key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
6149
RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
6150
uint min_key_length= cur->min_key - seq->param->min_key;
6151
uint max_key_length= cur->max_key - seq->param->max_key;
6152
uint len= cur->min_key - cur[-1].min_key;
6153
if (!(min_key_length == max_key_length &&
6154
!memcmp(cur[-1].min_key, cur[-1].max_key, len) &&
6155
!key_tree->min_flag && !key_tree->max_flag))
6157
seq->param->is_ror_scan= FALSE;
6158
if (!key_tree->min_flag)
6159
cur->min_key_parts +=
6160
key_tree->next_key_part->store_min_key(seq->param->key[seq->keyno],
6162
&cur->min_key_flag);
6163
if (!key_tree->max_flag)
6164
cur->max_key_parts +=
6165
key_tree->next_key_part->store_max_key(seq->param->key[seq->keyno],
6167
&cur->max_key_flag);
6173
Ok, current atomic interval is in form "t.field=const" and there is
6174
next_key_part interval. Step right, and walk up from there.
6176
key_tree= key_tree->next_key_part;
6179
while (key_tree->prev && key_tree->prev != &null_element)
6182
key_tree= key_tree->prev;
6184
step_down_to(seq, key_tree);
6187
/* Ok got a tuple */
6188
RANGE_SEQ_ENTRY *cur= &seq->stack[seq->i];
6190
range->ptr= (char*)(int)(key_tree->part);
6192
range->range_flag= cur->min_key_flag | cur->max_key_flag;
6194
range->start_key.key= seq->param->min_key;
6195
range->start_key.length= cur->min_key - seq->param->min_key;
6196
range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts);
6197
range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
6200
range->end_key.key= seq->param->max_key;
6201
range->end_key.length= cur->max_key - seq->param->max_key;
6202
range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
6204
range->end_key.keypart_map= make_prev_keypart_map(cur->max_key_parts);
6206
if (!(cur->min_key_flag & ~NULL_RANGE) && !cur->max_key_flag &&
6207
(uint)key_tree->part+1 == seq->param->table->key_info[seq->real_keyno].key_parts &&
6208
(seq->param->table->key_info[seq->real_keyno].flags & (HA_NOSAME | HA_END_SPACE_KEY)) ==
6210
range->start_key.length == range->end_key.length &&
6211
!memcmp(seq->param->min_key,seq->param->max_key,range->start_key.length))
6212
range->range_flag= UNIQUE_RANGE | (cur->min_key_flag & NULL_RANGE);
6214
if (seq->param->is_ror_scan)
6217
If we get here, the condition on the key was converted to form
6218
"(keyXpart1 = c1) AND ... AND (keyXpart{key_tree->part - 1} = cN) AND
6219
somecond(keyXpart{key_tree->part})"
6221
somecond is "keyXpart{key_tree->part} = const" and
6222
uncovered "tail" of KeyX parts is either empty or is identical to
6223
first members of clustered primary key.
6225
if (!(!(cur->min_key_flag & ~NULL_RANGE) && !cur->max_key_flag &&
6226
(range->start_key.length == range->end_key.length) &&
6227
!memcmp(range->start_key.key, range->end_key.key, range->start_key.length) &&
6228
is_key_scan_ror(seq->param, seq->real_keyno, key_tree->part + 1)))
6229
seq->param->is_ror_scan= FALSE;
6232
seq->param->range_count++;
6233
seq->param->max_key_part=max(seq->param->max_key_part,key_tree->part);
6239
Calculate cost and E(#rows) for a given index and intervals tree
6242
check_quick_select()
6243
param Parameter from test_quick_select
6244
idx Number of index to use in PARAM::key SEL_TREE::key
6245
index_only TRUE - assume only index tuples will be accessed
6246
FALSE - assume full table rows will be read
6247
tree Transformed selection condition, tree->key[idx] holds
6248
the intervals for the given index.
6249
update_tbl_stats TRUE <=> update table->quick_* with information
6250
about range scan we've evaluated.
6251
mrr_flags INOUT MRR access flags
6255
param->is_ror_scan is set to reflect if the key scan is a ROR (see
6256
is_key_scan_ror function for more info)
6257
param->table->quick_*, param->range_count (and maybe others) are
6258
updated with data of given key scan, see quick_range_seq_next for details.
6261
Estimate # of records to be retrieved.
6262
HA_POS_ERROR if estimate calculation failed due to table handler problems.
6266
ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
6267
SEL_ARG *tree, bool update_tbl_stats,
6268
uint *mrr_flags, uint *bufsize, COST_VECT *cost)
6270
SEL_ARG_RANGE_SEQ seq;
6271
RANGE_SEQ_IF seq_if = {sel_arg_range_seq_init, sel_arg_range_seq_next};
6272
handler *file= param->table->file;
6274
uint keynr= param->real_keynr[idx];
6275
DBUG_ENTER("check_quick_select");
6277
/* Handle cases when we don't have a valid non-empty list of range */
6279
DBUG_RETURN(HA_POS_ERROR);
6280
if (tree->type == SEL_ARG::IMPOSSIBLE)
6282
if (tree->type != SEL_ARG::KEY_RANGE || tree->part != 0)
6283
DBUG_RETURN(HA_POS_ERROR);
6286
seq.real_keyno= keynr;
6290
param->range_count=0;
6291
param->max_key_part=0;
6293
param->is_ror_scan= TRUE;
6294
if (file->index_flags(keynr, 0, TRUE) & HA_KEY_SCAN_NOT_ROR)
6295
param->is_ror_scan= FALSE;
6297
*mrr_flags= param->force_default_mrr? HA_MRR_USE_DEFAULT_IMPL: 0;
6298
*mrr_flags|= HA_MRR_NO_ASSOCIATION;
6300
bool pk_is_clustered= file->primary_key_is_clustered();
6302
(file->index_flags(keynr, param->max_key_part, 1) & HA_KEYREAD_ONLY) &&
6303
!(pk_is_clustered && keynr == param->table->s->primary_key))
6304
*mrr_flags |= HA_MRR_INDEX_ONLY;
6306
if (current_thd->lex->sql_command != SQLCOM_SELECT)
6307
*mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
6309
*bufsize= param->thd->variables.read_rnd_buff_size;
6310
rows= file->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
6311
bufsize, mrr_flags, cost);
6312
if (rows != HA_POS_ERROR)
6314
param->table->quick_rows[keynr]=rows;
6315
if (update_tbl_stats)
6317
param->table->quick_keys.set_bit(keynr);
6318
param->table->quick_key_parts[keynr]=param->max_key_part+1;
6319
param->table->quick_n_ranges[keynr]= param->range_count;
6320
param->table->quick_condition_rows=
6321
min(param->table->quick_condition_rows, rows);
6324
/* Figure out if the key scan is ROR (returns rows in ROWID order) or not */
6325
enum ha_key_alg key_alg= param->table->key_info[seq.real_keyno].algorithm;
6326
if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
6329
All scans are non-ROR scans for those index types.
6330
TODO: Don't have this logic here, make table engines return
6331
appropriate flags instead.
6333
param->is_ror_scan= FALSE;
6337
/* Clustered PK scan is always a ROR scan (TODO: same as above) */
6338
if (param->table->s->primary_key == keynr && pk_is_clustered)
6339
param->is_ror_scan= TRUE;
6342
DBUG_PRINT("exit", ("Records: %lu", (ulong) rows));
6343
DBUG_RETURN(rows); //psergey-merge:todo: maintain first_null_comp.
6348
Check if key scan on given index with equality conditions on first n key
6349
parts is a ROR scan.
6353
param Parameter from test_quick_select
6354
keynr Number of key in the table. The key must not be a clustered
6356
nparts Number of first key parts for which equality conditions
6360
ROR (Rowid Ordered Retrieval) key scan is a key scan that produces
6361
ordered sequence of rowids (ha_xxx::cmp_ref is the comparison function)
6363
This function is needed to handle a practically-important special case:
6364
an index scan is a ROR scan if it is done using a condition in form
6366
"key1_1=c_1 AND ... AND key1_n=c_n"
6368
where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])
6370
and the table has a clustered Primary Key defined as
6371
PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k)
6373
i.e. the first key parts of it are identical to uncovered parts ot the
6374
key being scanned. This function assumes that the index flags do not
6375
include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).
6377
Check (1) is made in quick_range_seq_next()
6380
TRUE The scan is ROR-scan
6384
static bool is_key_scan_ror(PARAM *param, uint keynr, uint8 nparts)
6386
KEY *table_key= param->table->key_info + keynr;
6387
KEY_PART_INFO *key_part= table_key->key_part + nparts;
6388
KEY_PART_INFO *key_part_end= (table_key->key_part +
6389
table_key->key_parts);
6392
for (KEY_PART_INFO *kp= table_key->key_part; kp < key_part; kp++)
6394
uint16 fieldnr= param->table->key_info[keynr].
6395
key_part[kp - table_key->key_part].fieldnr - 1;
6396
if (param->table->field[fieldnr]->key_length() != kp->length)
6400
if (key_part == key_part_end)
6403
key_part= table_key->key_part + nparts;
6404
pk_number= param->table->s->primary_key;
6405
if (!param->table->file->primary_key_is_clustered() || pk_number == MAX_KEY)
6408
KEY_PART_INFO *pk_part= param->table->key_info[pk_number].key_part;
6409
KEY_PART_INFO *pk_part_end= pk_part +
6410
param->table->key_info[pk_number].key_parts;
6411
for (;(key_part!=key_part_end) && (pk_part != pk_part_end);
6412
++key_part, ++pk_part)
6414
if ((key_part->field != pk_part->field) ||
6415
(key_part->length != pk_part->length))
6418
return (key_part == key_part_end);
6423
Create a QUICK_RANGE_SELECT from given key and SEL_ARG tree for that key.
6428
idx Index of used key in param->key.
6429
key_tree SEL_ARG tree for the used key
6430
mrr_flags MRR parameter for quick select
6431
mrr_buf_size MRR parameter for quick select
6432
parent_alloc If not NULL, use it to allocate memory for
6433
quick select data. Otherwise use quick->alloc.
6435
The caller must call QUICK_SELECT::init for returned quick select.
6437
CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
6438
deallocated when the returned quick select is deleted.
6442
otherwise created quick select
6445
QUICK_RANGE_SELECT *
6446
get_quick_select(PARAM *param,uint idx,SEL_ARG *key_tree, uint mrr_flags,
6447
uint mrr_buf_size, MEM_ROOT *parent_alloc)
6449
QUICK_RANGE_SELECT *quick;
6450
bool create_err= FALSE;
6451
DBUG_ENTER("get_quick_select");
6453
quick=new QUICK_RANGE_SELECT(param->thd, param->table,
6454
param->real_keynr[idx],
6455
test(parent_alloc), NULL, &create_err);
6460
get_quick_keys(param,quick,param->key[idx],key_tree,param->min_key,0,
6468
quick->mrr_flags= mrr_flags;
6469
quick->mrr_buf_size= mrr_buf_size;
6470
quick->key_parts=(KEY_PART*)
6471
memdup_root(parent_alloc? parent_alloc : &quick->alloc,
6472
(char*) param->key[idx],
6474
param->table->key_info[param->real_keynr[idx]].key_parts);
6482
** Fix this to get all possible sub_ranges
6485
get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
6486
SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
6487
uchar *max_key, uint max_key_flag)
6491
int min_part= key_tree->part-1, // # of keypart values in min_key buffer
6492
max_part= key_tree->part-1; // # of keypart values in max_key buffer
6494
if (key_tree->left != &null_element)
6496
if (get_quick_keys(param,quick,key,key_tree->left,
6497
min_key,min_key_flag, max_key, max_key_flag))
6500
uchar *tmp_min_key=min_key,*tmp_max_key=max_key;
6501
min_part+= key_tree->store_min(key[key_tree->part].store_length,
6502
&tmp_min_key,min_key_flag);
6503
max_part+= key_tree->store_max(key[key_tree->part].store_length,
6504
&tmp_max_key,max_key_flag);
6506
if (key_tree->next_key_part &&
6507
key_tree->next_key_part->part == key_tree->part+1 &&
6508
key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
6509
{ // const key as prefix
6510
if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
6511
memcmp(min_key, max_key, (uint)(tmp_max_key - max_key))==0 &&
6512
key_tree->min_flag==0 && key_tree->max_flag==0)
6514
if (get_quick_keys(param,quick,key,key_tree->next_key_part,
6515
tmp_min_key, min_key_flag | key_tree->min_flag,
6516
tmp_max_key, max_key_flag | key_tree->max_flag))
6518
goto end; // Ugly, but efficient
6521
uint tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
6523
min_part+= key_tree->next_key_part->store_min_key(key, &tmp_min_key,
6526
max_part+= key_tree->next_key_part->store_max_key(key, &tmp_max_key,
6528
flag=tmp_min_flag | tmp_max_flag;
6533
flag= key_tree->min_flag | key_tree->max_flag;
6537
Ensure that some part of min_key and max_key are used. If not,
6538
regard this as no lower/upper range
6541
if (tmp_min_key != param->min_key)
6542
flag&= ~NO_MIN_RANGE;
6544
flag|= NO_MIN_RANGE;
6545
if (tmp_max_key != param->max_key)
6546
flag&= ~NO_MAX_RANGE;
6548
flag|= NO_MAX_RANGE;
6552
uint length= (uint) (tmp_min_key - param->min_key);
6553
if (length == (uint) (tmp_max_key - param->max_key) &&
6554
!memcmp(param->min_key,param->max_key,length))
6556
KEY *table_key=quick->head->key_info+quick->index;
6558
if ((table_key->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME &&
6559
key->part == table_key->key_parts-1)
6561
if (!(table_key->flags & HA_NULL_PART_KEY) ||
6562
!null_part_in_key(key,
6564
(uint) (tmp_min_key - param->min_key)))
6565
flag|= UNIQUE_RANGE;
6572
/* Get range for retrieving rows in QUICK_SELECT::get_next */
6573
if (!(range= new QUICK_RANGE(param->min_key,
6574
(uint) (tmp_min_key - param->min_key),
6575
min_part >=0 ? make_keypart_map(min_part) : 0,
6577
(uint) (tmp_max_key - param->max_key),
6578
max_part >=0 ? make_keypart_map(max_part) : 0,
6580
return 1; // out of memory
6582
set_if_bigger(quick->max_used_key_length, range->min_length);
6583
set_if_bigger(quick->max_used_key_length, range->max_length);
6584
set_if_bigger(quick->used_key_parts, (uint) key_tree->part+1);
6585
if (insert_dynamic(&quick->ranges, (uchar*) &range))
6589
if (key_tree->right != &null_element)
6590
return get_quick_keys(param,quick,key,key_tree->right,
6591
min_key,min_key_flag,
6592
max_key,max_key_flag);
6597
Return 1 if there is only one range and this uses the whole primary key
6600
bool QUICK_RANGE_SELECT::unique_key_range()
6602
if (ranges.elements == 1)
6604
QUICK_RANGE *tmp= *((QUICK_RANGE**)ranges.buffer);
6605
if ((tmp->flag & (EQ_RANGE | NULL_RANGE)) == EQ_RANGE)
6607
KEY *key=head->key_info+index;
6608
return ((key->flags & (HA_NOSAME | HA_END_SPACE_KEY)) == HA_NOSAME &&
6609
key->key_length == tmp->min_length);
6618
Return TRUE if any part of the key is NULL
6622
key_part Array of key parts (index description)
6623
key Key values tuple
6624
length Length of key values tuple in bytes.
6627
TRUE The tuple has at least one "keypartX is NULL"
6631
static bool null_part_in_key(KEY_PART *key_part, const uchar *key, uint length)
6633
for (const uchar *end=key+length ;
6635
key+= key_part++->store_length)
6637
if (key_part->null_bit && *key)
6644
bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
6646
return is_key_used(head, index, fields);
6649
bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const MY_BITMAP *fields)
6651
QUICK_RANGE_SELECT *quick;
6652
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6653
while ((quick= it++))
6655
if (is_key_used(head, quick->index, fields))
6661
bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
6663
QUICK_RANGE_SELECT *quick;
6664
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
6665
while ((quick= it++))
6667
if (is_key_used(head, quick->index, fields))
6673
bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
6675
QUICK_SELECT_I *quick;
6676
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
6677
while ((quick= it++))
6679
if (quick->is_keys_used(fields))
6687
Create quick select from ref/ref_or_null scan.
6690
get_quick_select_for_ref()
6692
table Table to access
6693
ref ref[_or_null] scan parameters
6694
records Estimate of number of records (needed only to construct
6697
This allocates things in a new memory root, as this may be called many
6698
times during a query.
6701
Quick select that retrieves the same rows as passed ref scan
6705
QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
6706
TABLE_REF *ref, ha_rows records)
6708
MEM_ROOT *old_root, *alloc;
6709
QUICK_RANGE_SELECT *quick;
6710
KEY *key_info = &table->key_info[ref->key];
6714
bool create_err= FALSE;
6717
old_root= thd->mem_root;
6718
/* The following call may change thd->mem_root */
6719
quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0, 0, &create_err);
6720
/* save mem_root set by QUICK_RANGE_SELECT constructor */
6721
alloc= thd->mem_root;
6723
return back default mem_root (thd->mem_root) changed by
6724
QUICK_RANGE_SELECT constructor
6726
thd->mem_root= old_root;
6728
if (!quick || create_err)
6729
return 0; /* no ranges found */
6732
quick->records= records;
6734
if ((cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error) ||
6735
!(range= new(alloc) QUICK_RANGE()))
6736
goto err; // out of memory
6738
range->min_key= range->max_key= ref->key_buff;
6739
range->min_length= range->max_length= ref->key_length;
6740
range->min_keypart_map= range->max_keypart_map=
6741
make_prev_keypart_map(ref->key_parts);
6742
range->flag= ((ref->key_length == key_info->key_length &&
6743
(key_info->flags & HA_END_SPACE_KEY) == 0) ? EQ_RANGE : 0);
6745
if (!(quick->key_parts=key_part=(KEY_PART *)
6746
alloc_root(&quick->alloc,sizeof(KEY_PART)*ref->key_parts)))
6749
for (part=0 ; part < ref->key_parts ;part++,key_part++)
6751
key_part->part=part;
6752
key_part->field= key_info->key_part[part].field;
6753
key_part->length= key_info->key_part[part].length;
6754
key_part->store_length= key_info->key_part[part].store_length;
6755
key_part->null_bit= key_info->key_part[part].null_bit;
6756
key_part->flag= (uint8) key_info->key_part[part].key_part_flag;
6758
if (insert_dynamic(&quick->ranges,(uchar*)&range))
6762
Add a NULL range if REF_OR_NULL optimization is used.
6764
if we have "WHERE A=2 OR A IS NULL" we created the (A=2) range above
6765
and have ref->null_ref_key set. Will create a new NULL range here.
6767
if (ref->null_ref_key)
6769
QUICK_RANGE *null_range;
6771
*ref->null_ref_key= 1; // Set null byte then create a range
6772
if (!(null_range= new (alloc)
6773
QUICK_RANGE(ref->key_buff, ref->key_length,
6774
make_prev_keypart_map(ref->key_parts),
6775
ref->key_buff, ref->key_length,
6776
make_prev_keypart_map(ref->key_parts), EQ_RANGE)))
6778
*ref->null_ref_key= 0; // Clear null byte
6779
if (insert_dynamic(&quick->ranges,(uchar*)&null_range))
6783
/* Call multi_range_read_info() to get the MRR flags and buffer size */
6784
quick->mrr_flags= HA_MRR_NO_ASSOCIATION |
6785
(table->key_read ? HA_MRR_INDEX_ONLY : 0);
6786
if (thd->lex->sql_command != SQLCOM_SELECT)
6787
quick->mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
6789
quick->mrr_buf_size= thd->variables.read_rnd_buff_size;
6790
if (table->file->multi_range_read_info(quick->index, 1, (uint)records,
6791
&quick->mrr_buf_size,
6792
&quick->mrr_flags, &cost))
6803
Perform key scans for all used indexes (except CPK), get rowids and merge
6804
them into an ordered non-recurrent sequence of rowids.
6806
The merge/duplicate removal is performed using Unique class. We put all
6807
rowids into Unique, get the sorted sequence and destroy the Unique.
6809
If table has a clustered primary key that covers all rows (TRUE for bdb
6810
and innodb currently) and one of the index_merge scans is a scan on PK,
6811
then rows that will be retrieved by PK scan are not put into Unique and
6812
primary key scan is not performed here, it is performed later separately.
6819
int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()
6821
List_iterator_fast<QUICK_RANGE_SELECT> cur_quick_it(quick_selects);
6822
QUICK_RANGE_SELECT* cur_quick;
6825
handler *file= head->file;
6826
DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::read_keys_and_merge");
6828
file->extra(HA_EXTRA_KEYREAD);
6829
head->prepare_for_position();
6831
cur_quick_it.rewind();
6832
cur_quick= cur_quick_it++;
6833
DBUG_ASSERT(cur_quick != 0);
6836
We reuse the same instance of handler so we need to call both init and
6839
if (cur_quick->init() || cur_quick->reset())
6842
unique= new Unique(refpos_order_cmp, (void *)file,
6844
thd->variables.sortbuff_size);
6849
while ((result= cur_quick->get_next()) == HA_ERR_END_OF_FILE)
6851
cur_quick->range_end();
6852
cur_quick= cur_quick_it++;
6856
if (cur_quick->file->inited != handler::NONE)
6857
cur_quick->file->ha_index_end();
6858
if (cur_quick->init() || cur_quick->reset())
6864
if (result != HA_ERR_END_OF_FILE)
6866
cur_quick->range_end();
6867
DBUG_RETURN(result);
6875
/* skip row if it will be retrieved by clustered PK scan */
6876
if (pk_quick_select && pk_quick_select->row_in_ranges())
6879
cur_quick->file->position(cur_quick->record);
6880
result= unique->unique_add((char*)cur_quick->file->ref);
6886
DBUG_PRINT("info", ("ok"));
6887
/* ok, all row ids are in Unique */
6888
result= unique->get(head);
6890
doing_pk_scan= FALSE;
6891
/* index_merge currently doesn't support "using index" at all */
6892
file->extra(HA_EXTRA_NO_KEYREAD);
6893
/* start table scan */
6894
init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1, 1);
6895
DBUG_RETURN(result);
6900
Get next row for index_merge.
6902
The rows are read from
6903
1. rowids stored in Unique.
6904
2. QUICK_RANGE_SELECT with clustered primary key (if any).
6905
The sets of rows retrieved in 1) and 2) are guaranteed to be disjoint.
6908
int QUICK_INDEX_MERGE_SELECT::get_next()
6911
DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::get_next");
6914
DBUG_RETURN(pk_quick_select->get_next());
6916
if ((result= read_record.read_record(&read_record)) == -1)
6918
result= HA_ERR_END_OF_FILE;
6919
end_read_record(&read_record);
6920
/* All rows from Unique have been retrieved, do a clustered PK scan */
6921
if (pk_quick_select)
6923
doing_pk_scan= TRUE;
6924
if ((result= pk_quick_select->init()) ||
6925
(result= pk_quick_select->reset()))
6926
DBUG_RETURN(result);
6927
DBUG_RETURN(pk_quick_select->get_next());
6931
DBUG_RETURN(result);
6936
Retrieve next record.
6938
QUICK_ROR_INTERSECT_SELECT::get_next()
6941
Invariant on enter/exit: all intersected selects have retrieved all index
6942
records with rowid <= some_rowid_val and no intersected select has
6943
retrieved any index records with rowid > some_rowid_val.
6944
We start fresh and loop until we have retrieved the same rowid in each of
6945
the key scans or we got an error.
6947
If a Clustered PK scan is present, it is used only to check if row
6948
satisfies its condition (and never used for row retrieval).
6952
other - Error code if any error occurred.
6955
int QUICK_ROR_INTERSECT_SELECT::get_next()
6957
List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
6958
QUICK_RANGE_SELECT* quick;
6960
uint last_rowid_count=0;
6961
DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::get_next");
6965
/* Get a rowid for first quick and save it as a 'candidate' */
6967
error= quick->get_next();
6970
while (!error && !cpk_quick->row_in_ranges())
6971
error= quick->get_next();
6976
quick->file->position(quick->record);
6977
memcpy(last_rowid, quick->file->ref, head->file->ref_length);
6978
last_rowid_count= 1;
6980
while (last_rowid_count < quick_selects.elements)
6982
if (!(quick= quick_it++))
6990
if ((error= quick->get_next()))
6992
quick->file->position(quick->record);
6993
cmp= head->file->cmp_ref(quick->file->ref, last_rowid);
6996
/* Ok, current select 'caught up' and returned ref >= cur_ref */
6999
/* Found a row with ref > cur_ref. Make it a new 'candidate' */
7002
while (!cpk_quick->row_in_ranges())
7004
if ((error= quick->get_next()))
7008
memcpy(last_rowid, quick->file->ref, head->file->ref_length);
7009
last_rowid_count= 1;
7013
/* current 'candidate' row confirmed by this select */
7018
/* We get here if we got the same row ref in all scans. */
7019
if (need_to_fetch_row)
7020
error= head->file->rnd_pos(head->record[0], last_rowid);
7021
} while (error == HA_ERR_RECORD_DELETED);
7027
Retrieve next record.
7029
QUICK_ROR_UNION_SELECT::get_next()
7032
Enter/exit invariant:
7033
For each quick select in the queue a {key,rowid} tuple has been
7034
retrieved but the corresponding row hasn't been passed to output.
7038
other - Error code if any error occurred.
7041
int QUICK_ROR_UNION_SELECT::get_next()
7044
QUICK_SELECT_I *quick;
7046
DBUG_ENTER("QUICK_ROR_UNION_SELECT::get_next");
7052
if (!queue.elements)
7053
DBUG_RETURN(HA_ERR_END_OF_FILE);
7054
/* Ok, we have a queue with >= 1 scans */
7056
quick= (QUICK_SELECT_I*)queue_top(&queue);
7057
memcpy(cur_rowid, quick->last_rowid, rowid_length);
7059
/* put into queue rowid from the same stream as top element */
7060
if ((error= quick->get_next()))
7062
if (error != HA_ERR_END_OF_FILE)
7064
queue_remove(&queue, 0);
7068
quick->save_last_pos();
7069
queue_replaced(&queue);
7072
if (!have_prev_rowid)
7074
/* No rows have been returned yet */
7076
have_prev_rowid= TRUE;
7079
dup_row= !head->file->cmp_ref(cur_rowid, prev_rowid);
7083
cur_rowid= prev_rowid;
7086
error= head->file->rnd_pos(quick->record, prev_rowid);
7087
} while (error == HA_ERR_RECORD_DELETED);
7092
int QUICK_RANGE_SELECT::reset()
7097
HANDLER_BUFFER empty_buf;
7098
DBUG_ENTER("QUICK_RANGE_SELECT::reset");
7100
cur_range= (QUICK_RANGE**) ranges.buffer;
7102
if (file->inited == handler::NONE && (error= file->ha_index_init(index,1)))
7105
/* Allocate buffer if we need one but haven't allocated it yet */
7106
if (mrr_buf_size && !mrr_buf_desc)
7108
buf_size= mrr_buf_size;
7109
while (buf_size && !my_multi_malloc(MYF(MY_WME),
7110
&mrr_buf_desc, sizeof(*mrr_buf_desc),
7111
&mrange_buff, buf_size,
7114
/* Try to shrink the buffers until both are 0. */
7118
DBUG_RETURN(HA_ERR_OUT_OF_MEM);
7120
/* Initialize the handler buffer. */
7121
mrr_buf_desc->buffer= mrange_buff;
7122
mrr_buf_desc->buffer_end= mrange_buff + buf_size;
7123
mrr_buf_desc->end_of_used_area= mrange_buff;
7127
empty_buf.buffer= empty_buf.buffer_end= empty_buf.end_of_used_area= NULL;
7130
mrr_flags |= HA_MRR_SORTED;
7131
RANGE_SEQ_IF seq_funcs= {quick_range_seq_init, quick_range_seq_next};
7132
error= file->multi_range_read_init(&seq_funcs, (void*)this, ranges.elements,
7133
mrr_flags, mrr_buf_desc? mrr_buf_desc:
7140
Range sequence interface implementation for array<QUICK_RANGE>: initialize
7143
quick_range_seq_init()
7144
init_param Caller-opaque paramenter: QUICK_RANGE_SELECT* pointer
7145
n_ranges Number of ranges in the sequence (ignored)
7146
flags MRR flags (currently not used)
7149
Opaque value to be passed to quick_range_seq_next
7152
range_seq_t quick_range_seq_init(void *init_param, uint n_ranges, uint flags)
7154
QUICK_RANGE_SELECT *quick= (QUICK_RANGE_SELECT*)init_param;
7155
quick->qr_traversal_ctx.first= (QUICK_RANGE**)quick->ranges.buffer;
7156
quick->qr_traversal_ctx.cur= (QUICK_RANGE**)quick->ranges.buffer;
7157
quick->qr_traversal_ctx.last= quick->qr_traversal_ctx.cur +
7158
quick->ranges.elements;
7159
return &quick->qr_traversal_ctx;
7164
Range sequence interface implementation for array<QUICK_RANGE>: get next
7167
quick_range_seq_next()
7168
rseq Value returned from quick_range_seq_init
7169
range OUT Store information about the range here
7173
1 No more ranges in the sequence
7176
uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
7178
QUICK_RANGE_SEQ_CTX *ctx= (QUICK_RANGE_SEQ_CTX*)rseq;
7180
if (ctx->cur == ctx->last)
7181
return 1; /* no more ranges */
7183
QUICK_RANGE *cur= *(ctx->cur);
7184
key_range *start_key= &range->start_key;
7185
key_range *end_key= &range->end_key;
7187
start_key->key= cur->min_key;
7188
start_key->length= cur->min_length;
7189
start_key->keypart_map= cur->min_keypart_map;
7190
start_key->flag= ((cur->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
7191
(cur->flag & EQ_RANGE) ?
7192
HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
7193
end_key->key= cur->max_key;
7194
end_key->length= cur->max_length;
7195
end_key->keypart_map= cur->max_keypart_map;
7197
We use HA_READ_AFTER_KEY here because if we are reading on a key
7198
prefix. We want to find all keys with this prefix.
7200
end_key->flag= (cur->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
7202
range->range_flag= cur->flag;
7209
MRR range sequence interface: array<QUICK_RANGE> impl: utility func for NDB
7212
mrr_persistent_flag_storage()
7213
seq Range sequence being traversed
7217
MRR/NDB implementation needs to store some bits for each range. This
7218
function returns a reference to the "range_flag" associated with the
7221
This function should be removed when we get a proper MRR/NDB
7225
Reference to range_flag associated with range number #idx
7228
uint16 &mrr_persistent_flag_storage(range_seq_t seq, uint idx)
7230
QUICK_RANGE_SEQ_CTX *ctx= (QUICK_RANGE_SEQ_CTX*)seq;
7231
return ctx->first[idx]->flag;
7236
MRR range sequence interface: array<QUICK_RANGE> impl: utility func for NDB
7239
mrr_get_ptr_by_idx()
7240
seq Range sequence bening traversed
7241
idx Number of the range
7244
An extension of MRR range sequence interface needed by NDB: return the
7245
data associated with the given range.
7247
A proper MRR interface implementer is supposed to store and return
7248
range-associated data. NDB stores number of the range instead. So this
7249
is a helper function that translates range number to range associated
7252
This function does nothing, as currrently there is only one user of the
7253
MRR interface - the quick range select code, and this user doesn't need
7254
to use range-associated data.
7257
Reference to range-associated data
7260
char* &mrr_get_ptr_by_idx(range_seq_t seq, uint idx)
7268
Get next possible record using quick-struct.
7271
QUICK_RANGE_SELECT::get_next()
7274
Record is read into table->record[0]
7278
HA_ERR_END_OF_FILE No (more) rows in range
7282
int QUICK_RANGE_SELECT::get_next()
7285
DBUG_ENTER("QUICK_RANGE_SELECT::get_next");
7286
if (in_ror_merged_scan)
7289
We don't need to signal the bitmap change as the bitmap is always the
7290
same for this head->file
7292
head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap);
7295
int result= file->multi_range_read_next(&dummy);
7297
if (in_ror_merged_scan)
7299
/* Restore bitmaps set on entry */
7300
head->column_bitmaps_set_no_signal(save_read_set, save_write_set);
7302
DBUG_RETURN(result);
7307
Get the next record with a different prefix.
7310
QUICK_RANGE_SELECT::get_next_prefix()
7311
prefix_length length of cur_prefix
7312
cur_prefix prefix of a key to be searched for
7315
Each subsequent call to the method retrieves the first record that has a
7316
prefix with length prefix_length different from cur_prefix, such that the
7317
record with the new prefix is within the ranges described by
7318
this->ranges. The record found is stored into the buffer pointed by
7320
The method is useful for GROUP-BY queries with range conditions to
7321
discover the prefix of the next group that satisfies the range conditions.
7324
This method is a modified copy of QUICK_RANGE_SELECT::get_next(), so both
7325
methods should be unified into a more general one to reduce code
7330
HA_ERR_END_OF_FILE if returned all keys
7331
other if some error occurred
7334
int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length,
7335
key_part_map keypart_map,
7338
DBUG_ENTER("QUICK_RANGE_SELECT::get_next_prefix");
7343
key_range start_key, end_key;
7346
/* Read the next record in the same range with prefix after cur_prefix. */
7347
DBUG_ASSERT(cur_prefix != 0);
7348
result= file->index_read_map(record, cur_prefix, keypart_map,
7350
if (result || (file->compare_key(file->end_range) <= 0))
7351
DBUG_RETURN(result);
7354
uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
7357
/* Ranges have already been used up before. None is left for read. */
7359
DBUG_RETURN(HA_ERR_END_OF_FILE);
7361
last_range= *(cur_range++);
7363
start_key.key= (const uchar*) last_range->min_key;
7364
start_key.length= min(last_range->min_length, prefix_length);
7365
start_key.keypart_map= last_range->min_keypart_map & keypart_map;
7366
start_key.flag= ((last_range->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
7367
(last_range->flag & EQ_RANGE) ?
7368
HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
7369
end_key.key= (const uchar*) last_range->max_key;
7370
end_key.length= min(last_range->max_length, prefix_length);
7371
end_key.keypart_map= last_range->max_keypart_map & keypart_map;
7373
We use READ_AFTER_KEY here because if we are reading on a key
7374
prefix we want to find all keys with this prefix
7376
end_key.flag= (last_range->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
7379
result= file->read_range_first(last_range->min_keypart_map ? &start_key : 0,
7380
last_range->max_keypart_map ? &end_key : 0,
7381
test(last_range->flag & EQ_RANGE),
7383
if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
7384
last_range= 0; // Stop searching
7386
if (result != HA_ERR_END_OF_FILE)
7387
DBUG_RETURN(result);
7388
last_range= 0; // No matching rows; go to next range
7394
Check if current row will be retrieved by this QUICK_RANGE_SELECT
7397
It is assumed that currently a scan is being done on another index
7398
which reads all necessary parts of the index that is scanned by this
7400
The implementation does a binary search on sorted array of disjoint
7401
ranges, without taking size of range into account.
7403
This function is used to filter out clustered PK scan rows in
7404
index_merge quick select.
7407
TRUE if current row will be retrieved by this quick select
7411
bool QUICK_RANGE_SELECT::row_in_ranges()
7415
uint max= ranges.elements - 1;
7416
uint mid= (max + min)/2;
7420
if (cmp_next(*(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid)))
7422
/* current row value > mid->max */
7427
mid= (min + max) / 2;
7429
res= *(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid);
7430
return (!cmp_next(res) && !cmp_prev(res));
7434
This is a hack: we inherit from QUICK_SELECT so that we can use the
7435
get_next() interface, but we have to hold a pointer to the original
7436
QUICK_SELECT because its data are used all over the place. What
7437
should be done is to factor out the data that is needed into a base
7438
class (QUICK_SELECT), and then have two subclasses (_ASC and _DESC)
7439
which handle the ranges and implement the get_next() function. But
7440
for now, this seems to work right at least.
7443
QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
7444
uint used_key_parts_arg,
7446
:QUICK_RANGE_SELECT(*q), rev_it(rev_ranges)
7450
QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
7451
QUICK_RANGE **end_range= pr + ranges.elements;
7452
for (; pr!=end_range; pr++)
7453
rev_ranges.push_front(*pr);
7455
/* Remove EQ_RANGE flag for keys that are not using the full key */
7456
for (r = rev_it++; r; r = rev_it++)
7458
if ((r->flag & EQ_RANGE) &&
7459
head->key_info[index].key_length != r->max_length)
7460
r->flag&= ~EQ_RANGE;
7463
q->dont_free=1; // Don't free shared mem
7468
int QUICK_SELECT_DESC::get_next()
7470
DBUG_ENTER("QUICK_SELECT_DESC::get_next");
7472
/* The max key is handled as follows:
7473
* - if there is NO_MAX_RANGE, start at the end and move backwards
7474
* - if it is an EQ_RANGE, which means that max key covers the entire
7475
* key, go directly to the key and read through it (sorting backwards is
7476
* same as sorting forwards)
7477
* - if it is NEAR_MAX, go to the key or next, step back once, and
7479
* - otherwise (not NEAR_MAX == include the key), go after the key,
7480
* step back once, and move backwards
7487
{ // Already read through key
7488
result = ((last_range->flag & EQ_RANGE)
7489
? file->index_next_same(record, last_range->min_key,
7490
last_range->min_length) :
7491
file->index_prev(record));
7494
if (cmp_prev(*rev_it.ref()) == 0)
7497
else if (result != HA_ERR_END_OF_FILE)
7498
DBUG_RETURN(result);
7501
if (!(last_range= rev_it++))
7502
DBUG_RETURN(HA_ERR_END_OF_FILE); // All ranges used
7504
if (last_range->flag & NO_MAX_RANGE) // Read last record
7507
if ((local_error=file->index_last(record)))
7508
DBUG_RETURN(local_error); // Empty table
7509
if (cmp_prev(last_range) == 0)
7511
last_range= 0; // No match; go to next range
7515
if (last_range->flag & EQ_RANGE)
7517
result = file->index_read_map(record, last_range->max_key,
7518
last_range->max_keypart_map,
7523
DBUG_ASSERT(last_range->flag & NEAR_MAX ||
7524
range_reads_after_key(last_range));
7525
result=file->index_read_map(record, last_range->max_key,
7526
last_range->max_keypart_map,
7527
((last_range->flag & NEAR_MAX) ?
7528
HA_READ_BEFORE_KEY :
7529
HA_READ_PREFIX_LAST_OR_PREV));
7533
if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
7534
DBUG_RETURN(result);
7535
last_range= 0; // Not found, to next range
7538
if (cmp_prev(last_range) == 0)
7540
if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
7541
last_range= 0; // Stop searching
7542
DBUG_RETURN(0); // Found key is in range
7544
last_range= 0; // To next range
7550
Compare if found key is over max-value
7551
Returns 0 if key <= range->max_key
7552
TODO: Figure out why can't this function be as simple as cmp_prev().
7555
int QUICK_RANGE_SELECT::cmp_next(QUICK_RANGE *range_arg)
7557
if (range_arg->flag & NO_MAX_RANGE)
7558
return 0; /* key can't be to large */
7560
KEY_PART *key_part=key_parts;
7563
for (uchar *key=range_arg->max_key, *end=key+range_arg->max_length;
7565
key+= store_length, key_part++)
7568
store_length= key_part->store_length;
7569
if (key_part->null_bit)
7573
if (!key_part->field->is_null())
7577
else if (key_part->field->is_null())
7579
key++; // Skip null byte
7582
if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
7587
return (range_arg->flag & NEAR_MAX) ? 1 : 0; // Exact match
7592
Returns 0 if found key is inside range (found key >= range->min_key).
7595
int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg)
7598
if (range_arg->flag & NO_MIN_RANGE)
7599
return 0; /* key can't be to small */
7601
cmp= key_cmp(key_part_info, range_arg->min_key,
7602
range_arg->min_length);
7603
if (cmp > 0 || (cmp == 0 && (range_arg->flag & NEAR_MIN) == false))
7605
return 1; // outside of range
7610
* TRUE if this range will require using HA_READ_AFTER_KEY
7611
See comment in get_next() about this
7614
bool QUICK_SELECT_DESC::range_reads_after_key(QUICK_RANGE *range_arg)
7616
return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
7617
!(range_arg->flag & EQ_RANGE) ||
7618
head->key_info[index].key_length != range_arg->max_length) ? 1 : 0;
7622
void QUICK_RANGE_SELECT::add_info_string(String *str)
7624
KEY *key_info= head->key_info + index;
7625
str->append(key_info->name);
7628
void QUICK_INDEX_MERGE_SELECT::add_info_string(String *str)
7630
QUICK_RANGE_SELECT *quick;
7632
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
7633
str->append(STRING_WITH_LEN("sort_union("));
7634
while ((quick= it++))
7640
quick->add_info_string(str);
7642
if (pk_quick_select)
7645
pk_quick_select->add_info_string(str);
7650
void QUICK_ROR_INTERSECT_SELECT::add_info_string(String *str)
7653
QUICK_RANGE_SELECT *quick;
7654
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
7655
str->append(STRING_WITH_LEN("intersect("));
7656
while ((quick= it++))
7658
KEY *key_info= head->key_info + quick->index;
7663
str->append(key_info->name);
7667
KEY *key_info= head->key_info + cpk_quick->index;
7669
str->append(key_info->name);
7674
void QUICK_ROR_UNION_SELECT::add_info_string(String *str)
7677
QUICK_SELECT_I *quick;
7678
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
7679
str->append(STRING_WITH_LEN("union("));
7680
while ((quick= it++))
7686
quick->add_info_string(str);
7692
void QUICK_RANGE_SELECT::add_keys_and_lengths(String *key_names,
7693
String *used_lengths)
7697
KEY *key_info= head->key_info + index;
7698
key_names->append(key_info->name);
7699
length= longlong2str(max_used_key_length, buf, 10) - buf;
7700
used_lengths->append(buf, length);
7703
void QUICK_INDEX_MERGE_SELECT::add_keys_and_lengths(String *key_names,
7704
String *used_lengths)
7709
QUICK_RANGE_SELECT *quick;
7711
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
7712
while ((quick= it++))
7718
key_names->append(',');
7719
used_lengths->append(',');
7722
KEY *key_info= head->key_info + quick->index;
7723
key_names->append(key_info->name);
7724
length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
7725
used_lengths->append(buf, length);
7727
if (pk_quick_select)
7729
KEY *key_info= head->key_info + pk_quick_select->index;
7730
key_names->append(',');
7731
key_names->append(key_info->name);
7732
length= longlong2str(pk_quick_select->max_used_key_length, buf, 10) - buf;
7733
used_lengths->append(',');
7734
used_lengths->append(buf, length);
7738
void QUICK_ROR_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
7739
String *used_lengths)
7744
QUICK_RANGE_SELECT *quick;
7745
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
7746
while ((quick= it++))
7748
KEY *key_info= head->key_info + quick->index;
7753
key_names->append(',');
7754
used_lengths->append(',');
7756
key_names->append(key_info->name);
7757
length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
7758
used_lengths->append(buf, length);
7763
KEY *key_info= head->key_info + cpk_quick->index;
7764
key_names->append(',');
7765
key_names->append(key_info->name);
7766
length= longlong2str(cpk_quick->max_used_key_length, buf, 10) - buf;
7767
used_lengths->append(',');
7768
used_lengths->append(buf, length);
7772
void QUICK_ROR_UNION_SELECT::add_keys_and_lengths(String *key_names,
7773
String *used_lengths)
7776
QUICK_SELECT_I *quick;
7777
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
7778
while ((quick= it++))
7784
used_lengths->append(',');
7785
key_names->append(',');
7787
quick->add_keys_and_lengths(key_names, used_lengths);
7792
/*******************************************************************************
7793
* Implementation of QUICK_GROUP_MIN_MAX_SELECT
7794
*******************************************************************************/
7796
static inline uint get_field_keypart(KEY *index, Field *field);
7797
static inline SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree,
7798
PARAM *param, uint *param_idx);
7799
static bool get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
7800
KEY_PART_INFO *first_non_group_part,
7801
KEY_PART_INFO *min_max_arg_part,
7802
KEY_PART_INFO *last_part, THD *thd,
7803
uchar *key_infix, uint *key_infix_len,
7804
KEY_PART_INFO **first_non_infix_part);
7806
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
7807
Field::imagetype image_type);
7810
cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
7811
uint group_key_parts, SEL_TREE *range_tree,
7812
SEL_ARG *index_tree, ha_rows quick_prefix_records,
7813
bool have_min, bool have_max,
7814
double *read_cost, ha_rows *records);
7818
Test if this access method is applicable to a GROUP query with MIN/MAX
7819
functions, and if so, construct a new TRP object.
7822
get_best_group_min_max()
7823
param Parameter from test_quick_select
7824
sel_tree Range tree generated by get_mm_tree
7827
Test whether a query can be computed via a QUICK_GROUP_MIN_MAX_SELECT.
7828
Queries computable via a QUICK_GROUP_MIN_MAX_SELECT must satisfy the
7829
following conditions:
7830
A) Table T has at least one compound index I of the form:
7831
I = <A_1, ...,A_k, [B_1,..., B_m], C, [D_1,...,D_n]>
7832
B) Query conditions:
7833
B0. Q is over a single table T.
7834
B1. The attributes referenced by Q are a subset of the attributes of I.
7835
B2. All attributes QA in Q can be divided into 3 overlapping groups:
7836
- SA = {S_1, ..., S_l, [C]} - from the SELECT clause, where C is
7837
referenced by any number of MIN and/or MAX functions if present.
7838
- WA = {W_1, ..., W_p} - from the WHERE clause
7839
- GA = <G_1, ..., G_k> - from the GROUP BY clause (if any)
7840
= SA - if Q is a DISTINCT query (based on the
7841
equivalence of DISTINCT and GROUP queries.
7842
- NGA = QA - (GA union C) = {NG_1, ..., NG_m} - the ones not in
7843
GROUP BY and not referenced by MIN/MAX functions.
7844
with the following properties specified below.
7845
B3. If Q has a GROUP BY WITH ROLLUP clause the access method is not
7848
SA1. There is at most one attribute in SA referenced by any number of
7849
MIN and/or MAX functions which, which if present, is denoted as C.
7850
SA2. The position of the C attribute in the index is after the last A_k.
7851
SA3. The attribute C can be referenced in the WHERE clause only in
7852
predicates of the forms:
7853
- (C {< | <= | > | >= | =} const)
7854
- (const {< | <= | > | >= | =} C)
7855
- (C between const_i and const_j)
7859
SA4. If Q has a GROUP BY clause, there are no other aggregate functions
7860
except MIN and MAX. For queries with DISTINCT, aggregate functions
7862
SA5. The select list in DISTINCT queries should not contain expressions.
7863
GA1. If Q has a GROUP BY clause, then GA is a prefix of I. That is, if
7865
GA2. If Q has a DISTINCT clause, then there is a permutation of SA that
7866
forms a prefix of I. This permutation is used as the GROUP clause
7867
when the DISTINCT query is converted to a GROUP query.
7868
GA3. The attributes in GA may participate in arbitrary predicates, divided
7870
- RNG(G_1,...,G_q ; where q <= k) is a range condition over the
7871
attributes of a prefix of GA
7872
- PA(G_i1,...G_iq) is an arbitrary predicate over an arbitrary subset
7873
of GA. Since P is applied to only GROUP attributes it filters some
7874
groups, and thus can be applied after the grouping.
7875
GA4. There are no expressions among G_i, just direct column references.
7876
NGA1.If in the index I there is a gap between the last GROUP attribute G_k,
7877
and the MIN/MAX attribute C, then NGA must consist of exactly the
7878
index attributes that constitute the gap. As a result there is a
7879
permutation of NGA that coincides with the gap in the index
7881
NGA2.If BA <> {}, then the WHERE clause must contain a conjunction EQ of
7882
equality conditions for all NG_i of the form (NG_i = const) or
7883
(const = NG_i), such that each NG_i is referenced in exactly one
7884
conjunct. Informally, the predicates provide constants to fill the
7886
WA1. There are no other attributes in the WHERE clause except the ones
7887
referenced in predicates RNG, PA, PC, EQ defined above. Therefore
7888
WA is subset of (GA union NGA union C) for GA,NGA,C that pass the
7889
above tests. By transitivity then it also follows that each WA_i
7890
participates in the index I (if this was already tested for GA, NGA
7893
C) Overall query form:
7894
SELECT EXPR([A_1,...,A_k], [B_1,...,B_m], [MIN(C)], [MAX(C)])
7896
WHERE [RNG(A_1,...,A_p ; where p <= k)]
7897
[AND EQ(B_1,...,B_m)]
7899
[AND PA(A_i1,...,A_iq)]
7900
GROUP BY A_1,...,A_k
7901
[HAVING PH(A_1, ..., B_1,..., C)]
7902
where EXPR(...) is an arbitrary expression over some or all SELECT fields,
7904
SELECT DISTINCT A_i1,...,A_ik
7906
WHERE [RNG(A_1,...,A_p ; where p <= k)]
7907
[AND PA(A_i1,...,A_iq)];
7910
If the current query satisfies the conditions above, and if
7911
(mem_root! = NULL), then the function constructs and returns a new TRP
7912
object, that is later used to construct a new QUICK_GROUP_MIN_MAX_SELECT.
7913
If (mem_root == NULL), then the function only tests whether the current
7914
query satisfies the conditions above, and, if so, sets
7915
is_applicable = TRUE.
7917
Queries with DISTINCT for which index access can be used are transformed
7918
into equivalent group-by queries of the form:
7920
SELECT A_1,...,A_k FROM T
7921
WHERE [RNG(A_1,...,A_p ; where p <= k)]
7922
[AND PA(A_i1,...,A_iq)]
7923
GROUP BY A_1,...,A_k;
7925
The group-by list is a permutation of the select attributes, according
7926
to their order in the index.
7929
- What happens if the query groups by the MIN/MAX field, and there is no
7930
other field as in: "select min(a) from t1 group by a" ?
7931
- We assume that the general correctness of the GROUP-BY query was checked
7932
before this point. Is this correct, or do we have to check it completely?
7933
- Lift the limitation in condition (B3), that is, make this access method
7934
applicable to ROLLUP queries.
7938
- valid TRP_GROUP_MIN_MAX object if this QUICK class can be used for
7945
static TRP_GROUP_MIN_MAX *
7946
get_best_group_min_max(PARAM *param, SEL_TREE *tree)
7948
THD *thd= param->thd;
7949
JOIN *join= thd->lex->current_select->join;
7950
TABLE *table= param->table;
7951
bool have_min= FALSE; /* TRUE if there is a MIN function. */
7952
bool have_max= FALSE; /* TRUE if there is a MAX function. */
7953
Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
7954
KEY_PART_INFO *min_max_arg_part= NULL; /* The corresponding keypart. */
7955
uint group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
7956
KEY *index_info= NULL; /* The index chosen for data access. */
7957
uint index= 0; /* The id of the chosen index. */
7958
uint group_key_parts= 0; // Number of index key parts in the group prefix.
7959
uint used_key_parts= 0; /* Number of index key parts used for access. */
7960
uchar key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
7961
uint key_infix_len= 0; /* Length of key_infix. */
7962
TRP_GROUP_MIN_MAX *read_plan= NULL; /* The eventually constructed TRP. */
7966
Item_field *item_field;
7967
DBUG_ENTER("get_best_group_min_max");
7969
/* Perform few 'cheap' tests whether this access method is applicable. */
7971
DBUG_RETURN(NULL); /* This is not a select statement. */
7972
if ((join->tables != 1) || /* The query must reference one table. */
7973
((!join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
7974
(!join->select_distinct)) ||
7975
(join->select_lex->olap == ROLLUP_TYPE)) /* Check (B3) for ROLLUP */
7977
if (table->s->keys == 0) /* There are no indexes to use. */
7980
/* Analyze the query in more detail. */
7981
List_iterator<Item> select_items_it(join->fields_list);
7983
/* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
7984
if (join->make_sum_func_list(join->all_fields, join->fields_list, 1))
7986
if (join->sum_funcs[0])
7988
Item_sum *min_max_item;
7989
Item_sum **func_ptr= join->sum_funcs;
7990
while ((min_max_item= *(func_ptr++)))
7992
if (min_max_item->sum_func() == Item_sum::MIN_FUNC)
7994
else if (min_max_item->sum_func() == Item_sum::MAX_FUNC)
7999
/* The argument of MIN/MAX. */
8000
Item *expr= min_max_item->args[0]->real_item();
8001
if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
8003
if (! min_max_arg_item)
8004
min_max_arg_item= (Item_field*) expr;
8005
else if (! min_max_arg_item->eq(expr, 1))
8014
if (join->select_distinct)
8016
while ((item= select_items_it++))
8018
if (item->type() != Item::FIELD_ITEM)
8023
/* Check (GA4) - that there are no expressions among the group attributes. */
8024
for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
8026
if ((*tmp_group->item)->type() != Item::FIELD_ITEM)
8031
Check that table has at least one compound index such that the conditions
8032
(GA1,GA2) are all TRUE. If there is more than one such index, select the
8033
first one. Here we set the variables: group_prefix_len and index_info.
8035
KEY *cur_index_info= table->key_info;
8036
KEY *cur_index_info_end= cur_index_info + table->s->keys;
8037
KEY_PART_INFO *cur_part= NULL;
8038
KEY_PART_INFO *end_part; /* Last part for loops. */
8039
/* Last index part. */
8040
KEY_PART_INFO *last_part= NULL;
8041
KEY_PART_INFO *first_non_group_part= NULL;
8042
KEY_PART_INFO *first_non_infix_part= NULL;
8043
uint key_infix_parts= 0;
8044
uint cur_group_key_parts= 0;
8045
uint cur_group_prefix_len= 0;
8046
/* Cost-related variables for the best index so far. */
8047
double best_read_cost= DBL_MAX;
8048
ha_rows best_records= 0;
8049
SEL_ARG *best_index_tree= NULL;
8050
ha_rows best_quick_prefix_records= 0;
8051
uint best_param_idx= 0;
8052
double cur_read_cost= DBL_MAX;
8053
ha_rows cur_records;
8054
SEL_ARG *cur_index_tree= NULL;
8055
ha_rows cur_quick_prefix_records= 0;
8056
uint cur_param_idx=MAX_KEY;
8057
key_map cur_used_key_parts;
8058
uint pk= param->table->s->primary_key;
8060
for (uint cur_index= 0 ; cur_index_info != cur_index_info_end ;
8061
cur_index_info++, cur_index++)
8063
/* Check (B1) - if current index is covering. */
8064
if (!table->covering_keys.is_set(cur_index))
8068
If the current storage manager is such that it appends the primary key to
8069
each index, then the above condition is insufficient to check if the
8070
index is covering. In such cases it may happen that some fields are
8071
covered by the PK index, but not by the current index. Since we can't
8072
use the concatenation of both indexes for index lookup, such an index
8073
does not qualify as covering in our case. If this is the case, below
8074
we check that all query fields are indeed covered by 'cur_index'.
8076
if (pk < MAX_KEY && cur_index != pk &&
8077
(table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
8079
/* For each table field */
8080
for (uint i= 0; i < table->s->fields; i++)
8082
Field *cur_field= table->field[i];
8084
If the field is used in the current query ensure that it's
8087
if (bitmap_is_set(table->read_set, cur_field->field_index) &&
8088
!cur_field->part_of_key_not_clustered.is_set(cur_index))
8089
goto next_index; // Field was not part of key
8094
Check (GA1) for GROUP BY queries.
8096
if (join->group_list)
8098
cur_part= cur_index_info->key_part;
8099
end_part= cur_part + cur_index_info->key_parts;
8100
/* Iterate in parallel over the GROUP list and the index parts. */
8101
for (tmp_group= join->group_list; tmp_group && (cur_part != end_part);
8102
tmp_group= tmp_group->next, cur_part++)
8106
tmp_group::item is an array of Item, is it OK to consider only the
8107
first Item? If so, then why? What is the array for?
8109
/* Above we already checked that all group items are fields. */
8110
DBUG_ASSERT((*tmp_group->item)->type() == Item::FIELD_ITEM);
8111
Item_field *group_field= (Item_field *) (*tmp_group->item);
8112
if (group_field->field->eq(cur_part->field))
8114
cur_group_prefix_len+= cur_part->store_length;
8115
++cur_group_key_parts;
8122
Check (GA2) if this is a DISTINCT query.
8123
If GA2, then Store a new ORDER object in group_fields_array at the
8124
position of the key part of item_field->field. Thus we get the ORDER
8125
objects for each field ordered as the corresponding key parts.
8126
Later group_fields_array of ORDER objects is used to convert the query
8129
else if (join->select_distinct)
8131
select_items_it.rewind();
8132
cur_used_key_parts.clear_all();
8133
uint max_key_part= 0;
8134
while ((item= select_items_it++))
8136
item_field= (Item_field*) item; /* (SA5) already checked above. */
8137
/* Find the order of the key part in the index. */
8138
key_part_nr= get_field_keypart(cur_index_info, item_field->field);
8140
Check if this attribute was already present in the select list.
8141
If it was present, then its corresponding key part was alredy used.
8143
if (cur_used_key_parts.is_set(key_part_nr))
8145
if (key_part_nr < 1 || key_part_nr > join->fields_list.elements)
8147
cur_part= cur_index_info->key_part + key_part_nr - 1;
8148
cur_group_prefix_len+= cur_part->store_length;
8149
cur_used_key_parts.set_bit(key_part_nr);
8150
++cur_group_key_parts;
8151
max_key_part= max(max_key_part,key_part_nr);
8154
Check that used key parts forms a prefix of the index.
8155
To check this we compare bits in all_parts and cur_parts.
8156
all_parts have all bits set from 0 to (max_key_part-1).
8157
cur_parts have bits set for only used keyparts.
8159
ulonglong all_parts, cur_parts;
8160
all_parts= (1<<max_key_part) - 1;
8161
cur_parts= cur_used_key_parts.to_ulonglong() >> 1;
8162
if (all_parts != cur_parts)
8169
if (min_max_arg_item)
8171
key_part_nr= get_field_keypart(cur_index_info, min_max_arg_item->field);
8172
if (key_part_nr <= cur_group_key_parts)
8174
min_max_arg_part= cur_index_info->key_part + key_part_nr - 1;
8178
Check (NGA1, NGA2) and extract a sequence of constants to be used as part
8183
If there is MIN/MAX, each keypart between the last group part and the
8184
MIN/MAX part must participate in one equality with constants, and all
8185
keyparts after the MIN/MAX part must not be referenced in the query.
8187
If there is no MIN/MAX, the keyparts after the last group part can be
8188
referenced only in equalities with constants, and the referenced keyparts
8189
must form a sequence without any gaps that starts immediately after the
8192
last_part= cur_index_info->key_part + cur_index_info->key_parts;
8193
first_non_group_part= (cur_group_key_parts < cur_index_info->key_parts) ?
8194
cur_index_info->key_part + cur_group_key_parts :
8196
first_non_infix_part= min_max_arg_part ?
8197
(min_max_arg_part < last_part) ?
8201
if (first_non_group_part &&
8202
(!min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
8207
SEL_ARG *index_range_tree= get_index_range_tree(cur_index, tree, param,
8209
if (!get_constant_key_infix(cur_index_info, index_range_tree,
8210
first_non_group_part, min_max_arg_part,
8211
last_part, thd, key_infix, &key_infix_len,
8212
&first_non_infix_part))
8215
else if (min_max_arg_part &&
8216
(min_max_arg_part - first_non_group_part > 0))
8219
There is a gap but no range tree, thus no predicates at all for the
8224
else if (first_non_group_part && join->conds)
8227
If there is no MIN/MAX function in the query, but some index
8228
key part is referenced in the WHERE clause, then this index
8229
cannot be used because the WHERE condition over the keypart's
8230
field cannot be 'pushed' to the index (because there is no
8231
range 'tree'), and the WHERE clause must be evaluated before
8235
Store the first and last keyparts that need to be analyzed
8236
into one array that can be passed as parameter.
8238
KEY_PART_INFO *key_part_range[2];
8239
key_part_range[0]= first_non_group_part;
8240
key_part_range[1]= last_part;
8242
/* Check if cur_part is referenced in the WHERE clause. */
8243
if (join->conds->walk(&Item::find_item_in_field_list_processor, 0,
8244
(uchar*) key_part_range))
8250
Test (WA1) partially - that no other keypart after the last infix part is
8251
referenced in the query.
8253
if (first_non_infix_part)
8255
cur_part= first_non_infix_part +
8256
(min_max_arg_part && (min_max_arg_part < last_part));
8257
for (; cur_part != last_part; cur_part++)
8259
if (bitmap_is_set(table->read_set, cur_part->field->field_index))
8264
/* If we got to this point, cur_index_info passes the test. */
8265
key_infix_parts= key_infix_len ?
8266
(first_non_infix_part - first_non_group_part) : 0;
8267
used_key_parts= cur_group_key_parts + key_infix_parts;
8269
/* Compute the cost of using this index. */
8272
/* Find the SEL_ARG sub-tree that corresponds to the chosen index. */
8273
cur_index_tree= get_index_range_tree(cur_index, tree, param,
8275
/* Check if this range tree can be used for prefix retrieval. */
8276
COST_VECT dummy_cost;
8277
uint mrr_flags= HA_MRR_USE_DEFAULT_IMPL;
8279
cur_quick_prefix_records= check_quick_select(param, cur_param_idx,
8280
FALSE /*don't care*/,
8281
cur_index_tree, TRUE,
8282
&mrr_flags, &mrr_bufsize,
8285
cost_group_min_max(table, cur_index_info, used_key_parts,
8286
cur_group_key_parts, tree, cur_index_tree,
8287
cur_quick_prefix_records, have_min, have_max,
8288
&cur_read_cost, &cur_records);
8290
If cur_read_cost is lower than best_read_cost use cur_index.
8291
Do not compare doubles directly because they may have different
8292
representations (64 vs. 80 bits).
8294
if (cur_read_cost < best_read_cost - (DBL_EPSILON * cur_read_cost))
8296
DBUG_ASSERT(tree != 0 || cur_param_idx == MAX_KEY);
8297
index_info= cur_index_info;
8299
best_read_cost= cur_read_cost;
8300
best_records= cur_records;
8301
best_index_tree= cur_index_tree;
8302
best_quick_prefix_records= cur_quick_prefix_records;
8303
best_param_idx= cur_param_idx;
8304
group_key_parts= cur_group_key_parts;
8305
group_prefix_len= cur_group_prefix_len;
8309
cur_group_key_parts= 0;
8310
cur_group_prefix_len= 0;
8312
if (!index_info) /* No usable index found. */
8315
/* Check (SA3) for the where clause. */
8316
if (join->conds && min_max_arg_item &&
8317
!check_group_min_max_predicates(join->conds, min_max_arg_item,
8318
(index_info->flags & HA_SPATIAL) ?
8319
Field::itMBR : Field::itRAW))
8322
/* The query passes all tests, so construct a new TRP object. */
8323
read_plan= new (param->mem_root)
8324
TRP_GROUP_MIN_MAX(have_min, have_max, min_max_arg_part,
8325
group_prefix_len, used_key_parts,
8326
group_key_parts, index_info, index,
8328
(key_infix_len > 0) ? key_infix : NULL,
8329
tree, best_index_tree, best_param_idx,
8330
best_quick_prefix_records);
8333
if (tree && read_plan->quick_prefix_records == 0)
8336
read_plan->read_cost= best_read_cost;
8337
read_plan->records= best_records;
8340
("Returning group min/max plan: cost: %g, records: %lu",
8341
read_plan->read_cost, (ulong) read_plan->records));
8344
DBUG_RETURN(read_plan);
8349
Check that the MIN/MAX attribute participates only in range predicates
8353
check_group_min_max_predicates()
8354
cond tree (or subtree) describing all or part of the WHERE
8355
clause being analyzed
8356
min_max_arg_item the field referenced by the MIN/MAX function(s)
8357
min_max_arg_part the keypart of the MIN/MAX argument if any
8360
The function walks recursively over the cond tree representing a WHERE
8361
clause, and checks condition (SA3) - if a field is referenced by a MIN/MAX
8362
aggregate function, it is referenced only by one of the following
8363
predicates: {=, !=, <, <=, >, >=, between, is null, is not null}.
8366
TRUE if cond passes the test
8371
check_group_min_max_predicates(COND *cond, Item_field *min_max_arg_item,
8372
Field::imagetype image_type)
8374
DBUG_ENTER("check_group_min_max_predicates");
8375
DBUG_ASSERT(cond && min_max_arg_item);
8377
cond= cond->real_item();
8378
Item::Type cond_type= cond->type();
8379
if (cond_type == Item::COND_ITEM) /* 'AND' or 'OR' */
8381
DBUG_PRINT("info", ("Analyzing: %s", ((Item_func*) cond)->func_name()));
8382
List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
8384
while ((and_or_arg= li++))
8386
if (!check_group_min_max_predicates(and_or_arg, min_max_arg_item,
8395
This is a very crude fix to handle sub-selects in the WHERE clause
8396
(Item_subselect objects). With the test below we rule out from the
8397
optimization all queries with subselects in the WHERE clause. What has to
8398
be done, is that here we should analyze whether the subselect references
8399
the MIN/MAX argument field, and disallow the optimization only if this is
8402
if (cond_type == Item::SUBSELECT_ITEM)
8405
/* We presume that at this point there are no other Items than functions. */
8406
DBUG_ASSERT(cond_type == Item::FUNC_ITEM);
8408
/* Test if cond references only group-by or non-group fields. */
8409
Item_func *pred= (Item_func*) cond;
8410
Item **arguments= pred->arguments();
8412
DBUG_PRINT("info", ("Analyzing: %s", pred->func_name()));
8413
for (uint arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
8415
cur_arg= arguments[arg_idx]->real_item();
8416
DBUG_PRINT("info", ("cur_arg: %s", cur_arg->full_name()));
8417
if (cur_arg->type() == Item::FIELD_ITEM)
8419
if (min_max_arg_item->eq(cur_arg, 1))
8422
If pred references the MIN/MAX argument, check whether pred is a range
8423
condition that compares the MIN/MAX argument with a constant.
8425
Item_func::Functype pred_type= pred->functype();
8426
if (pred_type != Item_func::EQUAL_FUNC &&
8427
pred_type != Item_func::LT_FUNC &&
8428
pred_type != Item_func::LE_FUNC &&
8429
pred_type != Item_func::GT_FUNC &&
8430
pred_type != Item_func::GE_FUNC &&
8431
pred_type != Item_func::BETWEEN &&
8432
pred_type != Item_func::ISNULL_FUNC &&
8433
pred_type != Item_func::ISNOTNULL_FUNC &&
8434
pred_type != Item_func::EQ_FUNC &&
8435
pred_type != Item_func::NE_FUNC)
8438
/* Check that pred compares min_max_arg_item with a constant. */
8440
bzero(args, 3 * sizeof(Item*));
8442
/* Test if this is a comparison of a field and a constant. */
8443
if (!simple_pred(pred, args, &inv))
8446
/* Check for compatible string comparisons - similar to get_mm_leaf. */
8447
if (args[0] && args[1] && !args[2] && // this is a binary function
8448
min_max_arg_item->result_type() == STRING_RESULT &&
8450
Don't use an index when comparing strings of different collations.
8452
((args[1]->result_type() == STRING_RESULT &&
8453
image_type == Field::itRAW &&
8454
((Field_str*) min_max_arg_item->field)->charset() !=
8455
pred->compare_collation())
8458
We can't always use indexes when comparing a string index to a
8461
(args[1]->result_type() != STRING_RESULT &&
8462
min_max_arg_item->field->cmp_type() != args[1]->result_type())))
8466
else if (cur_arg->type() == Item::FUNC_ITEM)
8468
if (!check_group_min_max_predicates(cur_arg, min_max_arg_item,
8472
else if (cur_arg->const_item())
8485
Extract a sequence of constants from a conjunction of equality predicates.
8488
get_constant_key_infix()
8489
index_info [in] Descriptor of the chosen index.
8490
index_range_tree [in] Range tree for the chosen index
8491
first_non_group_part [in] First index part after group attribute parts
8492
min_max_arg_part [in] The keypart of the MIN/MAX argument if any
8493
last_part [in] Last keypart of the index
8494
thd [in] Current thread
8495
key_infix [out] Infix of constants to be used for index lookup
8496
key_infix_len [out] Lenghth of the infix
8497
first_non_infix_part [out] The first keypart after the infix (if any)
8500
Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
8501
for each keypart field NGF_i not in GROUP-BY, check that there is a
8502
constant equality predicate among conds with the form (NGF_i = const_ci) or
8504
Thus all the NGF_i attributes must fill the 'gap' between the last group-by
8505
attribute and the MIN/MAX attribute in the index (if present). If these
8506
conditions hold, copy each constant from its corresponding predicate into
8507
key_infix, in the order its NG_i attribute appears in the index, and update
8508
key_infix_len with the total length of the key parts in key_infix.
8511
TRUE if the index passes the test
8516
get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
8517
KEY_PART_INFO *first_non_group_part,
8518
KEY_PART_INFO *min_max_arg_part,
8519
KEY_PART_INFO *last_part, THD *thd,
8520
uchar *key_infix, uint *key_infix_len,
8521
KEY_PART_INFO **first_non_infix_part)
8524
KEY_PART_INFO *cur_part;
8525
/* End part for the first loop below. */
8526
KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;
8529
uchar *key_ptr= key_infix;
8530
for (cur_part= first_non_group_part; cur_part != end_part; cur_part++)
8533
Find the range tree for the current keypart. We assume that
8534
index_range_tree points to the leftmost keypart in the index.
8536
for (cur_range= index_range_tree; cur_range;
8537
cur_range= cur_range->next_key_part)
8539
if (cur_range->field->eq(cur_part->field))
8544
if (min_max_arg_part)
8545
return FALSE; /* The current keypart has no range predicates at all. */
8548
*first_non_infix_part= cur_part;
8553
/* Check that the current range tree is a single point interval. */
8554
if (cur_range->prev || cur_range->next)
8555
return FALSE; /* This is not the only range predicate for the field. */
8556
if ((cur_range->min_flag & NO_MIN_RANGE) ||
8557
(cur_range->max_flag & NO_MAX_RANGE) ||
8558
(cur_range->min_flag & NEAR_MIN) || (cur_range->max_flag & NEAR_MAX))
8561
uint field_length= cur_part->store_length;
8562
if ((cur_range->maybe_null &&
8563
cur_range->min_value[0] && cur_range->max_value[0]) ||
8564
!memcmp(cur_range->min_value, cur_range->max_value, field_length))
8566
/* cur_range specifies 'IS NULL' or an equality condition. */
8567
memcpy(key_ptr, cur_range->min_value, field_length);
8568
key_ptr+= field_length;
8569
*key_infix_len+= field_length;
8575
if (!min_max_arg_part && (cur_part == last_part))
8576
*first_non_infix_part= last_part;
8583
Find the key part referenced by a field.
8587
index descriptor of an index
8588
field field that possibly references some key part in index
8591
The return value can be used to get a KEY_PART_INFO pointer by
8592
part= index->key_part + get_field_keypart(...) - 1;
8595
Positive number which is the consecutive number of the key part, or
8596
0 if field does not reference any index field.
8600
get_field_keypart(KEY *index, Field *field)
8602
KEY_PART_INFO *part, *end;
8604
for (part= index->key_part, end= part + index->key_parts; part < end; part++)
8606
if (field->eq(part->field))
8607
return part - index->key_part + 1;
8614
Find the SEL_ARG sub-tree that corresponds to the chosen index.
8617
get_index_range_tree()
8618
index [in] The ID of the index being looked for
8619
range_tree[in] Tree of ranges being searched
8620
param [in] PARAM from SQL_SELECT::test_quick_select
8621
param_idx [out] Index in the array PARAM::key that corresponds to 'index'
8625
A SEL_TREE contains range trees for all usable indexes. This procedure
8626
finds the SEL_ARG sub-tree for 'index'. The members of a SEL_TREE are
8627
ordered in the same way as the members of PARAM::key, thus we first find
8628
the corresponding index in the array PARAM::key. This index is returned
8629
through the variable param_idx, to be used later as argument of
8630
check_quick_select().
8633
Pointer to the SEL_ARG subtree that corresponds to index.
8636
SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree, PARAM *param,
8639
uint idx= 0; /* Index nr in param->key_parts */
8640
while (idx < param->keys)
8642
if (index == param->real_keynr[idx])
8647
return(range_tree->keys[idx]);
8652
Compute the cost of a quick_group_min_max_select for a particular index.
8655
cost_group_min_max()
8656
table [in] The table being accessed
8657
index_info [in] The index used to access the table
8658
used_key_parts [in] Number of key parts used to access the index
8659
group_key_parts [in] Number of index key parts in the group prefix
8660
range_tree [in] Tree of ranges for all indexes
8661
index_tree [in] The range tree for the current index
8662
quick_prefix_records [in] Number of records retrieved by the internally
8663
used quick range select if any
8664
have_min [in] True if there is a MIN function
8665
have_max [in] True if there is a MAX function
8666
read_cost [out] The cost to retrieve rows via this quick select
8667
records [out] The number of rows retrieved
8670
This method computes the access cost of a TRP_GROUP_MIN_MAX instance and
8671
the number of rows returned. It updates this->read_cost and this->records.
8674
The cost computation distinguishes several cases:
8675
1) No equality predicates over non-group attributes (thus no key_infix).
8676
If groups are bigger than blocks on the average, then we assume that it
8677
is very unlikely that block ends are aligned with group ends, thus even
8678
if we look for both MIN and MAX keys, all pairs of neighbor MIN/MAX
8679
keys, except for the first MIN and the last MAX keys, will be in the
8680
same block. If groups are smaller than blocks, then we are going to
8682
2) There are equality predicates over non-group attributes.
8683
In this case the group prefix is extended by additional constants, and
8684
as a result the min/max values are inside sub-groups of the original
8685
groups. The number of blocks that will be read depends on whether the
8686
ends of these sub-groups will be contained in the same or in different
8687
blocks. We compute the probability for the two ends of a subgroup to be
8688
in two different blocks as the ratio of:
8689
- the number of positions of the left-end of a subgroup inside a group,
8690
such that the right end of the subgroup is past the end of the buffer
8691
containing the left-end, and
8692
- the total number of possible positions for the left-end of the
8693
subgroup, which is the number of keys in the containing group.
8694
We assume it is very unlikely that two ends of subsequent subgroups are
8696
3) The are range predicates over the group attributes.
8697
Then some groups may be filtered by the range predicates. We use the
8698
selectivity of the range predicates to decide how many groups will be
8702
- Take into account the optional range predicates over the MIN/MAX
8704
- Check if we have a PK index and we use all cols - then each key is a
8705
group, and it will be better to use an index scan.
8711
void cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
8712
uint group_key_parts, SEL_TREE *range_tree,
8713
SEL_ARG *index_tree, ha_rows quick_prefix_records,
8714
bool have_min, bool have_max,
8715
double *read_cost, ha_rows *records)
8717
ha_rows table_records;
8720
uint keys_per_block;
8721
uint keys_per_group;
8722
uint keys_per_subgroup; /* Average number of keys in sub-groups */
8723
/* formed by a key infix. */
8724
double p_overlap; /* Probability that a sub-group overlaps two blocks. */
8725
double quick_prefix_selectivity;
8727
double cpu_cost= 0; /* TODO: CPU cost of index_read calls? */
8728
DBUG_ENTER("cost_group_min_max");
8730
table_records= table->file->stats.records;
8731
keys_per_block= (table->file->stats.block_size / 2 /
8732
(index_info->key_length + table->file->ref_length)
8734
num_blocks= (uint)(table_records / keys_per_block) + 1;
8736
/* Compute the number of keys in a group. */
8737
keys_per_group= index_info->rec_per_key[group_key_parts - 1];
8738
if (keys_per_group == 0) /* If there is no statistics try to guess */
8739
/* each group contains 10% of all records */
8740
keys_per_group= (uint)(table_records / 10) + 1;
8741
num_groups= (uint)(table_records / keys_per_group) + 1;
8743
/* Apply the selectivity of the quick select for group prefixes. */
8744
if (range_tree && (quick_prefix_records != HA_POS_ERROR))
8746
quick_prefix_selectivity= (double) quick_prefix_records /
8747
(double) table_records;
8748
num_groups= (uint) rint(num_groups * quick_prefix_selectivity);
8749
set_if_bigger(num_groups, 1);
8752
if (used_key_parts > group_key_parts)
8754
Compute the probability that two ends of a subgroup are inside
8757
keys_per_subgroup= index_info->rec_per_key[used_key_parts - 1];
8758
if (keys_per_subgroup >= keys_per_block) /* If a subgroup is bigger than */
8759
p_overlap= 1.0; /* a block, it will overlap at least two blocks. */
8762
double blocks_per_group= (double) num_blocks / (double) num_groups;
8763
p_overlap= (blocks_per_group * (keys_per_subgroup - 1)) / keys_per_group;
8764
p_overlap= min(p_overlap, 1.0);
8766
io_cost= (double) min(num_groups * (1 + p_overlap), num_blocks);
8769
io_cost= (keys_per_group > keys_per_block) ?
8770
(have_min && have_max) ? (double) (num_groups + 1) :
8771
(double) num_groups :
8772
(double) num_blocks;
8775
TODO: If there is no WHERE clause and no other expressions, there should be
8776
no CPU cost. We leave it here to make this cost comparable to that of index
8777
scan as computed in SQL_SELECT::test_quick_select().
8779
cpu_cost= (double) num_groups / TIME_FOR_COMPARE;
8781
*read_cost= io_cost + cpu_cost;
8782
*records= num_groups;
8785
("table rows: %lu keys/block: %u keys/group: %u result rows: %lu blocks: %u",
8786
(ulong)table_records, keys_per_block, keys_per_group,
8787
(ulong) *records, num_blocks));
8793
Construct a new quick select object for queries with group by with min/max.
8796
TRP_GROUP_MIN_MAX::make_quick()
8797
param Parameter from test_quick_select
8798
retrieve_full_rows ignored
8799
parent_alloc Memory pool to use, if any.
8802
Make_quick ignores the retrieve_full_rows parameter because
8803
QUICK_GROUP_MIN_MAX_SELECT always performs 'index only' scans.
8804
The other parameter are ignored as well because all necessary
8805
data to create the QUICK object is computed at this TRP creation
8809
New QUICK_GROUP_MIN_MAX_SELECT object if successfully created,
8814
TRP_GROUP_MIN_MAX::make_quick(PARAM *param, bool retrieve_full_rows,
8815
MEM_ROOT *parent_alloc)
8817
QUICK_GROUP_MIN_MAX_SELECT *quick;
8818
DBUG_ENTER("TRP_GROUP_MIN_MAX::make_quick");
8820
quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
8821
param->thd->lex->current_select->join,
8822
have_min, have_max, min_max_arg_part,
8823
group_prefix_len, group_key_parts,
8824
used_key_parts, index_info, index,
8825
read_cost, records, key_infix_len,
8826
key_infix, parent_alloc);
8838
DBUG_ASSERT(quick_prefix_records > 0);
8839
if (quick_prefix_records == HA_POS_ERROR)
8840
quick->quick_prefix_select= NULL; /* Can't construct a quick select. */
8842
/* Make a QUICK_RANGE_SELECT to be used for group prefix retrieval. */
8843
quick->quick_prefix_select= get_quick_select(param, param_idx,
8845
HA_MRR_USE_DEFAULT_IMPL, 0,
8849
Extract the SEL_ARG subtree that contains only ranges for the MIN/MAX
8850
attribute, and create an array of QUICK_RANGES to be used by the
8853
if (min_max_arg_part)
8855
SEL_ARG *min_max_range= index_tree;
8856
while (min_max_range) /* Find the tree for the MIN/MAX key part. */
8858
if (min_max_range->field->eq(min_max_arg_part->field))
8860
min_max_range= min_max_range->next_key_part;
8862
/* Scroll to the leftmost interval for the MIN/MAX argument. */
8863
while (min_max_range && min_max_range->prev)
8864
min_max_range= min_max_range->prev;
8865
/* Create an array of QUICK_RANGEs for the MIN/MAX argument. */
8866
while (min_max_range)
8868
if (quick->add_range(min_max_range))
8874
min_max_range= min_max_range->next;
8879
quick->quick_prefix_select= NULL;
8881
quick->update_key_stat();
8882
quick->adjust_prefix_ranges();
8889
Construct new quick select for group queries with min/max.
8892
QUICK_GROUP_MIN_MAX_SELECT::QUICK_GROUP_MIN_MAX_SELECT()
8893
table The table being accessed
8894
join Descriptor of the current query
8895
have_min TRUE if the query selects a MIN function
8896
have_max TRUE if the query selects a MAX function
8897
min_max_arg_part The only argument field of all MIN/MAX functions
8898
group_prefix_len Length of all key parts in the group prefix
8899
prefix_key_parts All key parts in the group prefix
8900
index_info The index chosen for data access
8901
use_index The id of index_info
8902
read_cost Cost of this access method
8903
records Number of records returned
8904
key_infix_len Length of the key infix appended to the group prefix
8905
key_infix Infix of constants from equality predicates
8906
parent_alloc Memory pool for this and quick_prefix_select data
8912
QUICK_GROUP_MIN_MAX_SELECT::
8913
QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg,
8915
KEY_PART_INFO *min_max_arg_part_arg,
8916
uint group_prefix_len_arg, uint group_key_parts_arg,
8917
uint used_key_parts_arg, KEY *index_info_arg,
8918
uint use_index, double read_cost_arg,
8919
ha_rows records_arg, uint key_infix_len_arg,
8920
uchar *key_infix_arg, MEM_ROOT *parent_alloc)
8921
:join(join_arg), index_info(index_info_arg),
8922
group_prefix_len(group_prefix_len_arg),
8923
group_key_parts(group_key_parts_arg), have_min(have_min_arg),
8924
have_max(have_max_arg), seen_first_key(FALSE),
8925
min_max_arg_part(min_max_arg_part_arg), key_infix(key_infix_arg),
8926
key_infix_len(key_infix_len_arg), min_functions_it(NULL),
8927
max_functions_it(NULL)
8932
record= head->record[0];
8933
tmp_record= head->record[1];
8934
read_time= read_cost_arg;
8935
records= records_arg;
8936
used_key_parts= used_key_parts_arg;
8937
real_key_parts= used_key_parts_arg;
8938
real_prefix_len= group_prefix_len + key_infix_len;
8940
min_max_arg_len= min_max_arg_part ? min_max_arg_part->store_length : 0;
8943
We can't have parent_alloc set as the init function can't handle this case
8946
DBUG_ASSERT(!parent_alloc);
8949
init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0);
8950
join->thd->mem_root= &alloc;
8953
bzero(&alloc, sizeof(MEM_ROOT)); // ensure that it's not used
8958
Do post-constructor initialization.
8961
QUICK_GROUP_MIN_MAX_SELECT::init()
8964
The method performs initialization that cannot be done in the constructor
8965
such as memory allocations that may fail. It allocates memory for the
8966
group prefix and inifix buffers, and for the lists of MIN/MAX item to be
8967
updated during execution.
8974
int QUICK_GROUP_MIN_MAX_SELECT::init()
8976
if (group_prefix) /* Already initialized. */
8979
if (!(last_prefix= (uchar*) alloc_root(&alloc, group_prefix_len)))
8982
We may use group_prefix to store keys with all select fields, so allocate
8983
enough space for it.
8985
if (!(group_prefix= (uchar*) alloc_root(&alloc,
8986
real_prefix_len + min_max_arg_len)))
8989
if (key_infix_len > 0)
8992
The memory location pointed to by key_infix will be deleted soon, so
8993
allocate a new buffer and copy the key_infix into it.
8995
uchar *tmp_key_infix= (uchar*) alloc_root(&alloc, key_infix_len);
8998
memcpy(tmp_key_infix, this->key_infix, key_infix_len);
8999
this->key_infix= tmp_key_infix;
9002
if (min_max_arg_part)
9004
if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16))
9009
if (!(min_functions= new List<Item_sum>))
9013
min_functions= NULL;
9016
if (!(max_functions= new List<Item_sum>))
9020
max_functions= NULL;
9022
Item_sum *min_max_item;
9023
Item_sum **func_ptr= join->sum_funcs;
9024
while ((min_max_item= *(func_ptr++)))
9026
if (have_min && (min_max_item->sum_func() == Item_sum::MIN_FUNC))
9027
min_functions->push_back(min_max_item);
9028
else if (have_max && (min_max_item->sum_func() == Item_sum::MAX_FUNC))
9029
max_functions->push_back(min_max_item);
9034
if (!(min_functions_it= new List_iterator<Item_sum>(*min_functions)))
9040
if (!(max_functions_it= new List_iterator<Item_sum>(*max_functions)))
9045
min_max_ranges.elements= 0;
9051
QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT()
9053
DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT");
9054
if (file->inited != handler::NONE)
9055
file->ha_index_end();
9056
if (min_max_arg_part)
9057
delete_dynamic(&min_max_ranges);
9058
free_root(&alloc,MYF(0));
9059
delete min_functions_it;
9060
delete max_functions_it;
9061
delete quick_prefix_select;
9067
Eventually create and add a new quick range object.
9070
QUICK_GROUP_MIN_MAX_SELECT::add_range()
9071
sel_range Range object from which a
9074
Construct a new QUICK_RANGE object from a SEL_ARG object, and
9075
add it to the array min_max_ranges. If sel_arg is an infinite
9076
range, e.g. (x < 5 or x > 4), then skip it and do not construct
9084
bool QUICK_GROUP_MIN_MAX_SELECT::add_range(SEL_ARG *sel_range)
9087
uint range_flag= sel_range->min_flag | sel_range->max_flag;
9089
/* Skip (-inf,+inf) ranges, e.g. (x < 5 or x > 4). */
9090
if ((range_flag & NO_MIN_RANGE) && (range_flag & NO_MAX_RANGE))
9093
if (!(sel_range->min_flag & NO_MIN_RANGE) &&
9094
!(sel_range->max_flag & NO_MAX_RANGE))
9096
if (sel_range->maybe_null &&
9097
sel_range->min_value[0] && sel_range->max_value[0])
9098
range_flag|= NULL_RANGE; /* IS NULL condition */
9099
else if (memcmp(sel_range->min_value, sel_range->max_value,
9100
min_max_arg_len) == 0)
9101
range_flag|= EQ_RANGE; /* equality condition */
9103
range= new QUICK_RANGE(sel_range->min_value, min_max_arg_len,
9104
make_keypart_map(sel_range->part),
9105
sel_range->max_value, min_max_arg_len,
9106
make_keypart_map(sel_range->part),
9110
if (insert_dynamic(&min_max_ranges, (uchar*)&range))
9117
Opens the ranges if there are more conditions in quick_prefix_select than
9118
the ones used for jumping through the prefixes.
9121
QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges()
9124
quick_prefix_select is made over the conditions on the whole key.
9125
It defines a number of ranges of length x.
9126
However when jumping through the prefixes we use only the the first
9127
few most significant keyparts in the range key. However if there
9128
are more keyparts to follow the ones we are using we must make the
9129
condition on the key inclusive (because x < "ab" means
9130
x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b').
9131
To achive the above we must turn off the NEAR_MIN/NEAR_MAX
9133
void QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges ()
9135
if (quick_prefix_select &&
9136
group_prefix_len < quick_prefix_select->max_used_key_length)
9141
for (inx= 0, arr= &quick_prefix_select->ranges; inx < arr->elements; inx++)
9145
get_dynamic(arr, (uchar*)&range, inx);
9146
range->flag &= ~(NEAR_MIN | NEAR_MAX);
9153
Determine the total number and length of the keys that will be used for
9157
QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
9160
The total length of the keys used for index lookup depends on whether
9161
there are any predicates referencing the min/max argument, and/or if
9162
the min/max argument field can be NULL.
9163
This function does an optimistic analysis whether the search key might
9164
be extended by a constant for the min/max keypart. It is 'optimistic'
9165
because during actual execution it may happen that a particular range
9166
is skipped, and then a shorter key will be used. However this is data
9167
dependent and can't be easily estimated here.
9173
void QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
9175
max_used_key_length= real_prefix_len;
9176
if (min_max_ranges.elements > 0)
9178
QUICK_RANGE *cur_range;
9180
{ /* Check if the right-most range has a lower boundary. */
9181
get_dynamic(&min_max_ranges, (uchar*)&cur_range,
9182
min_max_ranges.elements - 1);
9183
if (!(cur_range->flag & NO_MIN_RANGE))
9185
max_used_key_length+= min_max_arg_len;
9191
{ /* Check if the left-most range has an upper boundary. */
9192
get_dynamic(&min_max_ranges, (uchar*)&cur_range, 0);
9193
if (!(cur_range->flag & NO_MAX_RANGE))
9195
max_used_key_length+= min_max_arg_len;
9201
else if (have_min && min_max_arg_part &&
9202
min_max_arg_part->field->real_maybe_null())
9205
If a MIN/MAX argument value is NULL, we can quickly determine
9206
that we're in the beginning of the next group, because NULLs
9207
are always < any other value. This allows us to quickly
9208
determine the end of the current group and jump to the next
9209
group (see next_min()) and thus effectively increases the
9212
max_used_key_length+= min_max_arg_len;
9219
Initialize a quick group min/max select for key retrieval.
9222
QUICK_GROUP_MIN_MAX_SELECT::reset()
9225
Initialize the index chosen for access and find and store the prefix
9226
of the last group. The method is expensive since it performs disk access.
9233
int QUICK_GROUP_MIN_MAX_SELECT::reset(void)
9236
DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::reset");
9238
file->extra(HA_EXTRA_KEYREAD); /* We need only the key attributes */
9239
if ((result= file->ha_index_init(index,1)))
9240
DBUG_RETURN(result);
9241
if (quick_prefix_select && quick_prefix_select->reset())
9243
result= file->index_last(record);
9244
if (result == HA_ERR_END_OF_FILE)
9246
/* Save the prefix of the last group. */
9247
key_copy(last_prefix, record, index_info, group_prefix_len);
9255
Get the next key containing the MIN and/or MAX key for the next group.
9258
QUICK_GROUP_MIN_MAX_SELECT::get_next()
9261
The method finds the next subsequent group of records that satisfies the
9262
query conditions and finds the keys that contain the MIN/MAX values for
9263
the key part referenced by the MIN/MAX function(s). Once a group and its
9264
MIN/MAX values are found, store these values in the Item_sum objects for
9265
the MIN/MAX functions. The rest of the values in the result row are stored
9266
in the Item_field::result_field of each select field. If the query does
9267
not contain MIN and/or MAX functions, then the function only finds the
9268
group prefix, which is a query answer itself.
9271
If both MIN and MAX are computed, then we use the fact that if there is
9272
no MIN key, there can't be a MAX key as well, so we can skip looking
9273
for a MAX key in this case.
9277
HA_ERR_END_OF_FILE if returned all keys
9278
other if some error occurred
9281
int QUICK_GROUP_MIN_MAX_SELECT::get_next()
9286
int is_last_prefix= 0;
9288
DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::get_next");
9291
Loop until a group is found that satisfies all query conditions or the last
9296
result= next_prefix();
9298
Check if this is the last group prefix. Notice that at this point
9299
this->record contains the current prefix in record format.
9303
is_last_prefix= key_cmp(index_info->key_part, last_prefix,
9305
DBUG_ASSERT(is_last_prefix <= 0);
9309
if (result == HA_ERR_KEY_NOT_FOUND)
9316
min_res= next_min();
9318
update_min_result();
9320
/* If there is no MIN in the group, there is no MAX either. */
9321
if ((have_max && !have_min) ||
9322
(have_max && have_min && (min_res == 0)))
9324
max_res= next_max();
9326
update_max_result();
9327
/* If a MIN was found, a MAX must have been found as well. */
9328
DBUG_ASSERT((have_max && !have_min) ||
9329
(have_max && have_min && (max_res == 0)));
9332
If this is just a GROUP BY or DISTINCT without MIN or MAX and there
9333
are equality predicates for the key parts after the group, find the
9334
first sub-group with the extended prefix.
9336
if (!have_min && !have_max && key_infix_len > 0)
9337
result= file->index_read_map(record, group_prefix,
9338
make_prev_keypart_map(real_key_parts),
9341
result= have_min ? min_res : have_max ? max_res : result;
9342
} while ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
9343
is_last_prefix != 0);
9348
Partially mimic the behavior of end_select_send. Copy the
9349
field data from Item_field::field into Item_field::result_field
9350
of each non-aggregated field (the group fields, and optionally
9351
other fields in non-ANSI SQL mode).
9353
copy_fields(&join->tmp_table_param);
9355
else if (result == HA_ERR_KEY_NOT_FOUND)
9356
result= HA_ERR_END_OF_FILE;
9358
DBUG_RETURN(result);
9363
Retrieve the minimal key in the next group.
9366
QUICK_GROUP_MIN_MAX_SELECT::next_min()
9369
Find the minimal key within this group such that the key satisfies the query
9370
conditions and NULL semantics. The found key is loaded into this->record.
9373
Depending on the values of min_max_ranges.elements, key_infix_len, and
9374
whether there is a NULL in the MIN field, this function may directly
9375
return without any data access. In this case we use the key loaded into
9376
this->record by the call to this->next_prefix() just before this call.
9380
HA_ERR_KEY_NOT_FOUND if no MIN key was found that fulfills all conditions.
9381
HA_ERR_END_OF_FILE - "" -
9382
other if some error occurred
9385
int QUICK_GROUP_MIN_MAX_SELECT::next_min()
9388
DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_min");
9390
/* Find the MIN key using the eventually extended group prefix. */
9391
if (min_max_ranges.elements > 0)
9393
if ((result= next_min_in_range()))
9394
DBUG_RETURN(result);
9398
/* Apply the constant equality conditions to the non-group select fields */
9399
if (key_infix_len > 0)
9401
if ((result= file->index_read_map(record, group_prefix,
9402
make_prev_keypart_map(real_key_parts),
9403
HA_READ_KEY_EXACT)))
9404
DBUG_RETURN(result);
9408
If the min/max argument field is NULL, skip subsequent rows in the same
9409
group with NULL in it. Notice that:
9410
- if the first row in a group doesn't have a NULL in the field, no row
9411
in the same group has (because NULL < any other value),
9412
- min_max_arg_part->field->ptr points to some place in 'record'.
9414
if (min_max_arg_part && min_max_arg_part->field->is_null())
9416
/* Find the first subsequent record without NULL in the MIN/MAX field. */
9417
key_copy(tmp_record, record, index_info, 0);
9418
result= file->index_read_map(record, tmp_record,
9419
make_keypart_map(real_key_parts),
9422
Check if the new record belongs to the current group by comparing its
9423
prefix with the group's prefix. If it is from the next group, then the
9424
whole group has NULLs in the MIN/MAX field, so use the first record in
9425
the group as a result.
9427
It is possible to reuse this new record as the result candidate for the
9428
next call to next_min(), and to save one lookup in the next call. For
9429
this add a new member 'this->next_group_prefix'.
9433
if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
9434
key_restore(record, tmp_record, index_info, 0);
9436
else if (result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE)
9437
result= 0; /* There is a result in any case. */
9442
If the MIN attribute is non-nullable, this->record already contains the
9443
MIN key in the group, so just return.
9445
DBUG_RETURN(result);
9450
Retrieve the maximal key in the next group.
9453
QUICK_GROUP_MIN_MAX_SELECT::next_max()
9456
Lookup the maximal key of the group, and store it into this->record.
9460
HA_ERR_KEY_NOT_FOUND if no MAX key was found that fulfills all conditions.
9461
HA_ERR_END_OF_FILE - "" -
9462
other if some error occurred
9465
int QUICK_GROUP_MIN_MAX_SELECT::next_max()
9469
DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_max");
9471
/* Get the last key in the (possibly extended) group. */
9472
if (min_max_ranges.elements > 0)
9473
result= next_max_in_range();
9475
result= file->index_read_map(record, group_prefix,
9476
make_prev_keypart_map(real_key_parts),
9477
HA_READ_PREFIX_LAST);
9478
DBUG_RETURN(result);
9483
Determine the prefix of the next group.
9486
QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
9489
Determine the prefix of the next group that satisfies the query conditions.
9490
If there is a range condition referencing the group attributes, use a
9491
QUICK_RANGE_SELECT object to retrieve the *first* key that satisfies the
9492
condition. If there is a key infix of constants, append this infix
9493
immediately after the group attributes. The possibly extended prefix is
9494
stored in this->group_prefix. The first key of the found group is stored in
9495
this->record, on which relies this->next_min().
9499
HA_ERR_KEY_NOT_FOUND if there is no key with the formed prefix
9500
HA_ERR_END_OF_FILE if there are no more keys
9501
other if some error occurred
9503
int QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
9506
DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_prefix");
9508
if (quick_prefix_select)
9510
uchar *cur_prefix= seen_first_key ? group_prefix : NULL;
9511
if ((result= quick_prefix_select->get_next_prefix(group_prefix_len,
9512
make_prev_keypart_map(group_key_parts), cur_prefix)))
9513
DBUG_RETURN(result);
9514
seen_first_key= TRUE;
9518
if (!seen_first_key)
9520
result= file->index_first(record);
9522
DBUG_RETURN(result);
9523
seen_first_key= TRUE;
9527
/* Load the first key in this group into record. */
9528
result= file->index_read_map(record, group_prefix,
9529
make_prev_keypart_map(group_key_parts),
9532
DBUG_RETURN(result);
9536
/* Save the prefix of this group for subsequent calls. */
9537
key_copy(group_prefix, record, index_info, group_prefix_len);
9538
/* Append key_infix to group_prefix. */
9539
if (key_infix_len > 0)
9540
memcpy(group_prefix + group_prefix_len,
9541
key_infix, key_infix_len);
9548
Find the minimal key in a group that satisfies some range conditions for the
9549
min/max argument field.
9552
QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
9555
Given the sequence of ranges min_max_ranges, find the minimal key that is
9556
in the left-most possible range. If there is no such key, then the current
9557
group does not have a MIN key that satisfies the WHERE clause. If a key is
9558
found, its value is stored in this->record.
9562
HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
9564
HA_ERR_END_OF_FILE - "" -
9568
int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
9570
ha_rkey_function find_flag;
9571
key_part_map keypart_map;
9572
QUICK_RANGE *cur_range;
9573
bool found_null= FALSE;
9574
int result= HA_ERR_KEY_NOT_FOUND;
9576
DBUG_ASSERT(min_max_ranges.elements > 0);
9578
for (uint range_idx= 0; range_idx < min_max_ranges.elements; range_idx++)
9579
{ /* Search from the left-most range to the right. */
9580
get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx);
9583
If the current value for the min/max argument is bigger than the right
9584
boundary of cur_range, there is no need to check this range.
9586
if (range_idx != 0 && !(cur_range->flag & NO_MAX_RANGE) &&
9587
(key_cmp(min_max_arg_part, (const uchar*) cur_range->max_key,
9588
min_max_arg_len) == 1))
9591
if (cur_range->flag & NO_MIN_RANGE)
9593
keypart_map= make_prev_keypart_map(real_key_parts);
9594
find_flag= HA_READ_KEY_EXACT;
9598
/* Extend the search key with the lower boundary for this range. */
9599
memcpy(group_prefix + real_prefix_len, cur_range->min_key,
9600
cur_range->min_length);
9601
keypart_map= make_keypart_map(real_key_parts);
9602
find_flag= (cur_range->flag & (EQ_RANGE | NULL_RANGE)) ?
9603
HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MIN) ?
9604
HA_READ_AFTER_KEY : HA_READ_KEY_OR_NEXT;
9607
result= file->index_read_map(record, group_prefix, keypart_map, find_flag);
9610
if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
9611
(cur_range->flag & (EQ_RANGE | NULL_RANGE)))
9612
continue; /* Check the next range. */
9615
In all other cases (HA_ERR_*, HA_READ_KEY_EXACT with NO_MIN_RANGE,
9616
HA_READ_AFTER_KEY, HA_READ_KEY_OR_NEXT) if the lookup failed for this
9617
range, it can't succeed for any other subsequent range.
9622
/* A key was found. */
9623
if (cur_range->flag & EQ_RANGE)
9624
break; /* No need to perform the checks below for equal keys. */
9626
if (cur_range->flag & NULL_RANGE)
9629
Remember this key, and continue looking for a non-NULL key that
9630
satisfies some other condition.
9632
memcpy(tmp_record, record, head->s->rec_buff_length);
9637
/* Check if record belongs to the current group. */
9638
if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
9640
result= HA_ERR_KEY_NOT_FOUND;
9644
/* If there is an upper limit, check if the found key is in the range. */
9645
if ( !(cur_range->flag & NO_MAX_RANGE) )
9647
/* Compose the MAX key for the range. */
9648
uchar *max_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
9649
memcpy(max_key, group_prefix, real_prefix_len);
9650
memcpy(max_key + real_prefix_len, cur_range->max_key,
9651
cur_range->max_length);
9652
/* Compare the found key with max_key. */
9653
int cmp_res= key_cmp(index_info->key_part, max_key,
9654
real_prefix_len + min_max_arg_len);
9655
if ((!((cur_range->flag & NEAR_MAX) && (cmp_res == -1)) || (cmp_res <= 0)))
9657
result= HA_ERR_KEY_NOT_FOUND;
9661
/* If we got to this point, the current key qualifies as MIN. */
9662
DBUG_ASSERT(result == 0);
9666
If there was a key with NULL in the MIN/MAX field, and there was no other
9667
key without NULL from the same group that satisfies some other condition,
9668
then use the key with the NULL.
9670
if (found_null && result)
9672
memcpy(record, tmp_record, head->s->rec_buff_length);
9680
Find the maximal key in a group that satisfies some range conditions for the
9681
min/max argument field.
9684
QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
9687
Given the sequence of ranges min_max_ranges, find the maximal key that is
9688
in the right-most possible range. If there is no such key, then the current
9689
group does not have a MAX key that satisfies the WHERE clause. If a key is
9690
found, its value is stored in this->record.
9694
HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
9696
HA_ERR_END_OF_FILE - "" -
9700
int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
9702
ha_rkey_function find_flag;
9703
key_part_map keypart_map;
9704
QUICK_RANGE *cur_range;
9707
DBUG_ASSERT(min_max_ranges.elements > 0);
9709
for (uint range_idx= min_max_ranges.elements; range_idx > 0; range_idx--)
9710
{ /* Search from the right-most range to the left. */
9711
get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx - 1);
9714
If the current value for the min/max argument is smaller than the left
9715
boundary of cur_range, there is no need to check this range.
9717
if (range_idx != min_max_ranges.elements &&
9718
!(cur_range->flag & NO_MIN_RANGE) &&
9719
(key_cmp(min_max_arg_part, (const uchar*) cur_range->min_key,
9720
min_max_arg_len) == -1))
9723
if (cur_range->flag & NO_MAX_RANGE)
9725
keypart_map= make_prev_keypart_map(real_key_parts);
9726
find_flag= HA_READ_PREFIX_LAST;
9730
/* Extend the search key with the upper boundary for this range. */
9731
memcpy(group_prefix + real_prefix_len, cur_range->max_key,
9732
cur_range->max_length);
9733
keypart_map= make_keypart_map(real_key_parts);
9734
find_flag= (cur_range->flag & EQ_RANGE) ?
9735
HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MAX) ?
9736
HA_READ_BEFORE_KEY : HA_READ_PREFIX_LAST_OR_PREV;
9739
result= file->index_read_map(record, group_prefix, keypart_map, find_flag);
9743
if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
9744
(cur_range->flag & EQ_RANGE))
9745
continue; /* Check the next range. */
9748
In no key was found with this upper bound, there certainly are no keys
9749
in the ranges to the left.
9753
/* A key was found. */
9754
if (cur_range->flag & EQ_RANGE)
9755
return 0; /* No need to perform the checks below for equal keys. */
9757
/* Check if record belongs to the current group. */
9758
if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
9759
continue; // Row not found
9761
/* If there is a lower limit, check if the found key is in the range. */
9762
if ( !(cur_range->flag & NO_MIN_RANGE) )
9764
/* Compose the MIN key for the range. */
9765
uchar *min_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
9766
memcpy(min_key, group_prefix, real_prefix_len);
9767
memcpy(min_key + real_prefix_len, cur_range->min_key,
9768
cur_range->min_length);
9769
/* Compare the found key with min_key. */
9770
int cmp_res= key_cmp(index_info->key_part, min_key,
9771
real_prefix_len + min_max_arg_len);
9772
if ((!((cur_range->flag & NEAR_MIN) && (cmp_res == 1)) ||
9776
/* If we got to this point, the current key qualifies as MAX. */
9779
return HA_ERR_KEY_NOT_FOUND;
9784
Update all MIN function results with the newly found value.
9787
QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
9790
The method iterates through all MIN functions and updates the result value
9791
of each function by calling Item_sum::reset(), which in turn picks the new
9792
result value from this->head->record[0], previously updated by
9793
next_min(). The updated value is stored in a member variable of each of the
9794
Item_sum objects, depending on the value type.
9797
The update must be done separately for MIN and MAX, immediately after
9798
next_min() was called and before next_max() is called, because both MIN and
9799
MAX take their result value from the same buffer this->head->record[0]
9800
(i.e. this->record).
9806
void QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
9810
min_functions_it->rewind();
9811
while ((min_func= (*min_functions_it)++))
9817
Update all MAX function results with the newly found value.
9820
QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
9823
The method iterates through all MAX functions and updates the result value
9824
of each function by calling Item_sum::reset(), which in turn picks the new
9825
result value from this->head->record[0], previously updated by
9826
next_max(). The updated value is stored in a member variable of each of the
9827
Item_sum objects, depending on the value type.
9830
The update must be done separately for MIN and MAX, immediately after
9831
next_max() was called, because both MIN and MAX take their result value
9832
from the same buffer this->head->record[0] (i.e. this->record).
9838
void QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
9842
max_functions_it->rewind();
9843
while ((max_func= (*max_functions_it)++))
9849
Append comma-separated list of keys this quick select uses to key_names;
9850
append comma-separated list of corresponding used lengths to used_lengths.
9853
QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths()
9854
key_names [out] Names of used indexes
9855
used_lengths [out] Corresponding lengths of the index names
9858
This method is used by select_describe to extract the names of the
9859
indexes used by a quick select.
9863
void QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths(String *key_names,
9864
String *used_lengths)
9868
key_names->append(index_info->name);
9869
length= longlong2str(max_used_key_length, buf, 10) - buf;
9870
used_lengths->append(buf, length);
9876
static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
9879
SEL_ARG **key,**end;
9882
DBUG_ENTER("print_sel_tree");
9884
String tmp(buff,sizeof(buff),&my_charset_bin);
9886
for (idx= 0,key=tree->keys, end=key+param->keys ;
9890
if (tree_map->is_set(idx))
9892
uint keynr= param->real_keynr[idx];
9895
tmp.append(param->table->key_info[keynr].name);
9899
tmp.append(STRING_WITH_LEN("(empty)"));
9901
DBUG_PRINT("info", ("SEL_TREE: 0x%lx (%s) scans: %s", (long) tree, msg, tmp.ptr()));
9907
static void print_ror_scans_arr(TABLE *table, const char *msg,
9908
struct st_ror_scan_info **start,
9909
struct st_ror_scan_info **end)
9911
DBUG_ENTER("print_ror_scans_arr");
9914
String tmp(buff,sizeof(buff),&my_charset_bin);
9916
for (;start != end; start++)
9920
tmp.append(table->key_info[(*start)->keynr].name);
9923
tmp.append(STRING_WITH_LEN("(empty)"));
9924
DBUG_PRINT("info", ("ROR key scans (%s): %s", msg, tmp.ptr()));
9929
/*****************************************************************************
9930
** Print a quick range for debugging
9932
** This should be changed to use a String to store each row instead
9933
** of locking the DEBUG stream !
9934
*****************************************************************************/
9937
print_key(KEY_PART *key_part, const uchar *key, uint used_length)
9940
const uchar *key_end= key+used_length;
9941
String tmp(buff,sizeof(buff),&my_charset_bin);
9943
TABLE *table= key_part->field->table;
9944
my_bitmap_map *old_write_set, *old_read_set;
9945
old_write_set= dbug_tmp_use_all_columns(table, table->write_set);
9946
old_read_set= dbug_tmp_use_all_columns(table, table->read_set);
9948
for (; key < key_end; key+=store_length, key_part++)
9950
Field *field= key_part->field;
9951
store_length= key_part->store_length;
9953
if (field->real_maybe_null())
9957
fwrite("NULL",sizeof(char),4,DBUG_FILE);
9960
key++; // Skip null byte
9963
field->set_key_image(key, key_part->length);
9964
if (field->type() == MYSQL_TYPE_BIT)
9965
(void) field->val_int_as_str(&tmp, 1);
9967
field->val_str(&tmp);
9968
fwrite(tmp.ptr(),sizeof(char),tmp.length(),DBUG_FILE);
9969
if (key+store_length < key_end)
9970
fputc('/',DBUG_FILE);
9972
dbug_tmp_restore_column_map(table->write_set, old_write_set);
9973
dbug_tmp_restore_column_map(table->read_set, old_read_set);
9977
static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg)
9979
char buf[MAX_KEY/8+1];
9981
my_bitmap_map *old_read_map, *old_write_map;
9982
DBUG_ENTER("print_quick");
9988
old_read_map= dbug_tmp_use_all_columns(table, table->read_set);
9989
old_write_map= dbug_tmp_use_all_columns(table, table->write_set);
9990
quick->dbug_dump(0, TRUE);
9991
dbug_tmp_restore_column_map(table->read_set, old_read_map);
9992
dbug_tmp_restore_column_map(table->write_set, old_write_map);
9994
fprintf(DBUG_FILE,"other_keys: 0x%s:\n", needed_reg->print(buf));
10001
void QUICK_RANGE_SELECT::dbug_dump(int indent, bool verbose)
10003
/* purecov: begin inspected */
10004
fprintf(DBUG_FILE, "%*squick range select, key %s, length: %d\n",
10005
indent, "", head->key_info[index].name, max_used_key_length);
10009
QUICK_RANGE *range;
10010
QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
10011
QUICK_RANGE **end_range= pr + ranges.elements;
10012
for (; pr != end_range; ++pr)
10014
fprintf(DBUG_FILE, "%*s", indent + 2, "");
10016
if (!(range->flag & NO_MIN_RANGE))
10018
print_key(key_parts, range->min_key, range->min_length);
10019
if (range->flag & NEAR_MIN)
10020
fputs(" < ",DBUG_FILE);
10022
fputs(" <= ",DBUG_FILE);
10024
fputs("X",DBUG_FILE);
10026
if (!(range->flag & NO_MAX_RANGE))
10028
if (range->flag & NEAR_MAX)
10029
fputs(" < ",DBUG_FILE);
10031
fputs(" <= ",DBUG_FILE);
10032
print_key(key_parts, range->max_key, range->max_length);
10034
fputs("\n",DBUG_FILE);
10040
void QUICK_INDEX_MERGE_SELECT::dbug_dump(int indent, bool verbose)
10042
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
10043
QUICK_RANGE_SELECT *quick;
10044
fprintf(DBUG_FILE, "%*squick index_merge select\n", indent, "");
10045
fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
10046
while ((quick= it++))
10047
quick->dbug_dump(indent+2, verbose);
10048
if (pk_quick_select)
10050
fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
10051
pk_quick_select->dbug_dump(indent+2, verbose);
10053
fprintf(DBUG_FILE, "%*s}\n", indent, "");
10056
void QUICK_ROR_INTERSECT_SELECT::dbug_dump(int indent, bool verbose)
10058
List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
10059
QUICK_RANGE_SELECT *quick;
10060
fprintf(DBUG_FILE, "%*squick ROR-intersect select, %scovering\n",
10061
indent, "", need_to_fetch_row? "":"non-");
10062
fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
10063
while ((quick= it++))
10064
quick->dbug_dump(indent+2, verbose);
10067
fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
10068
cpk_quick->dbug_dump(indent+2, verbose);
10070
fprintf(DBUG_FILE, "%*s}\n", indent, "");
10073
void QUICK_ROR_UNION_SELECT::dbug_dump(int indent, bool verbose)
10075
List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
10076
QUICK_SELECT_I *quick;
10077
fprintf(DBUG_FILE, "%*squick ROR-union select\n", indent, "");
10078
fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
10079
while ((quick= it++))
10080
quick->dbug_dump(indent+2, verbose);
10081
fprintf(DBUG_FILE, "%*s}\n", indent, "");
10086
Print quick select information to DBUG_FILE.
10089
QUICK_GROUP_MIN_MAX_SELECT::dbug_dump()
10090
indent Indentation offset
10091
verbose If TRUE show more detailed output.
10094
Print the contents of this quick select to DBUG_FILE. The method also
10095
calls dbug_dump() for the used quick select if any.
10098
Caller is responsible for locking DBUG_FILE before this call and unlocking
10105
void QUICK_GROUP_MIN_MAX_SELECT::dbug_dump(int indent, bool verbose)
10108
"%*squick_group_min_max_select: index %s (%d), length: %d\n",
10109
indent, "", index_info->name, index, max_used_key_length);
10110
if (key_infix_len > 0)
10112
fprintf(DBUG_FILE, "%*susing key_infix with length %d:\n",
10113
indent, "", key_infix_len);
10115
if (quick_prefix_select)
10117
fprintf(DBUG_FILE, "%*susing quick_range_select:\n", indent, "");
10118
quick_prefix_select->dbug_dump(indent + 2, verbose);
10120
if (min_max_ranges.elements > 0)
10122
fprintf(DBUG_FILE, "%*susing %d quick_ranges for MIN/MAX:\n",
10123
indent, "", min_max_ranges.elements);
10128
#endif /* NOT_USED */
10130
/*****************************************************************************
10131
** Instantiate templates
10132
*****************************************************************************/
10134
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
10135
template class List<QUICK_RANGE>;
10136
template class List_iterator<QUICK_RANGE>;