~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/access_method/scan.cc

  • Committer: Olaf van der Spek
  • Date: 2011-06-25 13:36:24 UTC
  • mto: This revision was merged to the branch mainline in revision 2349.
  • Revision ID: olafvdspek@gmail.com-20110625133624-hzy2ordecn161qco
Refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
 
21
21
#include <config.h>
22
 
#include <drizzled/session.h>
23
22
#include <drizzled/join_table.h>
24
23
#include <drizzled/table.h>
25
24
#include <drizzled/sql_select.h>
27
26
#include <drizzled/optimizer/access_method/scan.h>
28
27
#include <drizzled/util/test.h>
29
28
#include <drizzled/statistics_variables.h>
 
29
#include <drizzled/session.h>
30
30
 
31
31
using namespace drizzled;
32
32
 
33
33
static uint32_t make_join_orderinfo(Join *join);
34
34
 
35
 
bool optimizer::Scan::getStats(Table *table,
36
 
                               JoinTable *join_tab)
 
35
void optimizer::Scan::getStats(Table& table, JoinTable& join_tab)
37
36
{
38
 
  Join *join= join_tab->join;
 
37
  Join *join= join_tab.join;
39
38
  bool statistics= test(! (join->select_options & SELECT_DESCRIBE));
40
39
  uint64_t options= (join->select_options &
41
40
                     (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) |
42
41
                     (0);
43
42
  uint32_t no_jbuf_after= make_join_orderinfo(join);
44
 
  uint32_t index= join_tab - join->join_tab;
 
43
  uint32_t index= &join_tab - join->join_tab;
45
44
 
46
45
  /*
47
46
   * If previous table use cache
48
47
   * If the incoming data set is already sorted don't use cache.
49
48
   */
50
 
  table->status= STATUS_NO_RECORD;
 
49
  table.status= STATUS_NO_RECORD;
51
50
 
52
51
  if (index != join->const_tables && 
53
52
      ! (options & SELECT_NO_JOIN_CACHE) &&
54
 
      join_tab->use_quick != 2 && 
55
 
      ! join_tab->first_inner && 
 
53
      join_tab.use_quick != 2 && 
 
54
      ! join_tab.first_inner && 
56
55
      index <= no_jbuf_after &&
57
 
      ! join_tab->insideout_match_tab)
 
56
      ! join_tab.insideout_match_tab)
58
57
  {
59
58
    if ((options & SELECT_DESCRIBE) ||
60
59
        ! join_init_cache(join->session,
61
60
                          join->join_tab + join->const_tables,
62
61
                          index - join->const_tables))
63
62
    {
64
 
      join_tab[-1].next_select= sub_select_cache; /* Patch previous */
 
63
      (&join_tab)[-1].next_select= sub_select_cache; /* Patch previous */
65
64
    }
66
65
  }
67
66
 
68
67
  /* These init changes read_record */
69
 
  if (join_tab->use_quick == 2)
 
68
  if (join_tab.use_quick == 2)
70
69
  {
71
70
    join->session->server_status|= SERVER_QUERY_NO_GOOD_INDEX_USED;
72
 
    join_tab->read_first_record= join_init_quick_read_record;
 
71
    join_tab.read_first_record= join_init_quick_read_record;
73
72
    if (statistics)
74
73
    {
75
74
      join->session->status_var.select_range_check_count++;
77
76
  }
78
77
  else
79
78
  {
80
 
    join_tab->read_first_record= join_init_read_record;
 
79
    join_tab.read_first_record= join_init_read_record;
81
80
    if (index == join->const_tables)
82
81
    {
83
 
      if (join_tab->select && join_tab->select->quick)
 
82
      if (join_tab.select && join_tab.select->quick)
84
83
      {
85
84
        if (statistics)
86
 
        {
87
85
          join->session->status_var.select_range_count++;
88
 
        }
89
86
      }
90
87
      else
91
88
      {
92
89
        join->session->server_status|= SERVER_QUERY_NO_INDEX_USED;
93
90
        if (statistics)
94
 
        {
95
91
          join->session->status_var.select_scan_count++;
96
 
        }
97
92
      }
98
93
    }
99
94
    else
100
95
    {
101
 
      if (join_tab->select && join_tab->select->quick)
 
96
      if (join_tab.select && join_tab.select->quick)
102
97
      {
103
98
        if (statistics)
104
 
        {
105
99
          join->session->status_var.select_full_range_join_count++;
106
 
        }
107
100
      }
108
101
      else
109
102
      {
110
103
        join->session->server_status|= SERVER_QUERY_NO_INDEX_USED;
111
104
        if (statistics)
112
 
        {
113
105
          join->session->status_var.select_full_join_count++;
114
 
        }
115
106
      }
116
107
    }
117
 
    if (! table->no_keyread)
 
108
    if (! table.no_keyread)
118
109
    {
119
 
      if (join_tab->select && 
120
 
          join_tab->select->quick &&
121
 
          join_tab->select->quick->index != MAX_KEY && //not index_merge
122
 
          table->covering_keys.test(join_tab->select->quick->index))
 
110
      if (join_tab.select && 
 
111
          join_tab.select->quick &&
 
112
          join_tab.select->quick->index != MAX_KEY && //not index_merge
 
113
          table.covering_keys.test(join_tab.select->quick->index))
123
114
      {
124
 
        table->key_read= 1;
125
 
        table->cursor->extra(HA_EXTRA_KEYREAD);
 
115
        table.key_read= 1;
 
116
        table.cursor->extra(HA_EXTRA_KEYREAD);
126
117
      }
127
 
      else if (! table->covering_keys.none() &&
128
 
               ! (join_tab->select && join_tab->select->quick))
 
118
      else if (! table.covering_keys.none() && ! (join_tab.select && join_tab.select->quick))
129
119
      { // Only read index tree
130
 
        if (! join_tab->insideout_match_tab)
 
120
        if (! join_tab.insideout_match_tab)
131
121
        {
132
122
          /*
133
123
             See bug #26447: "Using the clustered index for a table scan
134
124
             is always faster than using a secondary index".
135
125
           */
136
 
          if (table->getShare()->hasPrimaryKey() &&
137
 
              table->cursor->primary_key_is_clustered())
 
126
          if (table.getShare()->hasPrimaryKey() &&
 
127
              table.cursor->primary_key_is_clustered())
138
128
          {
139
 
            join_tab->index= table->getShare()->getPrimaryKey();
 
129
            join_tab.index= table.getShare()->getPrimaryKey();
140
130
          }
141
131
          else
142
132
          {
143
 
            join_tab->index= table->find_shortest_key(&table->covering_keys);
 
133
            join_tab.index= table.find_shortest_key(&table.covering_keys);
144
134
          }
145
135
        }
146
 
        join_tab->read_first_record= join_read_first;
147
 
        join_tab->type= AM_NEXT; // Read with index_first / index_next
 
136
        join_tab.read_first_record= join_read_first;
 
137
        join_tab.type= AM_NEXT; // Read with index_first / index_next
148
138
      }
149
139
    }
150
140
  }
151
 
 
152
 
  return false;
153
141
}
154
142
 
155
143
/**
163
151
*/
164
152
static uint32_t make_join_orderinfo(Join *join)
165
153
{
166
 
  uint32_t i= 0;
167
154
  if (join->need_tmp)
168
 
  {
169
155
    return join->tables;
170
 
  }
171
156
 
172
 
  for (i= join->const_tables ; i < join->tables ; i++)
 
157
  uint32_t i= join->const_tables;
 
158
  for (; i < join->tables; i++)
173
159
  {
174
160
    JoinTable *tab= join->join_tab + i;
175
161
    Table *table= tab->table;