~maria-captains/maria/5.5-explain-analyze

« back to all changes in this revision

Viewing changes to sql/sql_lex.cc

  • Committer: Sergey Petrunya
  • Date: 2012-09-03 15:11:24 UTC
  • Revision ID: psergey@askmonty.org-20120903151124-s1y9vrz3k4ezy642
MDEV-406: EXPLAIN ANALYZE 
- First code, it is possible to get the data for joins/unions
- Numbers for join buffering are likely to be incorrect
- Same for subqueries (what should 'loops' show?)

Show diffs side-by-side

added added

removed removed

Lines of Context:
476
476
  if (lex->select_lex.group_list_ptrs)
477
477
    lex->select_lex.group_list_ptrs->clear();
478
478
  lex->describe= 0;
 
479
  lex->describe_analyze= FALSE;
479
480
  lex->subqueries= FALSE;
480
481
  lex->context_analysis_only= 0;
481
482
  lex->derived_tables= 0;
1878
1879
  bzero((char*) expr_cache_may_be_used, sizeof(expr_cache_may_be_used));
1879
1880
  m_non_agg_field_used= false;
1880
1881
  m_agg_func_used= false;
 
1882
  saved_explain_plan= NULL;
1881
1883
}
1882
1884
 
1883
1885
void st_select_lex::init_select()
1916
1918
  merged_into= 0;
1917
1919
  m_non_agg_field_used= false;
1918
1920
  m_agg_func_used= false;
 
1921
  saved_explain_plan= NULL;
1919
1922
}
1920
1923
 
1921
1924
/*
4162
4165
 
4163
4166
int st_select_lex::print_explain(select_result_sink *output, 
4164
4167
                                 uint8 explain_flags,
 
4168
                                 bool explain_analyze,
4165
4169
                                 bool *printed_anything)
4166
4170
{
4167
4171
  int res;
4177
4181
      /* It's a degenerate join */
4178
4182
      const char *cause= join->zero_result_cause ? join-> zero_result_cause : 
4179
4183
                                                   "No tables used";
4180
 
      res= join->print_explain(output, explain_flags, TRUE, FALSE, FALSE, 
4181
 
                               FALSE, cause);
 
4184
      res= join->print_explain(output, explain_flags, TRUE, explain_analyze,
 
4185
                               FALSE, FALSE, FALSE, cause);
4182
4186
    }
4183
4187
    else
4184
4188
    {
4185
 
      res= join->print_explain(output, explain_flags, TRUE,
 
4189
      res= join->print_explain(output, explain_flags, TRUE, explain_analyze,
4186
4190
                               join->need_tmp, // need_tmp_table
4187
4191
                               !join->skip_sort_order && !join->no_order &&
4188
4192
                               (join->order || join->group_list), // bool need_order
4192
4196
    if (res)
4193
4197
      goto err;
4194
4198
 
4195
 
    for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
4196
 
         unit;
4197
 
         unit= unit->next_unit())
 
4199
    if (!explain_analyze)
4198
4200
    {
4199
 
      /* 
4200
 
        Display subqueries only if they are not parts of eliminated WHERE/ON
4201
 
        clauses.
4202
 
      */
4203
 
      if (!(unit->item && unit->item->eliminated))
 
4201
      for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
 
4202
           unit;
 
4203
           unit= unit->next_unit())
4204
4204
      {
4205
 
        if ((res= unit->print_explain(output, explain_flags, printed_anything)))
4206
 
          goto err;
 
4205
        /* 
 
4206
          Display subqueries only if they are not parts of eliminated WHERE/ON
 
4207
          clauses.
 
4208
        */
 
4209
        if (!(unit->item && unit->item->eliminated))
 
4210
        {
 
4211
          /* psergey-todo: note, explain_analyze flag is not passed down into UNIONs */
 
4212
          if ((res= unit->print_explain(output, explain_flags, printed_anything)))
 
4213
            goto err;
 
4214
        }
4207
4215
      }
4208
4216
    }
4209
4217
  }
4229
4237
}
4230
4238
 
4231
4239
 
 
4240
int st_select_lex::print_saved_explain(THD *thd, select_result_sink *output)
 
4241
{
 
4242
  int res= 0;
 
4243
  List_iterator<String> it(*saved_explain_plan);
 
4244
  String *str;
 
4245
  while ((str= it++))
 
4246
  {
 
4247
    thd->protocol->set_packet(str->ptr(), str->length());
 
4248
    thd->protocol->write();
 
4249
  }
 
4250
  saved_explain_plan->delete_elements();
 
4251
  delete saved_explain_plan;
 
4252
 
 
4253
  for (SELECT_LEX_UNIT *unit= first_inner_unit();
 
4254
       unit;
 
4255
       unit= unit->next_unit())
 
4256
  {
 
4257
    /* 
 
4258
      Display subqueries only if they are not parts of eliminated WHERE/ON
 
4259
      clauses.
 
4260
    */
 
4261
    if (!(unit->item && unit->item->eliminated))
 
4262
    {
 
4263
      if ((res= unit->print_saved_explain(thd, output)))
 
4264
        goto err;
 
4265
    }
 
4266
  }
 
4267
err:
 
4268
  return res;
 
4269
}
 
4270
 
 
4271
 
4232
4272
int st_select_lex_unit::print_explain(select_result_sink *output, 
4233
4273
                                      uint8 explain_flags, bool *printed_anything)
4234
4274
{
4249
4289
 
4250
4290
  for (SELECT_LEX *sl= first; sl; sl= sl->next_select())
4251
4291
  {
4252
 
    if ((res= sl->print_explain(output, explain_flags, printed_anything)))
4253
 
      break;
4254
 
  }
4255
 
 
4256
 
  /* Note: fake_select_lex->join may be NULL or non-NULL at this point */
4257
 
  if (fake_select_lex)
4258
 
  {
4259
 
    res= print_fake_select_lex_join(output, TRUE /* on the fly */,
4260
 
                                    fake_select_lex, explain_flags);
 
4292
    if ((res= sl->print_explain(output, explain_flags, 
 
4293
                                /* explain_analyze=*/false, printed_anything)))
 
4294
      break;
 
4295
  }
 
4296
 
 
4297
  /* Note: fake_select_lex->join may be NULL or non-NULL at this point */
 
4298
  if (fake_select_lex)
 
4299
  {
 
4300
    res= print_fake_select_lex_join(output, TRUE /* on the fly */,
 
4301
                                    fake_select_lex, explain_flags, 
 
4302
                                    false /* not explain_analyze*/);
 
4303
  }
 
4304
  return res;
 
4305
}
 
4306
 
 
4307
 
 
4308
int st_select_lex_unit::print_saved_explain(THD *thd, select_result_sink *output)
 
4309
{
 
4310
  int res= 0;
 
4311
  SELECT_LEX *first= first_select();
 
4312
  
 
4313
  for (SELECT_LEX *sl= first; sl; sl= sl->next_select())
 
4314
  {
 
4315
    if ((res= sl->print_saved_explain(thd, output)))
 
4316
      break;
 
4317
  }
 
4318
 
 
4319
  /* Note: fake_select_lex->join may be NULL or non-NULL at this point */
 
4320
  if (fake_select_lex)
 
4321
  {
 
4322
    res= fake_select_lex->print_saved_explain(thd, output);
 
4323
#if 0
 
4324
    res= print_fake_select_lex_join(output, TRUE /* on the fly */,
 
4325
                                    fake_select_lex, /* explain_flags*/0, 
 
4326
                                    /* explain_analyze */ true);
 
4327
#endif    
4261
4328
  }
4262
4329
  return res;
4263
4330
}