~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

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