首页 > 数据库 >MySQL(十五)分析优化器的查询计划:Trace

MySQL(十五)分析优化器的查询计划:Trace

时间:2023-07-18 09:11:21浏览次数:44  
标签:rows Trace 查询 range student MySQL table 2042366 id

1 MySQL(十五)分析优化器的查询计划:Trace


OPTIMIZER_TRACE是mysql 5.6引入的一项追踪功能,它可以追踪优化器做出的各种决策(比如访问表的方法、各种开销计算和各种转换等等),并将结果记录到表INFORMATION_SCHEMA.OPTIMIZER_TRACE表中。

​ Trace功能默认是关闭的,需要开启trace,设置JSON格式,并设置最大能使用内存为1000000,防止使用过程中因内存过小而不能够使用

SET optimizer_trace = 'enabled=on', end_markers_in_json=on;
set optimizer_trace_max_mem_size=1000000;

​ 测试 并查看对应计划

SELECT * FROM student WHERE id < 2042366;
mysql> SELECT * FROM information_schema.OPTIMIZER_TRACE\G;
*************************** 1. row ***************************
# 第一部分:查询的SQL
QUERY: SELECT * FROM student WHERE id < 2042366
# 第二部分:追踪的信息
TRACE: {
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `student`.`id` AS `id`,`student`.`stuno` AS `stuno`,`student`.`name` AS `name`,`student`.`age` AS `age`,`student`.`classid` AS `classid` from `student` where (`student`.`id` < 2042366)"
          }
        ] /* steps */
      } /* join_preparation */
    },
    {
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`student`.`id` < 2042366)",
              "steps": [
                {
                  "transformation": "equality_propagation",
                  "resulting_condition": "(`student`.`id` < 2042366)"
                },
                {
                  "transformation": "constant_propagation",
                  "resulting_condition": "(`student`.`id` < 2042366)"
                },
                {
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "(`student`.`id` < 2042366)"
                }
              ] /* steps */
            } /* condition_processing */
          },
          {
            "substitute_generated_columns": {
            } /* substitute_generated_columns */
          },
          {
            "table_dependencies": [
              {
                "table": "`student`",
                "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": "`student`",
                "range_analysis": {
                  "table_scan": {
                    "rows": 3989929,
                    "cost": 406388
                  } /* table_scan */,
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",
                      "usable": true,
                      "key_parts": [
                        "id"
                      ] /* key_parts */
                    }
                  ] /* potential_range_indexes */,
                  "setup_range_conditions": [
                  ] /* setup_range_conditions */,
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  } /* group_index_range */,
                  "skip_scan_range": {
                    "potential_skip_scan_indexes": [
                      {
                        "index": "PRIMARY",
                        "usable": false,
                        "cause": "query_references_nonkey_column"
                      }
                    ] /* potential_skip_scan_indexes */
                  } /* skip_scan_range */,
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      {
                        "index": "PRIMARY",
                        "ranges": [
                          "id < 2042366"
                        ] /* ranges */,
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": true,
                        "using_mrr": false,
                        "index_only": false,
                        "in_memory": 0.386539,
                        "rows": 19,
                        "cost": 2.635,
                        "chosen": true
                      }
                    ] /* range_scan_alternatives */,
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    } /* analyzing_roworder_intersect */
                  } /* analyzing_range_alternatives */,
                  "chosen_range_access_summary": {
                    "range_access_plan": {
                      "type": "range_scan",
                      "index": "PRIMARY",
                      "rows": 19,
                      "ranges": [
                        "id < 2042366"
                      ] /* ranges */
                    } /* range_access_plan */,
                    "rows_for_plan": 19,
                    "cost_for_plan": 2.635,
                    "chosen": true
                  } /* chosen_range_access_summary */
                } /* range_analysis */
              }
            ] /* rows_estimation */
          },
          {
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ] /* plan_prefix */,
                "table": "`student`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "rows_to_scan": 19,
                      "access_type": "range",
                      "range_details": {
                        "used_index": "PRIMARY"
                      } /* range_details */,
                      "resulting_rows": 19,
                      "cost": 4.535,
                      "chosen": true
                    }
                  ] /* considered_access_paths */
                } /* best_access_path */,
                "condition_filtering_pct": 100,
                "rows_for_plan": 19,
                "cost_for_plan": 4.535,
                "chosen": true
              }
            ] /* considered_execution_plans */
          },
          {
            "attaching_conditions_to_tables": {
              "original_condition": "(`student`.`id` < 2042366)",
              "attached_conditions_computation": [
              ] /* attached_conditions_computation */,
              "attached_conditions_summary": [
                {
                  "table": "`student`",
                  "attached": "(`student`.`id` < 2042366)"
                }
              ] /* attached_conditions_summary */
            } /* attaching_conditions_to_tables */
          },
          {
            "finalizing_table_conditions": [
              {
                "table": "`student`",
                "original_table_condition": "(`student`.`id` < 2042366)",
                "final_table_condition   ": "(`student`.`id` < 2042366)"
              }
            ] /* finalizing_table_conditions */
          },
          {
            "refine_plan": [
              {
                "table": "`student`"
              }
            ] /* refine_plan */
          }
        ] /* steps */
      } /* join_optimization */
    },
    {
      "join_execution": {
        "select#": 1,
        "steps": [
        ] /* steps */
      } /* join_execution */
    }
  ] /* steps */
}
# 第三部分:追踪信息过长导致被截断的字节数
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0
          INSUFFICIENT_PRIVILEGES: 0
1 row in set (0.00 sec)

2 mysql监控分析视图-sys schema


