~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/sel_tree.h

  • Committer: Padraig O'Sullivan
  • Date: 2009-12-17 04:48:59 UTC
  • mto: This revision was merged to the branch mainline in revision 1246.
  • Revision ID: osullivan.padraig@gmail.com-20091217044859-qorpkp4911zypfv3
Added some dtrace probes for tracing the optimizer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
#ifndef DRIZZLED_OPTIMIZER_SEL_TREE_H
21
 
#define DRIZZLED_OPTIMIZER_SEL_TREE_H
22
 
 
23
 
#include "drizzled/memory/sql_alloc.h"
24
 
 
25
 
namespace drizzled
26
 
{
27
 
 
28
 
namespace optimizer
29
 
{
30
 
 
31
 
class RangeParameter;
32
 
class SEL_IMERGE;
33
 
class SEL_ARG;
34
 
class RorScanInfo;
35
 
 
36
 
class SEL_TREE : public drizzled::memory::SqlAlloc
37
 
{
38
 
public:
39
 
  /*
40
 
    Starting an effort to document this field:
41
 
    (for some i, keys[i]->type == optimizer::SEL_ARG::IMPOSSIBLE) =>
42
 
       (type == SEL_TREE::IMPOSSIBLE)
43
 
  */
44
 
  enum Type
45
 
  {
46
 
    IMPOSSIBLE,
47
 
    ALWAYS,
48
 
    MAYBE,
49
 
    KEY,
50
 
    KEY_SMALLER
51
 
  } type;
52
 
 
53
 
  SEL_TREE(enum Type type_arg) :type(type_arg) {}
54
 
  SEL_TREE() :type(KEY)
55
 
  {
56
 
    keys_map.reset();
57
 
    memset(keys, 0, sizeof(keys));
58
 
  }
59
 
  /*
60
 
    Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
61
 
    keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
62
 
    merit in range analyzer functions (e.g. get_mm_parts) returning a
63
 
    pointer to such SEL_TREE instead of NULL)
64
 
  */
65
 
  SEL_ARG *keys[MAX_KEY];
66
 
  key_map keys_map;        /* bitmask of non-NULL elements in keys */
67
 
 
68
 
  /*
69
 
    Possible ways to read rows using index_merge. The list is non-empty only
70
 
    if type==KEY. Currently can be non empty only if keys_map.none().
71
 
  */
72
 
  List<SEL_IMERGE> merges;
73
 
 
74
 
  /* The members below are filled/used only after get_mm_tree is done */
75
 
  key_map ror_scans_map;   /* bitmask of ROR scan-able elements in keys */
76
 
  uint32_t n_ror_scans;     /* number of set bits in ror_scans_map */
77
 
 
78
 
  RorScanInfo **ror_scans;     /* list of ROR key scans */
79
 
  RorScanInfo **ror_scans_end; /* last ROR scan */
80
 
  /* Note that #records for each key scan is stored in table->quick_rows */
81
 
 
82
 
};
83
 
 
84
 
/*
85
 
  Check if two optimizer::SEL_TREES can be combined into one (i.e. a single key range
86
 
  read can be constructed for "cond_of_tree1 OR cond_of_tree2" ) without
87
 
  using index_merge.
88
 
*/
89
 
bool sel_trees_can_be_ored(SEL_TREE *tree1, 
90
 
                           SEL_TREE *tree2,
91
 
                           RangeParameter* param);
92
 
 
93
 
SEL_TREE *
94
 
tree_or(RangeParameter *param, SEL_TREE *tree1, SEL_TREE *tree2);
95
 
 
96
 
/*
97
 
   Remove the trees that are not suitable for record retrieval.
98
 
   SYNOPSIS
99
 
   param  Range analysis parameter
100
 
   tree   Tree to be processed, tree->type is KEY or KEY_SMALLER
101
 
 
102
 
   DESCRIPTION
103
 
   This function walks through tree->keys[] and removes the SEL_ARG* trees
104
 
   that are not "maybe" trees (*) and cannot be used to construct quick range
105
 
   selects.
106
 
   (*) - have type MAYBE or MAYBE_KEY. Perhaps we should remove trees of
107
 
   these types here as well.
108
 
 
109
 
   A SEL_ARG* tree cannot be used to construct quick select if it has
110
 
   tree->part != 0. (e.g. it could represent "keypart2 < const").
111
 
 
112
 
   WHY THIS FUNCTION IS NEEDED
113
 
 
114
 
   Normally we allow construction of optimizer::SEL_TREE objects that have SEL_ARG
115
 
   trees that do not allow quick range select construction. For example for
116
 
   " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
117
 
   tree1= optimizer::SEL_TREE { SEL_ARG{keypart1=1} }
118
 
   tree2= optimizer::SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
119
 
   from this
120
 
   call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
121
 
   tree.
122
 
 
123
 
   There is an exception though: when we construct index_merge optimizer::SEL_TREE,
124
 
   any SEL_ARG* tree that cannot be used to construct quick range select can
125
 
   be removed, because current range analysis code doesn't provide any way
126
 
   that tree could be later combined with another tree.
127
 
   Consider an example: we should not construct
128
 
   st1 = optimizer::SEL_TREE {
129
 
   merges = SEL_IMERGE {
130
 
   optimizer::SEL_TREE(t.key1part1 = 1),
131
 
   optimizer::SEL_TREE(t.key2part2 = 2)   -- (*)
132
 
   }
133
 
   };
134
 
   because
135
 
   - (*) cannot be used to construct quick range select,
136
 
   - There is no execution path that would cause (*) to be converted to
137
 
   a tree that could be used.
138
 
 
139
 
   The latter is easy to verify: first, notice that the only way to convert
140
 
   (*) into a usable tree is to call tree_and(something, (*)).
141
 
 
142
 
   Second look at what tree_and/tree_or function would do when passed a
143
 
   optimizer::SEL_TREE that has the structure like st1 tree has, and conlcude that
144
 
   tree_and(something, (*)) will not be called.
145
 
 
146
 
   RETURN
147
 
   0  Ok, some suitable trees left
148
 
   1  No tree->keys[] left.
149
 
 */
150
 
bool remove_nonrange_trees(RangeParameter *param, SEL_TREE *tree);
151
 
 
152
 
} /* namespace optimizer */
153
 
 
154
 
} /* namespace drizzled */
155
 
 
156
 
#endif /* DRIZZLED_OPTIMIZER_SEL_TREE_H */