欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mysql trace命令介绍

程序员文章站 2022-06-24 20:57:43
trace作用:对SQL的跟踪,可以知道SQL是如何执行的,比EXPLAIN还要强大。注意点:开启trace工具会影响mysql性能,所以只能临时分析sql使用,用 完之后立即关闭。开启trace:set session optimizer_trace="enabled=on",end_markers_in_json=on; #开启set session optimizer_trace="enabled=off"; #关闭查看根据记录:# 这里是需要执行的sqlSELECT *...
trace作用:
  • 对SQL的跟踪,可以知道SQL是如何执行的,比EXPLAIN还要强大。
注意点:
  • 开启trace工具会影响mysql性能,所以只能临时分析sql使用,用 完之后立即关闭。
开启trace:
set session optimizer_trace="enabled=on",end_markers_in_json=on; #开启
set session optimizer_trace="enabled=off"; #关闭

如果使用索引查询数据,但最终还是走了全表扫描,可能是全表扫描的成本低于索引扫描,所以mysql最终选择全表扫描。
具体根据trace命令就可以根据sql查询成本对比出来。

查看根据记录:
# 这里是需要执行的sql
SELECT * FROM information_schema.OPTIMIZER_TRACE;

出现的结果是json数据:

{
  "steps": [
    {
      "join_preparation": { -- 第一阶段:SQL准备阶段
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `cr_shop_tables`.`id` AS `id`,`cr_shop_tables`.`title` AS `title`,`cr_shop_tables`.`num` AS `num`,`cr_shop_tables`.`qrcode_pic` AS `qrcode_pic`,`cr_shop_tables`.`sort` AS `sort`,`cr_shop_tables`.`create_time` AS `create_time`,`cr_shop_tables`.`modify_time` AS `modify_time`,`cr_shop_tables`.`shop_id` AS `shop_id`,`cr_shop_tables`.`status` AS `status` from `cr_shop_tables` where (`cr_shop_tables`.`title` > 'a') order by `cr_shop_tables`.`num`"
          }
        ] /* steps */
      } /* join_preparation */
    },
    {
      "join_optimization": {  ‐‐第二阶段:SQL优化阶段
        "select#": 1,
        "steps": [
          {
            "condition_processing": {  ‐‐条件处理
              "condition": "WHERE",
              "original_condition": "(`cr_shop_tables`.`title` > 'a')",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(`cr_shop_tables`.`title` > 'a')"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(`cr_shop_tables`.`title` > 'a')"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "(`cr_shop_tables`.`title` > 'a')"
                }
              ] /* steps */
            } /* condition_processing */
          },
          {
            "substitute_generated_columns": {
            } /* substitute_generated_columns */
          },
          {
            "table_dependencies": [ ‐‐表依赖详情
              {
                "table": "`cr_shop_tables`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ] /* depends_on_map_bits */
              }
            ] /* table_dependencies */
          },
          {
            "ref_optimizer_key_uses": [
            ] /* ref_optimizer_key_uses */
          },
          {
            "rows_estimation": [   ‐‐预估表的访问成本
              {
                "table": "`cr_shop_tables`",
                "table_scan": {   ‐‐全表扫描情况
                  "rows": 207,  ‐‐扫描行数
                  "cost": 4  ‐‐查询成本
                } /* table_scan */
              }
            ] /* rows_estimation */
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ] /* plan_prefix */,
                "table": "`cr_shop_tables`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "rows_to_scan": 207,
                      "access_type": "scan",
                      "resulting_rows": 207,
                      "cost": 45.4,
                      "chosen": true,
                      "use_tmp_table": true
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,
                "rows_for_plan": 207,
                "cost_for_plan": 45.4,
                "sort_cost": 207,
                "new_cost_for_plan": 252.4,
                "chosen": true
              }
            ] /* considered_execution_plans */
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`cr_shop_tables`.`title` > 'a')",
              "attached_conditions_computation": [
              ] /* attached_conditions_computation */,
              "attached_conditions_summary": [
                {
                  "table": "`cr_shop_tables`",
                  "attached": "(`cr_shop_tables`.`title` > 'a')"
                }
              ] /* attached_conditions_summary */
            } /* attaching_conditions_to_tables */
          },
          {
            "clause_processing": {
              "clause": "ORDER BY",
              "original_clause": "`cr_shop_tables`.`num`",
              "items": [
                {
                  "item": "`cr_shop_tables`.`num`"
                }
              ] /* items */,
              "resulting_clause_is_simple": true,
              "resulting_clause": "`cr_shop_tables`.`num`"
            } /* clause_processing */
          },
          {
            "refine_plan": [
              {
                "table": "`cr_shop_tables`"
              }
            ] /* refine_plan */
          }
        ] /* steps */
      } /* join_optimization */
    },
    {
      "join_execution": {
        "select#": 1,
        "steps": [
          {
            "filesort_information": [
              {
                "direction": "asc",
                "table": "`cr_shop_tables`",
                "field": "num"
              }
            ] /* filesort_information */,
            "filesort_priority_queue_optimization": {
              "usable": false,
              "cause": "not applicable (no LIMIT)"
            } /* filesort_priority_queue_optimization */,
            "filesort_execution": [
            ] /* filesort_execution */,
            "filesort_summary": {
              "rows": 133,
              "examined_rows": 207,
              "number_of_tmp_files": 0,
              "sort_buffer_size": 32136,
              "sort_mode": "<sort_key, rowid>"
            } /* filesort_summary */
          }
        ] /* steps */
      } /* join_execution */
    }
  ] /* steps */
}

本文地址:https://blog.csdn.net/xianyun1992/article/details/107650596

相关标签: MySQL