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.mfbz.cn/a/378698.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

【数据结构】二叉树的顺序结构及链式结构

目录 1.树的概念及结构 1.1树的概念 1.2树的相关概念 ​编辑 1.3树的表示 1.4树在实际中的运用&#xff08;表示文件系统的目录树结构&#xff09; 2.二叉树概念及结构 2.1二叉树的概念 2.2现实中的二叉树 ​编辑 2.3特殊的二叉树 2.4二叉树的性质 2.5二叉树的存储结…

2023爱分析·通用大模型市场厂商评估报告:出门问问

01 研究范围定义 研究范围&#xff1a; 大模型是指通过在海量数据上依托强大算力资源进行训练后能完成大量不同下游任务的模型。2023年以来&#xff0c;ChatGPT引爆全球大模型市场。国内众多大模型先后公测&#xff0c;众多互联网领军者投身大模型事业&#xff0c;使得大模型…

微信小程序(三十八)滚动容器

注释很详细&#xff0c;直接上代码 上一篇 新增内容&#xff1a; 1.滚动触底事件 2.下拉刷新事件 源码&#xff1a; index.wxml <view class"Area"> <!-- scroll-y 垂直滚动refresher-enabled 允许刷新bindrefresherrefresh 绑定刷新作用函数bindscrollto…

SolidWorks的抽壳

抽壳在建模的时候是比较常见的要求&#xff0c;这里废话不多说&#xff0c;直接开始实例操作。 文章目录 一、规则实体抽壳2、多面抽壳3、空心化抽壳 二、椎体抽壳三、不规则实体抽壳1、不规则实体2、部分实体抽壳 文章随时可能更新&#xff0c;请关注文章原出处&#xff1a; …

【GAMES101】Lecture 19 透镜

目录 理想的薄透镜 模糊 利用透镜模型做光线追踪 景深&#xff08;Depth of Field&#xff09; 理想的薄透镜 在实际的相机中都是用的一组透镜来作为这个镜头 这个因为真实的棱镜无法将光线真正聚焦到一个点上&#xff0c;它只能聚在一堆上 所以方便研究提出了一种理想化的…

vue3的常用功能

文章目录 一、前言二、自动注册全局组件2.1、自动注册components目录下所有vue组件并以组件的文件名为组件的名称2.2、使用这个插件2.3、为全局组件添加类型提示 三、函数式图片预览四、手动封装 svgIcon 组件五、封装拖拽钩子函数六、vscode 中 vue3 代码片段七、最后 一、前言…

C#静态数组删除数组元素不改变数组长度 vs 动态数组删除数组元素改变数组长度

目录 一、使用的方法 1.对静态数组删除指定长度并不改变数长度的方法 &#xff08;1&#xff09;静态数组 &#xff08;2&#xff09;对静态数组删除元素不得改变其长度 2.对动态数组删除指定长度并改变数长度的方法 &#xff08;1&#xff09;动态数组 &#xff08;2&a…

Log4j2漏洞(二)3种方式复现反弹shell

★★免责声明★★ 文章中涉及的程序(方法)可能带有攻击性&#xff0c;仅供安全研究与学习之用&#xff0c;读者将信息做其他用途&#xff0c;由Ta承担全部法律及连带责任&#xff0c;文章作者不承担任何法律及连带责任。 1、前言 明天就是除夕了&#xff0c;提前祝大家&#x…

2023年06月CCF-GESP编程能力等级认证C++编程二级真题解析

一、单选题(每题2分,共30分) 第1题 高级语言编写的程序需要经过以下()操作,可以生成在计算机上运行的可执行代码。 A. 编辑 B. 保存 C. 调试 D. 编译 答案:D 第2题 能够实现下面流程图功能的伪代码是( )。 A. if 条件判断 then 语句块 B. if 条件判断 then 什么…

使用x86架构+Nvidia消费显卡12G显存,搭建智能终端,将大模型本地化部署,说不定是未来方向,开源交互机器人设计

1&#xff0c;大模型本地部署 视频说明地址&#xff1a; https://www.bilibili.com/video/BV1BF4m1u769/ 【创新思考】&#xff08;1&#xff09;&#xff1a;使用x86架构Nvidia消费显卡12G显存&#xff0c;搭建智能终端&#xff0c;将大模型本地化部署&#xff0c;语音交互机…

