MySQL SQL trace

Posted 面具下的戏命师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 SQL trace的主要内容,如果未能解决你的问题,请参考以下文章

MySQL SQL trace

用MySQL的optimizer_trace进行sql调优

mysql Tracing the Optimizer

Mysql5.5 慢查询 trace的配置(转,针对5.5)

[MySQL 5.6] 初识5.6的optimizer trace

mysql索引优化实践