MySQL内核调试_MySQL内核技术之“Opt_trace_系列”
MySQL代碼使用了大量Opt_trace相關結構,先看代碼中的一段注釋:
This optimizer trace is aimed at producing output, which is readable by
humans and by programs, to aid understanding of decisions and actions taken
by the MySQL Optimizer.
可以看出Opt_trace是用來記錄相關操作的,以便幫助開發人員進行調試。其并不具有功能性作用。先來看一下MySQL中是怎么使用Opt_trace的。在主執行函數mysql_execute_command中(位于sql_parse.cc):
Opt_trace_start ots(thd, all_tables, lex->sql_command, &lex->var_list,
thd->query().str, thd->query().length, NULL,
thd->variables.character_set_client);
Opt_trace_object trace_command(&thd->opt_trace);
Opt_trace_array trace_command_steps(&thd->opt_trace, "steps");
...
case SQLCOM_SELECT:
{
if (!res)
res= execute_sqlcom_select(thd, all_tables);
break;
}
在執行sql操作(如SELECT)前,基于thd->opt_trace創建出trace_command和trace_command_steps。注意:當執行完畢后,這兩個變量自動銷毀。
那么Opt_trace_context是什么呢?直接看注釋:
A per-session context which is always available at any point of execution,
It maintains properties of the session's regarding tracing: enabled/disabled
state, style (all trace on one line, or not, etc), a list of all remembered
traces of previous and current SQL statement (as restricted by OFFSET/LIMIT),
and a pointer to the current (being-generated) trace (which itself has a
pointer to its current open object/array).
簡單來說就是thd->opt_trace存了很多上下文信息。這些信息在整個query執行過程中都是需要的。最上面的代碼中:
Opt_trace_start ots(thd, all_tables, lex->sql_command, &lex->var_list,
thd->query().str, thd->query().length, NULL,
thd->variables.character_set_client);
就是用來初始化和賦值thd->opt_trace的。當執行完畢后,ots所在的函數體mysql_execute_command退出時就自動銷毀了,thd->opt_trace在ots的析構函數中被銷毀。除了
基類為Opt_trace_struct定義在opt_trace.h中。代碼用的最多的是兩個派生類Opt_trace_object和Opt_trace_array,以及Opt_trace_context
總結
以上是生活随笔為你收集整理的MySQL内核调试_MySQL内核技术之“Opt_trace_系列”的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: get mysql options_my
- 下一篇: c语言爱心代码空心,c语言心形图案代码,