~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/optimizer/explain_plan.cc

  • Committer: Monty Taylor
  • Date: 2011-02-13 17:26:39 UTC
  • mfrom: (2157.2.2 give-in-to-pkg-config)
  • mto: This revision was merged to the branch mainline in revision 2166.
  • Revision ID: mordred@inaugust.com-20110213172639-nhy7i72sfhoq13ms
Merged in pkg-config fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include <config.h>
21
 
 
22
 
#include <drizzled/session.h>
23
 
#include <drizzled/item/uint.h>
24
 
#include <drizzled/item/float.h>
25
 
#include <drizzled/item/string.h>
26
 
#include <drizzled/optimizer/explain_plan.h>
27
 
#include <drizzled/optimizer/position.h>
28
 
#include <drizzled/optimizer/quick_ror_intersect_select.h>
29
 
#include <drizzled/optimizer/range.h>
30
 
#include <drizzled/sql_select.h>
31
 
#include <drizzled/join.h>
32
 
#include <drizzled/internal/m_string.h>
33
 
#include <drizzled/select_result.h>
 
20
#include "config.h"
 
21
#include "drizzled/session.h"
 
22
#include "drizzled/item/uint.h"
 
23
#include "drizzled/item/float.h"
 
24
#include "drizzled/item/string.h"
 
25
#include "drizzled/optimizer/explain_plan.h"
 
26
#include "drizzled/optimizer/position.h"
 
27
#include "drizzled/optimizer/quick_ror_intersect_select.h"
 
28
#include "drizzled/optimizer/range.h"
 
29
#include "drizzled/sql_select.h"
 
30
#include "drizzled/join.h"
 
31
#include "drizzled/internal/m_string.h"
34
32
 
35
33
#include <cstdio>
36
34
#include <string>
75
73
 
76
74
void optimizer::ExplainPlan::printPlan()
77
75
{
 
76
  List<Item> field_list;
78
77
  List<Item> item_list;
79
78
  Session *session= join->session;
80
79
  select_result *result= join->result;
99
98
    for (uint32_t i= 0; i < 7; i++)
100
99
      item_list.push_back(item_null);
101
100
 
102
 
    if (join->session->getLex()->describe & DESCRIBE_EXTENDED)
 
101
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
103
102
      item_list.push_back(item_null);
104
103
 
105
104
    item_list.push_back(new Item_string(message,strlen(message),cs));
116
115
       appreciated :)
117
116
     */
118
117
    char table_name_buffer[NAME_LEN];
119
 
    item_list.clear();
 
118
    item_list.empty();
120
119
    /* id */
121
120
    item_list.push_back(new Item_null);
122
121
    /* select_type */
159
158
    /* ref */
160
159
    item_list.push_back(item_null);
161
160
    /* in_rows */
162
 
    if (join->session->getLex()->describe & DESCRIBE_EXTENDED)
 
161
    if (join->session->lex->describe & DESCRIBE_EXTENDED)
163
162
      item_list.push_back(item_null);
164
163
    /* rows */
165
164
    item_list.push_back(item_null);
181
180
    {
182
181
      JoinTable *tab= join->join_tab + i;
183
182
      Table *table= tab->table;
 
183
      char buff[512];
 
184
      char buff1[512], buff2[512], buff3[512];
184
185
      char keylen_str_buf[64];
185
 
      string extra;
 
186
      String extra(buff, sizeof(buff),cs);
186
187
      char table_name_buffer[NAME_LEN];
187
 
      string tmp1;
188
 
      string tmp2;
189
 
      string tmp3;
 
188
      String tmp1(buff1,sizeof(buff1),cs);
 
189
      String tmp2(buff2,sizeof(buff2),cs);
 
190
      String tmp3(buff3,sizeof(buff3),cs);
 
191
      extra.length(0);
 
192
      tmp1.length(0);
 
193
      tmp2.length(0);
 
194
      tmp3.length(0);
190
195
 
191
196
      quick_type= -1;
192
 
      item_list.clear();
 
197
      item_list.empty();
193
198
      /* id */
194
199
      item_list.push_back(new Item_uint((uint32_t)
195
200
            join->select_lex->select_number));
236
241
          if (tab->keys.test(j))
237
242
          {
238
243
            if (tmp1.length())
239
 
              tmp1.append(",");
 
244
              tmp1.append(',');
240
245
            tmp1.append(table->key_info[j].name,
241
 
                        strlen(table->key_info[j].name));
 
246
                        strlen(table->key_info[j].name),
 
247
                        system_charset_info);
242
248
          }
243
249
        }
244
250
      }
245
251
      if (tmp1.length())
246
 
        item_list.push_back(new Item_string(tmp1.c_str(),tmp1.length(),cs));
 
252
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
247
253
      else
248
254
        item_list.push_back(item_null);
249
255
 
262
268
        for (StoredKey **ref= tab->ref.key_copy; *ref; ref++)
263
269
        {
264
270
          if (tmp2.length())
265
 
            tmp2.append(",");
266
 
          tmp2.append((*ref)->name(),
267
 
                      strlen((*ref)->name()));
 
271
            tmp2.append(',');
 
272
          tmp2.append((*ref)->name(), 
 
273
                       strlen((*ref)->name()),
 
274
                       system_charset_info);
268
275
        }
269
 
        item_list.push_back(new Item_string(tmp2.c_str(),tmp2.length(),cs));
 
276
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
270
277
      }
271
278
      else if (tab->type == AM_NEXT)
272
279
      {
283
290
      else if (tab->select && tab->select->quick)
284
291
      {
285
292
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
286
 
        item_list.push_back(new Item_string(tmp2.c_str(),tmp2.length(),cs));
287
 
        item_list.push_back(new Item_string(tmp3.c_str(),tmp3.length(),cs));
 
293
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
 
294
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
288
295
        item_list.push_back(item_null);
289
296
      }
290
297
      else
298
305
      double examined_rows;
299
306
      if (tab->select && tab->select->quick)
300
307
      {
301
 
        examined_rows= tab->select->quick->records;
 
308
        examined_rows= rows2double(tab->select->quick->records);
302
309
      }
303
310
      else if (tab->type == AM_NEXT || tab->type == AM_ALL)
304
311
      {
305
 
        examined_rows= tab->limit ? tab->limit : tab->table->cursor->records();
 
312
        examined_rows= rows2double(tab->limit ? tab->limit :
 
313
                                                tab->table->cursor->records());
306
314
      }
307
315
      else
308
316
      {
314
322
                                       MY_INT64_NUM_DECIMAL_DIGITS));
315
323
 
316
324
      /* Add "filtered" field to item_list. */
317
 
      if (join->session->getLex()->describe & DESCRIBE_EXTENDED)
 
325
      if (join->session->lex->describe & DESCRIBE_EXTENDED)
318
326
      {
319
327
        float f= 0.0;
320
328
        if (examined_rows)
339
347
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
340
348
      {
341
349
        if (tab->packed_info & TAB_INFO_USING_INDEX)
342
 
          extra.append("; Using index");
 
350
          extra.append(STRING_WITH_LEN("; Using index"));
343
351
        if (tab->packed_info & TAB_INFO_USING_WHERE)
344
 
          extra.append("; Using where");
 
352
          extra.append(STRING_WITH_LEN("; Using where"));
345
353
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
346
 
          extra.append("; Full scan on NULL key");
347
 
        if (extra.length())
348
 
          extra.erase(0, 2);        /* Skip initial "; "*/
349
 
        item_list.push_back(new Item_string(extra.c_str(), extra.length(), cs));
 
354
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
 
355
        /* Skip initial "; "*/
 
356
        const char *str= extra.ptr();
 
357
        uint32_t len= extra.length();
 
358
        if (len)
 
359
        {
 
360
          str += 2;
 
361
          len -= 2;
 
362
        }
 
363
        item_list.push_back(new Item_string(str, len, cs));
350
364
      }
351
365
      else
352
366
      {
360
374
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
361
375
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE)
362
376
        {
363
 
          extra.append("; Using ");
 
377
          extra.append(STRING_WITH_LEN("; Using "));
364
378
          tab->select->quick->add_info_string(&extra);
365
379
        }
366
380
        if (tab->select)
385
399
              if (tmp.any())
386
400
                s << uppercase << hex << tmp.to_ulong();
387
401
            }
