Commit ff40705f authored by Sergey Petrunya's avatar Sergey Petrunya

Make SHOW EXPLAIN FOR produce a warning with the original text of query

that the EXPLAIN is for.
parent 9b269ea1
...@@ -20,12 +20,16 @@ select count(*) from t1 where a < 100000; ...@@ -20,12 +20,16 @@ select count(*) from t1 where a < 100000;
show explain for $thr2; show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index a a 5 NULL 1000 Using where; Using index 1 SIMPLE t1 index a a 5 NULL 1000 Using where; Using index
Warnings:
Note 1003 select count(*) from t1 where a < 100000
count(*) count(*)
1000 1000
select max(c) from t1 where a < 10; select max(c) from t1 where a < 10;
show explain for $thr2; show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 10 Using index condition 1 SIMPLE t1 range a a 5 NULL 10 Using index condition
Warnings:
Note 1003 select max(c) from t1 where a < 10
max(c) max(c)
9 9
# We can catch EXPLAIN, too. # We can catch EXPLAIN, too.
...@@ -35,6 +39,8 @@ explain select max(c) from t1 where a < 10; ...@@ -35,6 +39,8 @@ explain select max(c) from t1 where a < 10;
show explain for $thr2; show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 10 Using index condition; Rowid-ordered scan 1 SIMPLE t1 range a a 5 NULL 10 Using index condition; Rowid-ordered scan
Warnings:
Note 1003 explain select max(c) from t1 where a < 10
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 range a a 5 NULL 10 Using index condition; Rowid-ordered scan 1 SIMPLE t1 range a a 5 NULL 10 Using index condition; Rowid-ordered scan
set optimizer_switch= @show_expl_tmp; set optimizer_switch= @show_expl_tmp;
...@@ -47,6 +53,8 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -47,6 +53,8 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 1 PRIMARY A ALL NULL NULL NULL NULL 10
2 UNION B ALL NULL NULL NULL NULL 10 2 UNION B ALL NULL NULL NULL NULL 10
NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL
Warnings:
Note 1003 explain select a from t0 A union select a+1 from t0 B
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 1 PRIMARY A ALL NULL NULL NULL NULL 10
2 UNION B ALL NULL NULL NULL NULL 10 2 UNION B ALL NULL NULL NULL NULL 10
...@@ -60,6 +68,8 @@ id select_type table type possible_keys key key_len ref rows Extra ...@@ -60,6 +68,8 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 1 PRIMARY A ALL NULL NULL NULL NULL 10
2 UNION B ALL NULL NULL NULL NULL 10 2 UNION B ALL NULL NULL NULL NULL 10
NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL NULL UNION RESULT <union1,2> ALL NULL NULL NULL NULL NULL
Warnings:
Note 1003 explain select a from t0 A union select a+1 from t0 B
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 1 PRIMARY A ALL NULL NULL NULL NULL 10
2 UNION B ALL NULL NULL NULL NULL 10 2 UNION B ALL NULL NULL NULL NULL 10
...@@ -72,6 +82,8 @@ show explain for $thr2; ...@@ -72,6 +82,8 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where
2 SUBQUERY B ALL NULL NULL NULL NULL 10 2 SUBQUERY B ALL NULL NULL NULL NULL 10
Warnings:
Note 1003 select a, (select max(a) from t0 B) from t0 A where a<1
a (select max(a) from t0 B) a (select max(a) from t0 B)
0 9 0 9
# Uncorrelated subquery, explain # Uncorrelated subquery, explain
...@@ -82,6 +94,8 @@ show explain for $thr2; ...@@ -82,6 +94,8 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where
2 SUBQUERY B ALL NULL NULL NULL NULL 10 2 SUBQUERY B ALL NULL NULL NULL NULL 10
Warnings:
Note 1003 explain select a, (select max(a) from t0 B) from t0 A where a<1
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY A ALL NULL NULL NULL NULL 10 Using where
2 SUBQUERY B ALL NULL NULL NULL NULL 10 2 SUBQUERY B ALL NULL NULL NULL NULL 10
...@@ -93,6 +107,8 @@ show explain for $thr2; ...@@ -93,6 +107,8 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where
2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1
a (select max(a) from t0 b where b.a+a.a<10) a (select max(a) from t0 b where b.a+a.a<10)
0 9 0 9
# correlated subquery, explain # correlated subquery, explain
...@@ -103,6 +119,8 @@ show explain for $thr2; ...@@ -103,6 +119,8 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where
2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1
a (select max(a) from t0 b where b.a+a.a<10) a (select max(a) from t0 b where b.a+a.a<10)
0 9 0 9
# correlated subquery, select, while inside the subquery # correlated subquery, select, while inside the subquery
...@@ -113,6 +131,8 @@ show explain for $thr2; ...@@ -113,6 +131,8 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where
2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1
a (select max(a) from t0 b where b.a+a.a<10) a (select max(a) from t0 b where b.a+a.a<10)
0 9 0 9
# correlated subquery, explain, while inside the subquery # correlated subquery, explain, while inside the subquery
...@@ -123,6 +143,8 @@ show explain for $thr2; ...@@ -123,6 +143,8 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where 1 PRIMARY a ALL NULL NULL NULL NULL 10 Using where
2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY b ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select a, (select max(a) from t0 b where b.a+a.a<10) from t0 a where a<1
a (select max(a) from t0 b where b.a+a.a<10) a (select max(a) from t0 b where b.a+a.a<10)
0 9 0 9
# Try to do SHOW EXPLAIN for a query that runs a SET command: # Try to do SHOW EXPLAIN for a query that runs a SET command:
...@@ -168,14 +190,20 @@ show explain for $thr2; ...@@ -168,14 +190,20 @@ show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 1 PRIMARY t2 ALL NULL NULL NULL NULL 3
2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2
show explain for $thr2; show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 1 PRIMARY t2 ALL NULL NULL NULL NULL 3
2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2
show explain for $thr2; show explain for $thr2;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY t2 ALL NULL NULL NULL NULL 3 1 PRIMARY t2 ALL NULL NULL NULL NULL 3
2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where 2 DEPENDENT SUBQUERY t0 ALL NULL NULL NULL NULL 10 Using where
Warnings:
Note 1003 select t2.a, ((select max(a) from t0 where t2.a + t0.a <3) >3) as SUBQ from t2
a SUBQ a SUBQ
0 0 0 0
1 0 1 0
......
...@@ -3030,13 +3030,19 @@ void Show_explain_request::get_explain_data(void *arg) ...@@ -3030,13 +3030,19 @@ void Show_explain_request::get_explain_data(void *arg)
//TODO: change mem_root to point to request_thd->mem_root. //TODO: change mem_root to point to request_thd->mem_root.
// Actually, change the ARENA, because we're going to allocate items! // Actually, change the ARENA, because we're going to allocate items!
Query_arena backup_arena; Query_arena backup_arena;
req->target_thd->set_n_backup_active_arena((Query_arena*)req->request_thd, THD *target_thd= req->target_thd;
target_thd->set_n_backup_active_arena((Query_arena*)req->request_thd,
&backup_arena); &backup_arena);
if (req->target_thd->lex->unit.print_explain(req->explain_buf)) req->query_str.copy(target_thd->query(),
target_thd->query_length(),
&my_charset_bin);
if (target_thd->lex->unit.print_explain(req->explain_buf))
req->failed_to_produce= TRUE; req->failed_to_produce= TRUE;
req->target_thd->restore_active_arena((Query_arena*)req->request_thd, target_thd->restore_active_arena((Query_arena*)req->request_thd,
&backup_arena); &backup_arena);
} }
......
...@@ -1471,6 +1471,8 @@ class Show_explain_request ...@@ -1471,6 +1471,8 @@ class Show_explain_request
select_result_explain_buffer *explain_buf; select_result_explain_buffer *explain_buf;
String query_str;
static void get_explain_data(void *arg); static void get_explain_data(void *arg);
}; };
......
...@@ -2126,6 +2126,11 @@ void mysqld_show_explain(THD *thd, ulong thread_id) ...@@ -2126,6 +2126,11 @@ void mysqld_show_explain(THD *thd, ulong thread_id)
"Target is not running EXPLAINable command"); "Target is not running EXPLAINable command");
bres= TRUE; bres= TRUE;
} }
else
{
push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
ER_YES, explain_req.query_str.c_ptr_safe());
}
pthread_mutex_unlock(&tmp->LOCK_thd_data); pthread_mutex_unlock(&tmp->LOCK_thd_data);
if (!bres) if (!bres)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment