mysql trace_MySQL SQL trace

MySQL SQL trace

从 MySQL 5.6 开始,可以使用 trace 查看优化器如何选择执行计划,通过SQL trace来跟踪sql语句的执行过程。通过trace文件能够进一步了解为什么优化器选择A计划, 而不是选择B计划。

打开trace,并设置格式为json

SET optimizer_trace="enabled=on",end_markers_in_json=on;

设置trace使用的内存大小,避免解析过程内存不足,文件显示不完整

set optimizer_trace_max_mem_size=1000000;

执行一条SQL语句,例如:

select * from t_user where id = '5' ;

然后,通过查询information_schema库的OPTIMIZER_TRACE表,就可以获取到SQL的执行计划跟踪信息。

select * from information_schema.optimizer_trace;

trace json文件分析

QUERY: select * from t_user where id = '5' --查询sql

TRACE: {

"steps": [

{

"join_preparation": {

"select#": 1,

"steps": [

{

"expanded_query": "/* select#1 */ select `t_user`.`id` AS `id`,`t_user`.`username` AS `username`,`t_user`.`password` AS `password`,`t_user`.`name` AS `name` from `t_user` where (`t_user`.`id` = '5')"

}

] /* steps */

} /* join_preparation */

},

{

"join_optimization": {

"select#": 1,

"steps": [

{

"condition_processing": { --条件处理

"condition": "WHERE",

"original_condition": "(`t_user`.`id` = '5')",

"steps": [

{

"transformation": "equality_propagation", --等值优化

"resulting_condition": "(`t_user`.`id` = '5')"

},

{

"transformation": "constant_propagation", --常量优化

"resulting_condition": "(`t_user`.`id` = '5')"

},

{

"transformation": "trivial_condition_removal", --琐碎条件排除

"resulting_condition": "(`t_user`.`id` = '5')"

}

] /* steps */

} /* condition_processing */

},

{

"substitute_generated_columns": { -- 替换生成的列

} /* substitute_generated_columns */

},

{

"table_dependencies": [ -- 表依赖

{

"table": "`t_user`",

"row_may_be_null": false, --是否有null值

"map_bit": 0,

"depends_on_map_bits": [

] /* depends_on_map_bits */

}

] /* table_dependencies */

},

{

"ref_optimizer_key_uses": [ --相关索引优化使用

{

"table": "`t_user`",

"field": "id", --索引字段

"equals": "'5'",

"null_rejecting": false

}

] /* ref_optimizer_key_uses */

},

{

"rows_estimation": [ --行评估

{

"table": "`t_user`",

"rows": 1,

"cost": 1,

"table_type": "const",

"empty": false

}

] /* rows_estimation */

},

{

"condition_on_constant_tables": "('5' = '5')",

"condition_value": true

},

{

"attaching_conditions_to_tables": {

"original_condition": "('5' = '5')",

"attached_conditions_computation": [

] /* attached_conditions_computation */,

"attached_conditions_summary": [

] /* attached_conditions_summary */

} /* attaching_conditions_to_tables */

},

{

"refine_plan": [

] /* refine_plan */

}

] /* steps */

} /* join_optimization */

},

{

"join_execution": {

"select#": 1,

"steps": [

] /* steps */

} /* join_execution */

}

] /* steps */

}

MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0

INSUFFICIENT_PRIVILEGES: 0

你可能感兴趣的:(mysql,trace)