MySQL优化器

news2024/10/6 14:35:48

优化器

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.coloradmin.cn/o/1443929.html

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

相关文章

大模型实战营第二期——3. 基于 InternLM 和 LangChain 搭建你的知识库

github地址&#xff1a;InternLM/tutorial-书生浦语大模型实战营文档地址&#xff1a;基于 InternLM 和 LangChain 搭建你的知识库视频地址&#xff1a;基于 InternLM 和 LangChain 搭建你的知识库Intern Studio: https://studio.intern-ai.org.cn/console/instance动手学大模型…

如何在苹果Mac上进行分屏,多任务处理?

Apple 在 macOS Catalina 中引入了 Split View&#xff0c;让您可以同时查看两个应用程序。如果同时处理多个应用程序&#xff0c;但在它们之间切换时感到沮丧&#xff0c;小编教给大家在 Macbook Pro/Air 或 iMac 上使用分屏功能流畅地进行多任务处理。 注意&#xff1a;您可…

C# 委托(delegate)本质理解

目录 代码如下&#xff0c;很简单 运行的结果 反编译程序查看 关注两点&#xff1a; 什么是委托 委托的三个步骤 委托的意义 代码如下&#xff0c;很简单 namespace Delegate { class Program { delegate void SayHi(); void SayHi_1() …

专业135+总400+中国科学院大学859国科大信号与系统考研经验电子信息与通信,真题,大纲,参考书

今年考研专业课859信号与系统135&#xff0c;总分400上岸国科大&#xff0c;总结一下自己这一年的复习经验&#xff0c;希望对后面报考中科院大学的同学有所帮助。 专业课&#xff1a; 国科大不同研究所都是统一命题&#xff0c;859信号与系统的参考书目是郑君里的《信号与系…

移动光猫gs3101超级密码及改桥接模式教程

文章目录 超级管理员账号改桥接模式路由器连接光猫&#xff0c;PPPOE拨号即可&#xff01;附录&#xff1a;如果需要改桥接的话不知道拨号密码咋办打开光猫Telnet功能Telnet 登录 参考文章 移动光猫吉比特GS3101超级账号获取更改桥接 移动光猫gs3101超级密码及改桥接模式教程 …

C#入门及进阶|数组和集合(六):集合概述

1.集合概述 数组是一组具有相同名称和类型的变量集合&#xff0c;但是数组初始化后就不便于再改变其大小&#xff0c;不能实现在程序中动态添加和删除数组元素&#xff0c;使数组的使用具有很多局限性。集合能解决数组存在的这个问题&#xff0c;下面我们来学习介绍集合…

微服务入门篇:http客户端Feign(远程调用,自定义配置,Feign的性能优化,Feign服务抽取)

目录 1.基于Feign的远程调用1.RestTemplate方式调用存在的问题2.Feign的介绍3.定义和使用Feign客户端 2.自定义配置1.方式一&#xff1a;配置文件方式2.方式二: java代码方式&#xff0c;需要先声明一个Bean: 3.Feign的性能优化1.Feign底层的客户端实现2.连接池配置 4.Feign的最…

春节假期:思考新一年的发展思路

春节假期是人们放松身心、享受家庭团聚的时刻&#xff0c;但除了走亲戚、玩、吃之外&#xff0c;我们确实也需要思考新的一年的发展思路。以下是一些建议&#xff0c;帮助您在春节假期中为新的一年做好准备&#xff1a; 回顾过去&#xff0c;总结经验&#xff1a;在春节期间&a…

Blazor 子组件交互例子

源码 子组件 SwitchBar.razor &#xfeff;using Microsoft.Extensions.Logging inject ILogger<Index> Logger<div style"ClassString" onclick"OnClick">ChildContent </div>code {[Parameter]public RenderFragment? ChildContent…

AJAX——认识URL

1 什么是URL&#xff1f; 统一资源定位符&#xff08;英语&#xff1a;Uniform Resource Locator&#xff0c;缩写&#xff1a;URL&#xff0c;或称统一资源定位器、定位地址、URL地址&#xff09;俗称网页地址&#xff0c;简称网址&#xff0c;是因特网上标准的资源的地址&…

