259
276
* Returns true if any index used by this quick select
260
277
* uses field which is marked in passed bitmap.
262
virtual bool is_keys_used(const boost::dynamic_bitset<>& fields);
279
virtual bool is_keys_used(const MY_BITMAP *fields);
265
282
struct st_qsel_param;
267
class QuickRangeSelect;
270
* MRR range sequence, array<QuickRange> implementation: sequence traversal
287
* MRR range sequence, array<QUICK_RANGE> implementation: sequence traversal
273
290
typedef struct st_quick_range_seq_ctx
278
} QuickRangeSequenceContext;
295
} QUICK_RANGE_SEQ_CTX;
280
297
range_seq_t quick_range_seq_init(void *init_param, uint32_t n_ranges, uint32_t flags);
282
298
uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
301
* Quick select that does a range scan on a single key.
303
* The records are returned in key order.
306
class QUICK_RANGE_SELECT : public QUICK_SELECT_I
310
DYNAMIC_ARRAY ranges; /**< ordered array of range ptrs */
312
/** Members to deal with case when this quick select is a ROR-merged scan */
313
bool in_ror_merged_scan;
314
MY_BITMAP column_bitmap;
315
MY_BITMAP *save_read_set;
316
MY_BITMAP *save_write_set;
317
bool free_file; /**< True when this->file is "owned" by this quick select */
319
/* Range pointers to be used when not using MRR interface */
320
QUICK_RANGE **cur_range; /**< current element in ranges */
321
QUICK_RANGE *last_range;
323
/** Members needed to use the MRR interface */
324
QUICK_RANGE_SEQ_CTX qr_traversal_ctx;
325
uint32_t mrr_buf_size; /**< copy from session->variables.read_rnd_buff_size */
326
HANDLER_BUFFER *mrr_buf_desc; /**< the handler buffer */
328
/** Info about index we're scanning */
330
KEY_PART_INFO *key_part_info;
332
bool dont_free; /**< Used by QUICK_SELECT_DESC */
334
int cmp_next(QUICK_RANGE *range);
335
int cmp_prev(QUICK_RANGE *range);
336
bool row_in_ranges();
338
uint32_t mrr_flags; /**< Flags to be used with MRR interface */
341
QUICK_RANGE_SELECT(Session *session,
345
MEM_ROOT *parent_alloc,
347
~QUICK_RANGE_SELECT();
353
int get_next_prefix(uint32_t prefix_length,
354
key_part_map keypart_map,
355
unsigned char *cur_prefix);
356
bool reverse_sorted()
360
bool unique_key_range();
361
int init_ror_merged_scan(bool reuse_handler);
362
void save_last_pos();
365
return QS_TYPE_RANGE;
367
void add_keys_and_lengths(String *key_names, String *used_lengths);
368
void add_info_string(String *str);
370
/* Used only by QUICK_SELECT_DESC */
371
QUICK_RANGE_SELECT(const QUICK_RANGE_SELECT& org) : QUICK_SELECT_I()
373
memmove(this, &org, sizeof(*this));
375
Use default MRR implementation for reverse scans. No table engine
376
currently can do an MRR scan with output in reverse index order.
379
mrr_flags|= HA_MRR_USE_DEFAULT_IMPL;
382
friend class TRP_ROR_INTERSECT;
384
QUICK_RANGE_SELECT *get_quick_select_for_ref(Session *session, Table *table,
385
struct table_reference_st *ref,
387
friend bool get_quick_keys(PARAM *param, QUICK_RANGE_SELECT *quick,
388
KEY_PART *key, SEL_ARG *key_tree,
389
unsigned char *min_key, uint32_t min_key_flag,
390
unsigned char *max_key, uint32_t max_key_flag);
391
friend QUICK_RANGE_SELECT *get_quick_select(PARAM*,uint32_t idx,
394
uint32_t mrr_buf_size,
396
friend class QUICK_SELECT_DESC;
397
friend class QUICK_INDEX_MERGE_SELECT;
398
friend class QUICK_ROR_INTERSECT_SELECT;
399
friend class QUICK_GROUP_MIN_MAX_SELECT;
400
friend uint32_t quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range);
401
friend range_seq_t quick_range_seq_init(void *init_param,
402
uint32_t n_ranges, uint32_t flags);
403
friend void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
404
bool distinct,const char *message);
408
QUICK_INDEX_MERGE_SELECT - index_merge access method quick select.
410
QUICK_INDEX_MERGE_SELECT uses
411
* QUICK_RANGE_SELECTs to get rows
412
* Unique class to remove duplicate rows
414
INDEX MERGE OPTIMIZER
415
Current implementation doesn't detect all cases where index_merge could
416
be used, in particular:
417
* index_merge will never be used if range scan is possible (even if
418
range scan is more expensive)
420
* index_merge+'using index' is not supported (this the consequence of
421
the above restriction)
423
* If WHERE part contains complex nested AND and OR conditions, some ways
424
to retrieve rows using index_merge will not be considered. The choice
425
of read plan may depend on the order of conjuncts/disjuncts in WHERE
426
part of the query, see comments near imerge_list_or_list and
427
SEL_IMERGE::or_sel_tree_with_checks functions for details.
429
* There is no "index_merge_ref" method (but index_merge on non-first
430
table in join is possible with 'range checked for each record').
432
See comments around SEL_IMERGE class and test_quick_select for more
435
ROW RETRIEVAL ALGORITHM
437
index_merge uses Unique class for duplicates removal. index_merge takes
438
advantage of Clustered Primary Key (CPK) if the table has one.
439
The index_merge algorithm consists of two phases:
441
Phase 1 (implemented in QUICK_INDEX_MERGE_SELECT::prepare_unique):
444
activate 'index only';
445
while(retrieve next row for non-CPK scan)
447
if (there is a CPK scan and row will be retrieved by it)
450
put its rowid into Unique;
452
deactivate 'index only';
455
Phase 2 (implemented as sequence of QUICK_INDEX_MERGE_SELECT::get_next
460
retrieve all rows from row pointers stored in Unique;
462
retrieve all rows for CPK scan;
465
class QUICK_INDEX_MERGE_SELECT : public QUICK_SELECT_I
468
QUICK_INDEX_MERGE_SELECT(Session *session, Table *table);
469
~QUICK_INDEX_MERGE_SELECT();
474
bool reverse_sorted()
478
bool unique_key_range()
484
return QS_TYPE_INDEX_MERGE;
486
void add_keys_and_lengths(String *key_names, String *used_lengths);
487
void add_info_string(String *str);
488
bool is_keys_used(const MY_BITMAP *fields);
490
bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
492
/* range quick selects this index_merge read consists of */
493
List<QUICK_RANGE_SELECT> quick_selects;
495
/* quick select that uses clustered primary key (NULL if none) */
496
QUICK_RANGE_SELECT* pk_quick_select;
498
/* true if this select is currently doing a clustered PK scan */
503
int read_keys_and_merge();
505
/* used to get rows collected in Unique */
506
READ_RECORD read_record;
511
Rowid-Ordered Retrieval (ROR) index intersection quick select.
512
This quick select produces intersection of row sequences returned
513
by several QUICK_RANGE_SELECTs it "merges".
515
All merged QUICK_RANGE_SELECTs must return rowids in rowid order.
516
QUICK_ROR_INTERSECT_SELECT will return rows in rowid order, too.
518
All merged quick selects retrieve {rowid, covered_fields} tuples (not full
520
QUICK_ROR_INTERSECT_SELECT retrieves full records if it is not being used
521
by QUICK_ROR_INTERSECT_SELECT and all merged quick selects together don't
522
cover needed all fields.
524
If one of the merged quick selects is a Clustered PK range scan, it is
525
used only to filter rowid sequence produced by other merged quick selects.
527
class QUICK_ROR_INTERSECT_SELECT : public QUICK_SELECT_I
530
QUICK_ROR_INTERSECT_SELECT(Session *session, Table *table,
531
bool retrieve_full_rows,
532
MEM_ROOT *parent_alloc);
533
~QUICK_ROR_INTERSECT_SELECT();
538
bool reverse_sorted()
542
bool unique_key_range()
548
return QS_TYPE_ROR_INTERSECT;
550
void add_keys_and_lengths(String *key_names, String *used_lengths);
551
void add_info_string(String *str);
552
bool is_keys_used(const MY_BITMAP *fields);
553
int init_ror_merged_scan(bool reuse_handler);
554
bool push_quick_back(QUICK_RANGE_SELECT *quick_sel_range);
557
* Range quick selects this intersection consists of, not including
560
List<QUICK_RANGE_SELECT> quick_selects;
563
* Merged quick select that uses Clustered PK, if there is one. This quick
564
* select is not used for row retrieval, it is used for row retrieval.
566
QUICK_RANGE_SELECT *cpk_quick;
568
MEM_ROOT alloc; /**< Memory pool for this and merged quick selects data. */
569
Session *session; /**< Pointer to the current session */
570
bool need_to_fetch_row; /**< if true, do retrieve full table records. */
571
/** in top-level quick select, true if merged scans where initialized */
577
* This function object is defined in drizzled/opt_range.cc
578
* We need this here for the priority_queue definition in the
579
* QUICK_ROR_UNION_SELECT class.
581
class compare_functor;
584
Rowid-Ordered Retrieval index union select.
585
This quick select produces union of row sequences returned by several
586
quick select it "merges".
588
All merged quick selects must return rowids in rowid order.
589
QUICK_ROR_UNION_SELECT will return rows in rowid order, too.
591
All merged quick selects are set not to retrieve full table records.
592
ROR-union quick select always retrieves full records.
595
class QUICK_ROR_UNION_SELECT : public QUICK_SELECT_I
598
QUICK_ROR_UNION_SELECT(Session *session, Table *table);
599
~QUICK_ROR_UNION_SELECT();
604
bool reverse_sorted()
608
bool unique_key_range()
614
return QS_TYPE_ROR_UNION;
616
void add_keys_and_lengths(String *key_names, String *used_lengths);
617
void add_info_string(String *str);
618
bool is_keys_used(const MY_BITMAP *fields);
620
bool push_quick_back(QUICK_SELECT_I *quick_sel_range);
622
List<QUICK_SELECT_I> quick_selects; /**< Merged quick selects */
624
/** Priority queue for merge operation */
625
std::priority_queue<QUICK_SELECT_I *, std::vector<QUICK_SELECT_I *>, compare_functor > *queue;
626
MEM_ROOT alloc; /**< Memory pool for this and merged quick selects data. */
628
Session *session; /**< current thread */
629
unsigned char *cur_rowid; /**< buffer used in get_next() */
630
unsigned char *prev_rowid; /**< rowid of last row returned by get_next() */
631
bool have_prev_rowid; /**< true if prev_rowid has valid data */
632
uint32_t rowid_length; /**< table rowid length */
638
Index scan for GROUP-BY queries with MIN/MAX aggregate functions.
640
This class provides a specialized index access method for GROUP-BY queries
643
SELECT A_1,...,A_k, [B_1,...,B_m], [MIN(C)], [MAX(C)]
645
WHERE [RNG(A_1,...,A_p ; where p <= k)]
646
[AND EQ(B_1,...,B_m)]
648
[AND PA(A_i1,...,A_iq)]
649
GROUP BY A_1,...,A_k;
653
SELECT DISTINCT A_i1,...,A_ik
655
WHERE [RNG(A_1,...,A_p ; where p <= k)]
656
[AND PA(A_i1,...,A_iq)];
658
where all selected fields are parts of the same index.
659
The class of queries that can be processed by this quick select is fully
660
specified in the description of get_best_trp_group_min_max() in opt_range.cc.
662
The get_next() method directly produces result tuples, thus obviating the
663
need to call end_send_group() because all grouping is already done inside
666
Since one of the requirements is that all select fields are part of the same
667
index, this class produces only index keys, and not complete records.
669
class QUICK_GROUP_MIN_MAX_SELECT : public QUICK_SELECT_I
672
handler *file; /**< The handler used to get data. */
673
JOIN *join; /**< Descriptor of the current query */
674
KEY *index_info; /**< The index chosen for data access */
675
unsigned char *record; /**< Buffer where the next record is returned. */
676
unsigned char *tmp_record; /**< Temporary storage for next_min(), next_max(). */
677
unsigned char *group_prefix; /**< Key prefix consisting of the GROUP fields. */
678
uint32_t group_prefix_len; /**< Length of the group prefix. */
679
uint32_t group_key_parts; /**< A number of keyparts in the group prefix */
680
unsigned char *last_prefix; /**< Prefix of the last group for detecting EOF. */
681
bool have_min; /**< Specify whether we are computing */
682
bool have_max; /**< a MIN, a MAX, or both. */
683
bool seen_first_key; /**< Denotes whether the first key was retrieved.*/
684
KEY_PART_INFO *min_max_arg_part; /** The keypart of the only argument field of all MIN/MAX functions. */
685
uint32_t min_max_arg_len; /**< The length of the MIN/MAX argument field */
686
unsigned char *key_infix; /**< Infix of constants from equality predicates. */
687
uint32_t key_infix_len;
688
DYNAMIC_ARRAY min_max_ranges; /**< Array of range ptrs for the MIN/MAX field. */
689
uint32_t real_prefix_len; /**< Length of key prefix extended with key_infix. */
690
uint32_t real_key_parts; /**< A number of keyparts in the above value. */
691
List<Item_sum> *min_functions;
692
List<Item_sum> *max_functions;
693
List_iterator<Item_sum> *min_functions_it;
694
List_iterator<Item_sum> *max_functions_it;
697
The following two members are public to allow easy access from
698
TRP_GROUP_MIN_MAX::make_quick()
700
MEM_ROOT alloc; /**< Memory pool for this and quick_prefix_select data. */
701
QUICK_RANGE_SELECT *quick_prefix_select; /**< For retrieval of group prefixes. */
704
int next_min_in_range();
705
int next_max_in_range();
708
void update_min_result();
709
void update_max_result();
711
QUICK_GROUP_MIN_MAX_SELECT(Table *table, JOIN *join, bool have_min,
712
bool have_max, KEY_PART_INFO *min_max_arg_part,
713
uint32_t group_prefix_len, uint32_t group_key_parts,
714
uint32_t used_key_parts, KEY *index_info, uint
715
use_index, double read_cost, ha_rows records, uint
716
key_infix_len, unsigned char *key_infix, MEM_ROOT
718
~QUICK_GROUP_MIN_MAX_SELECT();
719
bool add_range(SEL_ARG *sel_range);
720
void update_key_stat();
721
void adjust_prefix_ranges();
722
bool alloc_buffers();
726
bool reverse_sorted()
730
bool unique_key_range()
736
return QS_TYPE_GROUP_MIN_MAX;
738
void add_keys_and_lengths(String *key_names, String *used_lengths);
741
class QUICK_SELECT_DESC: public QUICK_RANGE_SELECT
744
QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q, uint32_t used_key_parts,
747
bool reverse_sorted() { return 1; }
748
int get_type() { return QS_TYPE_RANGE_DESC; }
750
bool range_reads_after_key(QUICK_RANGE *range);
751
int reset(void) { rev_it.rewind(); return QUICK_RANGE_SELECT::reset(); }
752
List<QUICK_RANGE> rev_ranges;
753
List_iterator<QUICK_RANGE> rev_it;
285
757
* Executor class for SELECT statements.
289
* The QuickSelectInterface member variable is the implementor
761
* The QUICK_SELECT_I member variable is the implementor
290
762
* of the SELECT execution.
292
class SqlSelect : public memory::SqlAlloc
764
class SQL_SELECT :public Sql_alloc {
295
QuickSelectInterface *quick; /**< If quick-select used */
766
QUICK_SELECT_I *quick; /**< If quick-select used */
296
767
COND *cond; /**< where condition */
298
internal::IO_CACHE *file; /**< Positions to used records */
769
IO_CACHE file; /**< Positions to used records */
299
770
ha_rows records; /**< Records in use if read from file */
300
771
double read_time; /**< Time to read rows */
301
772
key_map quick_keys; /**< Possible quick keys */