~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2010 Padraig O'Sullivan
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <drizzled/join_table.h>
#include <drizzled/table.h>
#include <drizzled/sql_select.h>
#include <drizzled/internal/my_sys.h>
#include <drizzled/optimizer/access_method/scan.h>
#include <drizzled/util/test.h>
#include <drizzled/statistics_variables.h>
#include <drizzled/session.h>

using namespace drizzled;

static uint32_t make_join_orderinfo(Join *join);

void optimizer::Scan::getStats(Table& table, JoinTable& join_tab)
{
  Join *join= join_tab.join;
  bool statistics= test(! (join->select_options & SELECT_DESCRIBE));
  uint64_t options= (join->select_options &
                     (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) |
                     (0);
  uint32_t no_jbuf_after= make_join_orderinfo(join);
  uint32_t index= &join_tab - join->join_tab;

  /*
   * If previous table use cache
   * If the incoming data set is already sorted don't use cache.
   */
  table.status= STATUS_NO_RECORD;

  if (index != join->const_tables && 
      ! (options & SELECT_NO_JOIN_CACHE) &&
      join_tab.use_quick != 2 && 
      ! join_tab.first_inner && 
      index <= no_jbuf_after &&
      ! join_tab.insideout_match_tab)
  {
    if ((options & SELECT_DESCRIBE) ||
        ! join_init_cache(join->session,
                          join->join_tab + join->const_tables,
                          index - join->const_tables))
    {
      (&join_tab)[-1].next_select= sub_select_cache; /* Patch previous */
    }
  }

  /* These init changes read_record */
  if (join_tab.use_quick == 2)
  {
    join->session->server_status|= SERVER_QUERY_NO_GOOD_INDEX_USED;
    join_tab.read_first_record= join_init_quick_read_record;
    if (statistics)
    {
      join->session->status_var.select_range_check_count++;
    }
  }
  else
  {
    join_tab.read_first_record= join_init_read_record;
    if (index == join->const_tables)
    {
      if (join_tab.select && join_tab.select->quick)
      {
        if (statistics)
          join->session->status_var.select_range_count++;
      }
      else
      {
        join->session->server_status|= SERVER_QUERY_NO_INDEX_USED;
        if (statistics)
          join->session->status_var.select_scan_count++;
      }
    }
    else
    {
      if (join_tab.select && join_tab.select->quick)
      {
        if (statistics)
          join->session->status_var.select_full_range_join_count++;
      }
      else
      {
        join->session->server_status|= SERVER_QUERY_NO_INDEX_USED;
        if (statistics)
          join->session->status_var.select_full_join_count++;
      }
    }
    if (! table.no_keyread)
    {
      if (join_tab.select && 
          join_tab.select->quick &&
          join_tab.select->quick->index != MAX_KEY && //not index_merge
          table.covering_keys.test(join_tab.select->quick->index))
      {
        table.key_read= 1;
        table.cursor->extra(HA_EXTRA_KEYREAD);
      }
      else if (! table.covering_keys.none() && ! (join_tab.select && join_tab.select->quick))
      { // Only read index tree
        if (! join_tab.insideout_match_tab)
        {
          /*
             See bug #26447: "Using the clustered index for a table scan
             is always faster than using a secondary index".
           */
          if (table.getShare()->hasPrimaryKey() &&
              table.cursor->primary_key_is_clustered())
          {
            join_tab.index= table.getShare()->getPrimaryKey();
          }
          else
          {
            join_tab.index= table.find_shortest_key(&table.covering_keys);
          }
        }
        join_tab.read_first_record= join_read_first;
        join_tab.type= AM_NEXT; // Read with index_first / index_next
      }
    }
  }
}

/**
  Determine if the set is already ordered for order_st BY, so it can
  disable join cache because it will change the ordering of the results.
  Code handles sort table that is at any location (not only first after
  the const tables) despite the fact that it's currently prohibited.
  We must disable join cache if the first non-const table alone is
  ordered. If there is a temp table the ordering is done as a last
  operation and doesn't prevent join cache usage.
*/
static uint32_t make_join_orderinfo(Join *join)
{
  if (join->need_tmp)
    return join->tables;

  uint32_t i= join->const_tables;
  for (; i < join->tables; i++)
  {
    JoinTable *tab= join->join_tab + i;
    Table *table= tab->table;
    if ((table == join->sort_by_table &&
        (! join->order || join->skip_sort_order)) ||
        (join->sort_by_table == (Table *) 1 &&  i != join->const_tables))
    {
      break;
    }
  }
  return i;
}