LeetCode---383周赛

题目列表 3028. 边界上的蚂蚁 3029. 将单词恢复初始状态所需的最短时间 I 3030. 找出网格的区域平均强度 3031. 将单词恢复初始状态所需的最短时间 II 一、边界上的蚂蚁 这题没什么好说的&#xff0c;模拟就行&#xff0c;本质就是看前缀和有几个为0。 代码如下 class S…

【制作100个unity游戏之25】3D背包、库存、制作、快捷栏、存储系统、砍伐树木获取资源、随机战利品宝箱1(附带项目源码)

效果演示 文章目录 效果演示系列目录前言人物和视角基本控制简单的背包系统和物品交互绘制背包UI脚本控制 源码完结 系列目录 前言 欢迎来到【制作100个Unity游戏】系列&#xff01;本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第25篇中&#xff0c;我们将…

游戏服务器哪家强?国内几款主流云服务器测评

游戏服务器租用多少钱一年&#xff1f;1个月游戏服务器费用多少&#xff1f;阿里云游戏服务器26元1个月、腾讯云游戏服务器32元&#xff0c;华为云26元&#xff0c;游戏服务器配置从4核16G、4核32G、8核32G、16核64G等配置可选&#xff0c;游戏专业服务器公网带宽10M、12M、15M…

【大厂AI课学习笔记】【1.6 人工智能基础知识】(1)人工智能、机器学习、深度学习之间的关系

6.1 人工智能、机器学习与深度学习的关系 必须要掌握的内容&#xff1a; 如上图&#xff1a;人工智能>机器学习>深度学习。 机器学习是人工智能的一个分支&#xff0c;该领域的主要研究对象是人工智能&#xff0c;特别是如何在经验学习中改进具体算法的性能。 深度学习…

算法||实现典型数据结构的查找、添加和删除数据 并分析其时间和空间复杂度

实现典型数据结构的查找、添加和删除数据 并分析其时间和空间复杂度 线性结构&#xff1a; 数组&#xff1a;是一种线性表数据结构&#xff0c;它用一组连续的内存空间&#xff0c;来存储一组具有相同类型的数据。 查找数据 &#xff1a;随机访问 流程图 /** 查询元素下标…

02 数据库管理 数据表管理

文章目录 数据库管理数据表管理基础数据类型表的基本操作 数据库管理 查看已有库 show databases; 创建库 create database 库名 [character set utf8]; e.g. 创建stu数据库&#xff0c;编码为utf8 create database stu character set utf8; create database stu charsetutf8;…

LSF 主机状态 unreach 分析

在LSF集群运行过程中&#xff0c;有主机状态变为 unreach。熟悉LSF的朋友都知道主机状态为 unreach 表示主机上的 SBD 服务中断服务了&#xff0c;但其它服务 LIM 和 RES 还在正常运行。 影响分析 那么主机上的 SBD 服务中断的影响是什么呢&#xff1f; 我们需要先明白 SBD …

Java 学习和实践笔记(6)

各数据类型所占的空间&#xff1a; byte: 1个字节 short&#xff1a;2个字节 int&#xff1a;4个 long&#xff1a;8个 float&#xff1a;4个 double: 8个 char:1个 boolean:1bit 所有引用数据类型都是4个字节&#xff0c;实际其值是指向该数据类型的地址。 上图中稍特…

使用Softing edgeConnector模块将云轻松连接到Siemens PLC

一 工业边缘的连接解决方案 云服务提供商 (CSP) 引入了服务和功能&#xff0c;以简化基于云的工业物联网解决方案的实施。Azure Industrial IoT Platform或AWS IoT SiteWise支持标准协议和接口&#xff0c;例如OPC UA或MQTT。但是&#xff0c;如果您希望在典型的旧改项目中连接…

Debezium发布历史122

原文地址&#xff1a; https://debezium.io/blog/2022/05/04/switch-to-java-11/ 欢迎关注留言&#xff0c;我是收集整理小能手&#xff0c;工具翻译&#xff0c;仅供参考&#xff0c;笔芯笔芯. Switching to Java 11/17 May 4, 2022 by Vojtěch Jurnek community news 你可…