MySQL优化器

优化器

MySQL存储引擎中存在了一个可插拔的优化器OPTIMIZER_TRACE,可以看到内部查询计划的TRACE信息,从而可以知道MySQL内部执行过程

查询优化器状态

show variables like 'optimizer_trace';

Variable_name   Value
optimizer_trace enabled=off,one_line=off

开启优化器

set session optimizer_trace="enabled=on,one_line=on",end_markers_in_json=on;

查看优化器追踪内存大小

show variables like 'optimizer_trace_max_mem_size';

Variable_name                 Value
optimizer_trace_max_mem_size 16384

可以在information_schema数据库中的OPTIMIZER_TRACE查看sql执行情况

SELECT trace FROM information_schema.OPTIMIZER_TRACE;

示例:

 explain select id,sum(costfrom jr_form where id in (
select max(idfrom jr_form group by jr_code) group by id;
SELECT * FROM INFORMATION_SCHEMA.OPTIMIZER_TRACE;

这里注意一下,我在Navicat中执行时如果两个语句分开执行会导致TRACE中内容为

错误的Trace操作
错误的Trace操作

需要选中两条sql语句一块执行才可以

trace结果为

{
    "steps": [
        {
            "join_preparation": {
                "select#"1,
                "steps": [
                    {
                        "join_preparation": {
                            "select#"2,
                            "steps": [
                                {
                                    "expanded_query""/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code`"
                                },
                                {
                                    "transformation": {
                                        "select#"2,
                                        "from""IN (SELECT)",
                                        "to""semijoin",
                                        "chosen"false
                                    }
                                },
                                {
                                    "transformation": {
                                        "select#"2,
                                        "from""IN (SELECT)",
                                        "to""EXISTS (CORRELATED SELECT)",
                                        "chosen"true,
                                        "evaluating_constant_having_conditions": []
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "expanded_query""/* select#1 */ select `jr_form`.`id` AS `id`,sum(`jr_form`.`cost`) AS `sum(cost)` from `jr_form` where <in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`))))) group by `jr_form`.`id`"
                    }
                ]
            }
        },
        {
            "join_optimization": {
                "select#"1,
                "steps": [
                    {
                        "condition_processing": {
                            "condition""WHERE",
                            "original_condition""<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))",
                            "steps": [
                                {
                                    "transformation""equality_propagation",
                                    "subselect_evaluation": [],
                                    "resulting_condition""<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
                                },
                                {
                                    "transformation""constant_propagation",
                                    "subselect_evaluation": [],
                                    "resulting_condition""<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
                                },
                                {
                                    "transformation""trivial_condition_removal",
                                    "subselect_evaluation": [],
                                    "resulting_condition""<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
                                }
                            ]
                        }
                    },
                    {
                        "substitute_generated_columns": {}
                    },
                    {
                        "table_dependencies": [
                            {
                                "table""`jr_form`",
                                "row_may_be_null"false,
                                "map_bit"0,
                                "depends_on_map_bits": []
                            }
                        ]
                    },
                    {
                        "ref_optimizer_key_uses": []
                    },
                    {
                        "rows_estimation": [
                            {
                                "table""`jr_form`",
                                "const_keys_added": {
                                    "keys": [
                                        "PRIMARY",
                                        "jr_code"
                                    ],
                                    "cause""group_by"
                                },
                                "range_analysis": {
                                    "table_scan": {
                                        "rows"184,
                                        "cost"44.9
                                    },
                                    "potential_range_indexes": [
                                        {
                                            "index""PRIMARY",
                                            "usable"true,
                                            "key_parts": [
                                                "id"
                                            ]
                                        },
                                        {
                                            "index""jr_code",
                                            "usable"true,
                                            "key_parts": [
                                                "jr_code",
                                                "id"
                                            ]
                                        }
                                    ],
                                    "setup_range_conditions": [],
                                    "group_index_range": {
                                        "chosen"false,
                                        "cause""not_applicable_aggregate_function"
                                    }
                                }
                            }
                        ]
                    },
                    {
                        "considered_execution_plans": [
                            {
                                "plan_prefix": [],
                                "table""`jr_form`",
                                "best_access_path": {
                                    "considered_access_paths": [
                                        {
                                            "rows_to_scan"184,
                                            "access_type""scan",
                                            "resulting_rows"184,
                                            "cost"42.8,
                                            "chosen"true,
                                            "use_tmp_table"true
                                        }
                                    ]
                                },
                                "condition_filtering_pct"100,
                                "rows_for_plan"184,
                                "cost_for_plan"42.8,
                                "sort_cost"184,
                                "new_cost_for_plan"226.8,
                                "chosen"true
                            }
                        ]
                    },
                    {
                        "attaching_conditions_to_tables": {
                            "original_condition""<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))",
                            "attached_conditions_computation": [],
                            "attached_conditions_summary": [
                                {
                                    "table""`jr_form`",
                                    "attached""<in_optimizer>(`jr_form`.`id`,<exists>(/* select#2 */ select max(`jr_form`.`id`) from `jr_form` group by `jr_form`.`jr_code` having (<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))))"
                                }
                            ]
                        }
                    },
                    {
                        "clause_processing": {
                            "clause""GROUP BY",
                            "original_clause""`jr_form`.`id`",
                            "items": [
                                {
                                    "item""`jr_form`.`id`"
                                }
                            ],
                            "resulting_clause_is_simple"true,
                            "resulting_clause""`jr_form`.`id`"
                        }
                    },
                    {
                        "reconsidering_access_paths_for_index_ordering": {
                            "clause""GROUP BY",
                            "index_order_summary": {
                                "table""`jr_form`",
                                "index_provides_order"true,
                                "order_direction""asc",
                                "index""PRIMARY",
                                "plan_changed"true,
                                "access_type""index"
                            }
                        }
                    },
                    {
                        "refine_plan": [
                            {
                                "table""`jr_form`"
                            }
                        ]
                    }
                ]
            }
        },
        {
            "join_optimization": {
                "select#"2,
                "steps": [
                    {
                        "condition_processing": {
                            "condition""HAVING",
                            "original_condition""(<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))",
                            "steps": [
                                {
                                    "transformation""constant_propagation",
                                    "resulting_condition""(<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))"
                                },
                                {
                                    "transformation""trivial_condition_removal",
                                    "resulting_condition""(<cache>(`jr_form`.`id`) = <ref_null_helper>(max(`jr_form`.`id`)))"
                                }
                            ]
                        }
                    },
                    {
                        "substitute_generated_columns": {}
                    },
                    {
                        "table_dependencies": [
                            {
                                "table""`jr_form`",
                                "row_may_be_null"false,
                                "map_bit"0,
                                "depends_on_map_bits": []
                            }
                        ]
                    },
                    {
                        "rows_estimation": [
                            {
                                "table""`jr_form`",
                                "const_keys_added": {
                                    "keys": [
                                        "jr_code"
                                    ],
                                    "cause""group_by"
                                },
                                "range_analysis": {
                                    "table_scan": {
                                        "rows"184,
                                        "cost"44.9
                                    },
                                    "potential_range_indexes": [
                                        {
                                            "index""PRIMARY",
                                            "usable"false,
                                            "cause""not_applicable"
                                        },
                                        {
                                            "index""jr_code",
                                            "usable"true,
                                            "key_parts": [
                                                "jr_code",
                                                "id"
                                            ]
                                        }
                                    ],
                                    "best_covering_index_scan": {
                                        "index""jr_code",
                                        "cost"41.319,
                                        "chosen"true
                                    },
                                    "group_index_range": {
                                        "potential_group_range_indexes": [
                                            {
                                                "index""jr_code",
                                                "covering"true,
                                                "rows"152,
                                                "cost"64.8
                                            }
                                        ]
                                    },
                                    "best_group_range_summary": {
                                        "type""index_group",
                                        "index""jr_code",
                                        "group_attribute""id",
                                        "min_aggregate"false,
                                        "max_aggregate"true,
                                        "distinct_aggregate"false,
                                        "rows"152,
                                        "cost"64.8,
                                        "key_parts_used_for_access": [
                                            "jr_code"
                                        ],
                                        "ranges": [],
                                        "chosen"false,
                                        "cause""cost"
                                    }
                                }
                            }
                        ]
                    },
                    {
                        "considered_execution_plans": [
                            {
                                "plan_prefix": [],
                                "table""`jr_form`",
                                "best_access_path": {
                                    "considered_access_paths": [
                                        {
                                            "rows_to_scan"184,
                                            "access_type""scan",
                                            "resulting_rows"184,
                                            "cost"42.8,
                                            "chosen"true,
                                            "use_tmp_table"true
                                        }
                                    ]
                                },
                                "condition_filtering_pct"100,
                                "rows_for_plan"184,
                                "cost_for_plan"42.8,
                                "sort_cost"184,
                                "new_cost_for_plan"226.8,
                                "chosen"true
                            }
                        ]
                    },
                    {
                        "transformation": {
                            "select#"2,
                            "from""IN (SELECT)",
                            "to""materialization",
                            "has_nullable_expressions"true,
                            "treat_UNKNOWN_as_FALSE"true,
                            "possible"true
                        }
                    },
                    {
                        "execution_plan_for_potential_materialization": {
                            "surely_same_plan_as_EXISTS"true,
                            "cause""EXISTS_did_not_change_WHERE",
                            "subq_mat_decision": {
                                "parent_fanouts": [
                                    {
                                        "select#"1,
                                        "subq_attached_to_table"true,
                                        "table""`jr_form`",
                                        "fanout"184,
                                        "cacheable"true
                                    }
                                ],
                                "cost_to_create_and_fill_materialized_table"265.6,
                                "cost_of_one_EXISTS"226.8,
                                "number_of_subquery_evaluations"184,
                                "cost_of_materialization"302.4,
                                "cost_of_EXISTS"41731,
                                "chosen"true
                            }
                        }
                    },
                    {
                        "transformation": {
                            "select#"2,
                            "from""IN (SELECT)",
                            "to""materialization",
                            "chosen"true,
                            "unknown_key_1": {
                                "creating_tmp_table": {
                                    "tmp_table_info": {
                                        "row_length"9,
                                        "key_length"9,
                                        "unique_constraint"false,
                                        "location""memory (heap)",
                                        "row_limit_estimate"1864135
                                    }
                                }
                            }
                        }
                    },
                    {
                        "attaching_conditions_to_tables": {
                            "original_condition"null,
                            "attached_conditions_computation": [],
                            "attached_conditions_summary": [
                                {
                                    "table""`jr_form`",
                                    "attached"null
                                }
                            ]
                        }
                    },
                    {
                        "clause_processing": {
                            "clause""GROUP BY",
                            "original_clause""`jr_form`.`jr_code`",
                            "items": [
                                {
                                    "item""`jr_form`.`jr_code`"
                                }
                            ],
                            "resulting_clause_is_simple"true,
                            "resulting_clause""`jr_form`.`jr_code`"
                        }
                    },
                    {
                        "reconsidering_access_paths_for_index_ordering": {
                            "clause""GROUP BY",
                            "index_order_summary": {
                                "table""`jr_form`",
                                "index_provides_order"true,
                                "order_direction""asc",
                                "index""jr_code",
                                "plan_changed"false
                            }
                        }
                    },
                    {
                        "refine_plan": [
                            {
                                "table""`jr_form`"
                            }
                        ]
                    }
                ]
            }
        },
        {
            "join_explain": {
                "select#"1,
                "steps": [
                    {
                        "join_explain": {
                            "select#"2,
                            "steps": []
                        }
                    }
                ]
            }
        }
    ]
}

内容很长,之后在详细说明

https://zhhll.icu/2021/数据库/关系型数据库/MySQL/进阶/25.优化器/

本文由 mdnice 多平台发布

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/675353.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Go语言每日一题——链表篇(七)

传送门 牛客面试笔试必刷101题 ----------------删除链表的倒数第n个节点 题目以及解析 题目 解题代码及解析 解析 这一道题与昨天的题目在解题思路上有一定的相似之处&#xff0c;都是基于双指针定义快慢指针&#xff0c;这里我们让快指针先走n步&#xff0c;又因为n一定…

吉他学习:右手拨弦方法,右手拨弦训练 左手按弦方法

第六课 右手拨弦方法https://m.lizhiweike.com/lecture2/29362775 第七课 右手拨弦训练https://m.lizhiweike.com/lecture2/29362708

vue.js基于springboot的实验室设备管理系统10345

(1)设备信息模块&#xff1a;记录设备的基本信息&#xff0c;如设备采购来源信息、设备需求量、当前数量、日期等。 (2) 用户模块&#xff1a;教师职工。实现对用户个人信息、消息管理和实验室设备的查询使用申请等。 (3) 管理员模块&#xff1a;实现对所有设备信息的增删改查&…

腾讯云游戏服务器购买入口,详细配置精准报价

2024年更新腾讯云游戏联机服务器配置价格表&#xff0c;可用于搭建幻兽帕鲁、雾锁王国等游戏服务器&#xff0c;游戏服务器配置可选4核16G12M、8核32G22M、4核32G10M、16核64G35M、4核16G14M等配置&#xff0c;可以选择轻量应用服务器和云服务器CVM内存型MA3或标准型SA2实例&am…

BUUCTF-Real-[Tomcat]CVE-2017-12615

目录 漏洞描述 一、漏洞编号&#xff1a;CVE-2017-12615 二、漏洞复现 get flag 漏洞描述 CVE-2017-12615&#xff1a;远程代码执行漏洞 影响范围&#xff1a;Apache Tomcat 7.0.0 - 7.0.79 (windows环境) 当 Tomcat 运行在 Windows 操作系统时&#xff0c;且启用了 HTTP P…

Qlik Sense : where exists

什么是Exists函数 Exists() 用于确定是否已经将特定字段值加载到数据加载脚本中的字段。此函数用于返回 TRUE 或 FALSE&#xff0c;这样它可以用于 LOAD 语句或 IF 语句中的 where 子句。 信息注释您也可使用 Not Exists() 来确定是否尚未加载字段值&#xff0c;但是如果要在…

Excel+VBA处理高斯光束

文章目录 1 图片导入与裁剪2 获取图片数据3 数据拟合 1 图片导入与裁剪 插入图片没什么好说的&#xff0c;新建Excel&#xff0c;【插入】->【图片】。 由于图像比较大&#xff0c;所以要对数据进行截取&#xff0c;选中图片之后&#xff0c;点击选项卡右端的【图片格式】…

【JavaWeb】头条新闻项目实现 基本增删改查 分页查询 登录注册校验 业务功能实现 第二期

文章目录 一、为什么使用token口令二、登录注册功能2.1 登录表单提交后端代码&#xff1a; 2.2 根据token获取完整用户信息代码实现&#xff1a; 2.3 注册时用户名占用校验代码实现&#xff1a; 2.4 注册表单提交代码实现&#xff1a; 三、头条首页功能3.1 查询所有头条分类3.2…

docker自定义镜像并使用

写在前面 本文看下如何自定义镜像。 ik包从这里 下载。 1&#xff1a;自定义带有ik的es镜像 先看下目录结构&#xff1a; /opt/program/mychinese [rootlocalhost mychinese]# ll total 16 -rw-r--r-- 1 root root 1153 Feb 5 04:18 docker-compose.yaml -rw-rw-r-- 1 el…

利巴韦林市场规模及厂商格局分析

利巴韦林又名病毒唑&#xff0c;是一种合成鸟苷类似物&#xff0c;对多种DNA和RNA病毒具有广谱活性&#xff0c;可干扰病毒mRNA的合成。利巴韦林1970年由ICN制药公司发现&#xff0c;1986年被批准临床应用&#xff0c;通常与干扰素药物&#xff08;如聚乙二醇干扰素α-2a或聚乙…

CTFshow web(php命令执行 45-49)

基础知识&#xff1a; 1.绕过cat使用&#xff1a; tac more less head tac tail nl od(二进制查看) vi vim sort uniq rev 2.绕过空格用&#xff1a; %09 <> ${IFS} $IFS$ {cat,fl*} %20 注&#xff1a; %09 ##&#xff08;Tab&#xff09; %20 ##&#xff08;spa…

请手写几种js排序算法

什么是排序算法 冒泡排序选择排序插入排序快速排序归并排序&#xff08;Merge Sort&#xff09; 思想实现测试分析动画 快速排序 &#xff08;Quick Sort&#xff09; 思想实现测试分析动画 思考&#xff1a;快排和归并用的都是分治思想&#xff0c;递推公式和递归代码也非常相…

vue3 之 商城项目—一级分类

整体认识和路由配置 场景&#xff1a;点击哪个分类跳转到对应的路由页面&#xff0c;路由传对应的参数 router/index.js import { createRouter, createWebHashHistory } from vue-router import Layout from /views/Layout/index.vue import Home from /views/Home/index.vu…

Spring Boot + 七牛OSS: 简化云存储集成

引言 Spring Boot 是一个非常流行的、快速搭建应用的框架&#xff0c;它无需大量的配置即可运行起来&#xff0c;而七牛云OSS提供了稳定高效的云端对象存储服务。利用两者的优势&#xff0c;可以为应用提供强大的文件存储功能。 为什么选择七牛云OSS? 七牛云OSS提供了高速的…

Akamai 如何揪出微软 RPC 服务中的漏洞

近日&#xff0c;Akamai研究人员在微软Windows RPC服务中发现了两个重要漏洞&#xff1a;严重程度分值为4.3的CVE-2022-38034&#xff0c;以及分值为8.8的CVE-2022-38045。这些漏洞可以利用设计上的瑕疵&#xff0c;通过缓存机制绕过MS-RPC安全回调。我们已经确认&#xff0c;所…

nodejs+vue高校实验室耗材管理系统_m20vy

用户功能&#xff1a; 登录后要有一个首页 比如:可以看见目前的耗材消耗记录&#xff0c;可做成图表菜单栏在左侧显示 1.个人信息管理 可以对基本信息进行修改&#xff0c;(修改密码时需要验证) 2.耗材管理&#xff08;耗材信息&#xff09; 普通用户可以查询当前相关耗材信息[…

问题:银行账号建立以后,一般需要维护哪些设置,不包括() #学习方法#经验分享

问题&#xff1a;银行账号建立以后&#xff0c;一般需要维护哪些设置&#xff0c;不包括&#xff08;&#xff09; A&#xff0e;维护结算科目对照 B&#xff0e;期初余额初始化刷 C&#xff0e;自定义转账定义 D&#xff0e;对账单初始化 参考答案如图所示

VMware17上安装centos7.9

一、下载安装包&#xff1a; 1、VMware安装 VMware 下载地址&#xff1a; https://www.vmware.com/cn/products/workstation-pro.html VMware下载后安装即可 安装教程可以参考VMware安装教程 2、CentOs7.9下载地址&#xff1a; http://mirrors.aliyun.com/centos/7.9.2009/iso…

【flink状态管理(三)】StateBackend的整体设计、StateBackend创建说明

文章目录 一. 状态后端概述二. StateBackend的整体设计1. 核心功能2. StateBackend的UML3. 小结 三. StateBackend的加载与初始化1. StateBackend创建概述2. StateBackend创建过程 一. 状态后端概述 StateBackend作为状态存储后端&#xff0c;提供了创建和获取KeyedStateBacke…

蓝桥杯(Web大学组)2022国赛真题:水果消消乐

思路&#xff1a; 记录点击次数&#xff0c;点击次数为1时&#xff0c;记录点击下标&#xff08;用于隐藏or消除&#xff09;、点击种类&#xff0c;点击次数为2时&#xff0c;判断该下标所对应种类与第一次是否相同 相同&#xff1a;两个都visibility:hidden &#xff08;占…