1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-01 03:47:19 +03:00

MWL#182: Explain running statements

- Further progress with the code
- Testcases.
This commit is contained in:
Sergey Petrunya
2011-08-24 14:41:13 +04:00
parent 7e66213444
commit 84cb5de047
6 changed files with 107 additions and 13 deletions

View File

@ -0,0 +1,26 @@
drop table if exists t0, t1;
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int);
insert into t1 select A.a + 10*B.a + 100*C.a from t0 A, t0 B, t0 C;
show explain for 2*1000*1000*1000;
ERROR HY000: Unknown thread id: 2000000000
show explain for 3;
ERROR HY000: Error when executing command SHOW EXPLAIN: Target is not running EXPLAINable command
show explain for 2;
ERROR HY000: Error when executing command SHOW EXPLAIN: Target is not running EXPLAINable command
select get_lock('optimizer_done', 10);
get_lock('optimizer_done', 10)
1
select count(*) from t1 where a < 100000 and sleep(a*0 + release_lock('optimizer_done') +1);
select get_lock('optimizer_done', 100);
get_lock('optimizer_done', 100)
1
show explain for 3;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ALL NULL NULL NULL NULL 1000 Using where
select release_lock('optimizer_done');
release_lock('optimizer_done')
1
kill query 3;
drop table t0,t1;

View File

@ -0,0 +1,46 @@
#
# Tests for SHOW EXPLAIN FOR functionality
#
--disable_warnings
drop table if exists t0, t1;
--enable_warnings
create table t0 (a int);
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
create table t1 (a int);
insert into t1 select A.a + 10*B.a + 100*C.a from t0 A, t0 B, t0 C;
#
# Try killing a non-existent thread
#
--error ER_NO_SUCH_THREAD
show explain for 2*1000*1000*1000;
# Setup two threads and their ids
let $thr1=`select connection_id()`;
connect (con1, localhost, root,,);
connection con1;
let $thr2=`select connection_id()`;
connection default;
# SHOW EXPLAIN FOR <idle thread>
--error ER_ERROR_WHEN_EXECUTING_COMMAND
eval show explain for $thr2;
# SHOW EXPLAIN FOR <ourselves>
--error ER_ERROR_WHEN_EXECUTING_COMMAND
eval show explain for $thr1;
# SHOW EXPLAIN FOR <running thread>
connection con1;
select get_lock('optimizer_done', 10);
send select count(*) from t1 where a < 100000 and sleep(a*0 + release_lock('optimizer_done') +1);
connection default;
select get_lock('optimizer_done', 100);
eval show explain for $thr2;
select release_lock('optimizer_done');
eval kill query $thr2;
#insert into t1 values ('one'),('two'),('three');
drop table t0,t1;

View File

@ -2932,7 +2932,14 @@ void THD::restore_active_arena(Query_arena *set, Query_arena *backup)
DBUG_VOID_RETURN; DBUG_VOID_RETURN;
} }
// psergey
/*
Produce EXPLAIN data.
This function is APC-scheduled to be run in the context of the thread that
we're producing EXPLAIN for.
*/
void Show_explain_request::get_explain_data(void *arg) void Show_explain_request::get_explain_data(void *arg)
{ {
Show_explain_request *req= (Show_explain_request*)arg; Show_explain_request *req= (Show_explain_request*)arg;

View File

@ -3625,19 +3625,24 @@ bool st_select_lex::save_prep_leaf_tables(THD *thd)
int st_select_lex::print_explain(select_result_sink *output) int st_select_lex::print_explain(select_result_sink *output)
{ {
int res;
if (join && join->optimized == 2) if (join && join->optimized == 2)
{ {
//psergey-TODO: any? res= join->print_explain(output, TRUE,
return join->print_explain(output, TRUE, FALSE, // need_tmp_table,
FALSE, // need_tmp_table, FALSE, // bool need_order,
FALSE, // bool need_order, FALSE, // bool distinct,
FALSE, // bool distinct, NULL); //const char *message
NULL); //const char *message
} }
else else
{ {
DBUG_ASSERT(0); /* Produce "not yet optimized" line */
/* produce "not yet optimized" line */ const char *msg="Not yet optimized";
res= join->print_explain(output, TRUE,
FALSE, // need_tmp_table,
FALSE, // bool need_order,
FALSE, // bool distinct,
msg); //const char *message
} }
return 0; return 0;
} }

View File

@ -2019,6 +2019,17 @@ JOIN::save_join_tab()
} }
void JOIN::exec()
{
/*
Enable SHOW EXPLAIN only if we're in the top-level query.
*/
thd->apc_target.enable();
exec_inner();
thd->apc_target.disable();
}
/** /**
Exec select. Exec select.
@ -2030,8 +2041,8 @@ JOIN::save_join_tab()
@todo @todo
When can we have here thd->net.report_error not zero? When can we have here thd->net.report_error not zero?
*/ */
void
JOIN::exec() void JOIN::exec_inner()
{ {
List<Item> *columns_list= &fields_list; List<Item> *columns_list= &fields_list;
int tmp_error; int tmp_error;
@ -2894,9 +2905,7 @@ mysql_select(THD *thd, Item ***rref_pointer_array,
if (thd->is_error()) if (thd->is_error())
goto err; goto err;
thd->apc_target.enable();
join->exec(); join->exec();
thd->apc_target.disable();
if (thd->cursor && thd->cursor->is_open()) if (thd->cursor && thd->cursor->is_open())
{ {

View File

@ -1078,6 +1078,7 @@ public:
int reinit(); int reinit();
int init_execution(); int init_execution();
void exec(); void exec();
void exec_inner();
int destroy(); int destroy();
void restore_tmp(); void restore_tmp();
bool alloc_func_list(); bool alloc_func_list();