Git中为常用指令配置别名

目录 1 前言 2 具体操作 2.1 创建.bashrc文件 2.2 添加指令 2.3 使其生效 2.4 测试 1 前言 在Git中有一些常用指令比较长&#xff0c;当我们直接输入&#xff0c;不仅费时费力&#xff0c;还容易出错。这时候&#xff0c;如果能给其取个简短的别名&#xff0c;那么事情就…

一文读懂转融通

最近多家公司都在讲解关于转融通要求。今天我们就来详细聊聊&#xff01; 转融通是一种证券借贷机制&#xff0c;它允许机构投资者在融资融券交易中借入或借出证券。 具体来说&#xff0c;机构投资者可以向券商借入证券&#xff0c;或者将持有的证券借给券商&#xff0c;以扩大…

开源项目的三年,我的项目经历了哪些变化?

0.前言 自己一个项目写了三年&#xff0c;到底写了什么东西了&#xff0c;这个项目经历了哪些变化呢&#xff1f;其中的心路历程如何&#xff1f; 兄弟们&#xff0c;要是感觉我的项目有价值&#xff0c;去b站给俺点点关注呐。我更新的更快。点击下面的了解就可以跳转去b站。…

【VTKExamples::PolyData】第二十三期 InterpolateMeshOnGrid

很高兴在雪易的CSDN遇见你 VTK技术爱好者 QQ:870202403 前言 本文分享VTK样例InterpolateMeshOnGrid,并解析接口vtkProbeFilter 、vtkWarpScalar & vtkDealuany2D等多个接口,希望对各位小伙伴有所帮助! 感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!…

LabVIEW多任务实时测控系统

LabVIEW多任务实时测控系统 面对现代化工业生产的复杂性和多变性&#xff0c;传统的测控系统已难以满足高效、精准、可靠的监控和控制需求。因此&#xff0c;开发一种基于LabVIEW的智能测控系统&#xff0c;能够提高生产效率&#xff0c;保证生产安全&#xff0c;是解决现代工…

java 批量获取修改mp3文件标题、艺术家等内容

前言 java 批量获取&修改mp3文件标题、艺术家等内容 最近下了一本有声小说&#xff0c;文件名称是每一集的集数&#xff0c;但标题全是网址&#xff0c;放到手机音乐播放器名称显示的是标题&#xff0c;完全看不出是哪一集&#xff0c;200多集如果一集一集改怕是手得废掉…

【Spring源码解读!底层原理进阶】【上】探寻Spring内部:BeanFactory和ApplicationContext实现原理揭秘✨

&#x1f389;&#x1f389;欢迎光临&#x1f389;&#x1f389; &#x1f3c5;我是苏泽&#xff0c;一位对技术充满热情的探索者和分享者。&#x1f680;&#x1f680; &#x1f31f;特别推荐给大家我的最新专栏《Spring 狂野之旅&#xff1a;底层原理高级进阶》 &#x1f680…

MySQL8.0 最新版下载安装,手把手式的教程,小白都能安装成功!!

1、打开MySQL数据库官网的下载地址,根据自己的操作系统下载对应的安装包。我这里是Windows的,操作步骤如下: https://dev.mysql.com/downloads/installer/2、选择不登录下载 3、等待下载完成 4、然后点击下载下来的安装包 5、选择安装类型,Custom自定义安装 6、接…

io三个练习:

练习一&#xff1a; 使用 四种方式拷贝文件&#xff0c;并统计各自用时 1字节流的基本流&#xff1a;一次读写一个字节 2字节流的基本流&#xff1a;一次读写一个字节数组 3字节缓冲流&#xff1a;一次读写一个字节 4字节缓冲流&#xff1a;一次读写一个字节数组 public clas…

2024幻兽帕鲁服务器多少钱一套?

2024年幻兽帕鲁服务器价格表更新&#xff0c;阿里云、腾讯云和华为云Palworld服务器报价大全&#xff0c;4核16G幻兽帕鲁专用服务器阿里云26元、腾讯云32元、华为云26元&#xff0c;阿腾云atengyun.com分享幻兽帕鲁服务器优惠价格表&#xff0c;多配置报价&#xff1a; 幻兽帕鲁…
最新文章