索引情况
-- 查询冗余索引
SELECT * FROM sys.schema_redundant_indexes;
-- 查询未使用过的索引 
SELECT * FROM sys.schema_unused_indexes;
-- 查询索引的使用情况
SELECT index_name, rows_selected, rows_inserted, rows_updated, rows_deleted
FROM sys.schema_index_statistics
WHERE table_schema = 'atguigudb1'
表情况
-- 查询表的访问量
SELECT table_schema, table_name, SUM(io_read_requests + io_write_requests) AS io
FROM sys.schema_table_statistics GROUP BY table_schema, table_name ORDER BY io;
-- 查询占用 buffer pool较多的表
SELECT object_schema, object_name, allocated, DATA
FROM sys.innodb_buffer_stats_by_table ORDER BY allocated LIMIT 10;
-- 查看表的全表扫描情况
SELECT * FROM sys.statements_with_full_table_scans WHERE db='atguigudb1'
语句相关
-- 监控SQL执行的频率
SELECT db, exec_count, query from sys.statement_analysis
ORDER BY exec_count DESC

-- 监控使用了排序的SQL
SELECT db, exec_count, first_seen, last_seen, query
FROM sys.statements_with_sorting limit 1

-- 监控使用了临时表或者磁盘临时表的SQL
SELECT db, exec_count, tmp_tables, tmp_disk_tables, query 
from sys.statement_analysis 
WHERE tmp_tables > 0 or tmp_disk_tables > 0
ORDER BY (tmp_tables + tmp_disk_tables) DESC

image-20230414164919638

注意

通过sys库去查询的时候,mysql会耗费大量的资源去收集信息,严重的情况可能会导致业务请求被阻塞,从而引起故障,生产的时候尽量不要去频繁查看sys或者performance_schema、information_schema来完成监控、巡检等工作

标签:rows,Trace,查询,range,student,MySQL,table,2042366,id
From: https://www.cnblogs.com/tod4/p/17561843.html

相关文章

  • hive sql 查询 所有表 行数
    HiveSQL查询所有表行数ApacheHive是一个基于Hadoop的数据仓库基础架构,它提供了一种类似于SQL的查询语言,称为HiveSQL。在Hive中,我们可以使用HiveSQL查询和处理大规模的结构化和半结构化数据。本文将介绍如何使用HiveSQL查询所有表的行数。Hive中的表在Hive中,表是数据的逻辑......
  • EF Core Like 模糊查询
    在EFCore中可用EF.Functions.Like()、StartWith、Contains、EndsWith实现模糊查询 EF.Functions.Like() 在EntityFrameworkCore2.0中增加一个很酷的功能:EF.Functions.Like(),最终解析为SQL中的Like语句,以便于在LINQ查询中直接调用。query=query.Where(d=>EF.Fun......
  • Mysql基础4-数据查询
    一、DQL介绍DQL全称:DataQueryLanguage(数据查询语言),用来查询数据库中表的记录。关键字:select 二、DQL语法select字段列表from表名列表where条件列表groupby分组字段列表having分组后条件列表orderby排序字段列表limit分页参数注意:本章......
  • .NET Core6.0 通过EF的方式如何链接MySQL
    之前一直用的是EF链接SQLServer今天聊一聊EF链接MySQL第一步我们首先创建一个简单的用户实体类 这里呢创建了一个主键为string类型的用户Id和一个用户名称第二步我们创建一个类库里面创建上下文类 但是这里需要注意引用的NuGet包和链接SQLServer的包有个是不一样的这是......
  • linux Mysql 备份与还原
    数据库是企业中非常重要的部分,数据是企业的根本,不可丢失的,需要备份和还原。目录一、数据备份的重要性二、数据库备份类型三、常见的备份方法四、MySQL完全备份五、数据库完全备份分类六、实战案列七、总结   一、数据备份的重要性1.数据备份的重要......
  • java mongodb查询忽略大小写
    实现JavaMongoDB查询忽略大小写概述在使用Java与MongoDB进行数据交互时,有时我们需要执行不区分大小写的查询操作。本文将指导你如何在Java中实现忽略大小写的MongoDB查询。前提条件在开始之前,确保已经安装并配置好了Java开发环境和MongoDB数据库。流程下面是实现JavaMongoD......
  • mysql 更新时where条件缺失导致更新全表问题
    1、问题更新时where条件缺失导致更新全表问题2、错误sqlupdateorderset`status`=1in('XX001','XX002','XX003');错误分析:更新整个order表,并没有限制更新的范围。原因是这个SQL语句在IN子句中没有提供任何条件,导致MySQL将IN子句视为一个包含了所有order_id值的列表。......
  • mysql 笔记
    行转列: namecoursegradezhangsanjava20zhangsanc#60zhangsanpython40lisijava109lisic#30lisipython20wangwujava33 selectname,sum(casewhencourse='java'thengradeend)as'java',sum(casewhen......
  • 浅谈oracle,mysql数据备份
    oracle备份 方案1:Navicat工具迁移1.1开启Navicat,打开工具-数据同步   1.2选定原数据源与需要迁移到的数据源  1.3选择下一步,比对后开始进行数据同步   方案2:数据库服务器迁移2.1登录源数据库切换用户su-oracle切换到临时目录cd/u01/app/oracle......
  • Window安装解压版MySQL5.7
    软件下载官网地址:https://www.mysql.com下载地址:https://dev.mysql.com/downloads/mysql/安装步骤下载后会得到zip安装文件将zip安装文件解压到某个目录下,解压的目录最好不要有中文和空格以E:\Dev\languages\mysql\mysql-5.7.25目录为例添加环境变量:电脑-属......