~drizzle-trunk/drizzle/development

« back to all changes in this revision

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

Merge Monty - Updates to pandora-build to support features of gcc 4.5.

Show diffs side-by-side

added added

removed removed

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