388
 
            extra.append("; Range checked for each record (index map: 0x");
 
402
            extra.append(STRING_WITH_LEN("; Range checked for each "
 
403
                  "record (index map: 0x"));
389
404
            extra.append(s.str().c_str());
390
 
            extra.append(")");
 
405
            extra.append(')');
391
406
          }
392
407
          else if (tab->select->cond)
393
408
          {
394
 
            extra.append("; Using where");
 
409
            extra.append(STRING_WITH_LEN("; Using where"));
395
410
          }
396
411
        }
397
412
        if (key_read)
398
413
        {
399
414
          if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
400
 
            extra.append("; Using index for group-by");
 
415
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
401
416
          else
402
 
            extra.append("; Using index");
 
417
            extra.append(STRING_WITH_LEN("; Using index"));
403
418
        }
404
419
        if (table->reginfo.not_exists_optimize)
405
 
          extra.append("; Not exists");
 
420
          extra.append(STRING_WITH_LEN("; Not exists"));
406
421
 
407
422
        if (need_tmp_table)
408
423
        {
409
424
          need_tmp_table=0;
410
 
          extra.append("; Using temporary");
 
425
          extra.append(STRING_WITH_LEN("; Using temporary"));
411
426
        }
412
427
        if (need_order)
413
428
        {
414
429
          need_order=0;
415
 
          extra.append("; Using filesort");
 
430
          extra.append(STRING_WITH_LEN("; Using filesort"));
416
431
        }
417
432
        if (distinct & test_all_bits(used_tables,session->used_tables))
418
 
          extra.append("; Distinct");
 
433
          extra.append(STRING_WITH_LEN("; Distinct"));
419
434
 
420
435
        if (tab->insideout_match_tab)
421
436
        {
422
 
          extra.append("; LooseScan");
 
437
          extra.append(STRING_WITH_LEN("; LooseScan"));
423
438
        }
424
439
 
425
440
        for (uint32_t part= 0; part < tab->ref.key_parts; part++)
426
441
        {
427
442
          if (tab->ref.cond_guards[part])
428
443
          {
429
 
            extra.append("; Full scan on NULL key");
 
444
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
430
445
            break;
431
446
          }
432
447
        }
433
448
 
434
449
        if (i > 0 && tab[-1].next_select == sub_select_cache)
435
 
          extra.append("; Using join buffer");
 
450
          extra.append(STRING_WITH_LEN("; Using join buffer"));
436
451
 
437
 
        if (extra.length())
438
 
          extra.erase(0, 2);
439
 
        item_list.push_back(new Item_string(extra.c_str(), extra.length(), cs));
 
452
        /* Skip initial "; "*/
 
453
        const char *str= extra.ptr();
 
454
        uint32_t len= extra.length();
 
455
        if (len)
 
456
        {
 
457
          str += 2;
 
458
          len -= 2;
 
459
        }
 
460
        item_list.push_back(new Item_string(str, len, cs));
440
461
      }
441
462
      // For next iteration
442
463
      used_tables|=table->map;
467
488
  {
468
489
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
469
490
    sl->uncacheable.reset(UNCACHEABLE_EXPLAIN);
470
 
    if (&session->getLex()->select_lex == sl)
 
491
    if (&session->lex->select_lex == sl)
471
492
    {
472
493
      if (sl->first_inner_unit() || sl->next_select())
473
494
      {
540
561
  }
541
562
  else
542
563
  {
543
 
    session->getLex()->current_select= first;
 
564
    session->lex->current_select= first;
544
565
    unit->set_limit(unit->global_parameters);
545
566
    res= select_query(session, 
546
567
                      &first->ref_pointer_array,