当前位置:首页 > 文章列表 > 数据库 > MySQL > GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(下)

GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(下)

来源:SegmentFault 2023-01-20 11:16:57 0浏览 收藏

对于一个数据库开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(下)》,主要介绍了MySQL,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

背景

接着 https://mengkang.net/1328.html 的案例,我们继续磕。
上一篇 GDB 调试 Mysql 实战(三)优先队列排序算法探究(上) 分析了实验3中的

select `aid`,sum(`pv`) as num from article_rank force index(idx_day_aid_pv)  where `day`>20190115 group by aid order by num desc LIMIT 10;

{
  "join_execution": {
    "select#": 1,
    "steps": [
      {
        "creating_tmp_table": {
          "tmp_table_info": {
            "table": "intermediate_tmp_table",
            "row_length": 20,
            "key_length": 4,
            "unique_constraint": false,
            "location": "memory (heap)",
            "row_limit_estimate": 838860
          }
        }
      },
      {
        "converting_tmp_table_to_ondisk": {
          "cause": "memory_table_size_exceeded",
          "tmp_table_info": {
            "table": "intermediate_tmp_table",
            "row_length": 20,
            "key_length": 4,
            "unique_constraint": false,
            "location": "disk (InnoDB)",
            "record_format": "fixed"
          }
        }
      },
      {
        "filesort_information": [
          {
            "direction": "desc",
            "table": "intermediate_tmp_table",
            "field": "num"
          }
        ],
        "filesort_priority_queue_optimization": {
          "limit": 10,
          "rows_estimate": 1057,
          "row_size": 36,
          "memory_available": 262144,
          "chosen": true
        },
        "filesort_execution": [
        ],
        "filesort_summary": {
          "rows": 11,
          "examined_rows": 649091,
          "number_of_tmp_files": 0,
          "sort_buffer_size": 488,
          "sort_mode": ""
        }
      }
    ]
  }
}

row_size 为什么是 36

(gdb) b Sort_param::init_for_filesort
Breakpoint 1 at 0xf1a89f: file /root/newdb/mysql-server/sql/filesort.cc, line 107.

image.png

(gdb) b Filesort::get_addon_fields
Breakpoint 2 at 0xf21231: file /root/newdb/mysql-server/sql/filesort.cc, line 2459.
(gdb) b /root/newdb/mysql-server/sql/filesort.cc:2496
Breakpoint 3 at 0xf212f9: file /root/newdb/mysql-server/sql/filesort.cc, line 2496.
(gdb) b /root/newdb/mysql-server/sql/filesort.cc:2523
Breakpoint 4 at 0xf2145f: file /root/newdb/mysql-server/sql/filesort.cc, line 2523.

image.png

排序字段还是实验3一样是16字节,后面20字节则是两个字段相加20字节+

(gdb) b /root/newdb/mysql-server/sql/filesort.cc:320
Breakpoint 5 at 0xf1b1d9: file /root/newdb/mysql-server/sql/filesort.cc, line 320.
...
Breakpoint 5, filesort (thd=0x7f0214000d80, filesort=0x7f021401f668, sort_positions=false, examined_rows=0x7f022804d050,
    found_rows=0x7f022804d048, returned_rows=0x7f022804d040) at /root/newdb/mysql-server/sql/filesort.cc:320
320      num_rows= table->file->estimate_rows_upper_bound();
(gdb) s
ha_innobase::estimate_rows_upper_bound (this=0x7f0214022b50)
    at /root/newdb/mysql-server/storage/innobase/handler/ha_innodb.cc:13655
ha_innobase::estimate_rows_upper_bound (this=0x7f0214022b50)
    at /root/newdb/mysql-server/storage/innobase/handler/ha_innodb.cc:13655
warning: Source file is more recent than executable.
13655        DBUG_ENTER("estimate_rows_upper_bound");
(gdb) n
13661        update_thd(ha_thd());
(gdb) n
13663        TrxInInnoDB    trx_in_innodb(m_prebuilt->trx);
(gdb) n
13665        m_prebuilt->trx->op_info = "calculating upper bound for table rows";
(gdb) n
13667        index = dict_table_get_first_index(m_prebuilt->table);
(gdb) n
13669        ulint    stat_n_leaf_pages = index->stat_n_leaf_pages;
(gdb) p stat_n_leaf_pages
$19 = 139646902217632
(gdb) n
13671        ut_a(stat_n_leaf_pages > 0);
(gdb) p UNIV_PAGE_SIZE
No symbol "UNIV_PAGE_SIZE" in current context.
(gdb) n
13674            ((ulonglong) stat_n_leaf_pages) * UNIV_PAGE_SIZE;
(gdb) n
13681        estimate = 2 * local_data_file_length
(gdb) p local_data_file_length
$20 = 16384
(gdb) p stat_n_leaf_pages
$21 = 1
(gdb) n
13682            / dict_index_calc_min_rec_len(index);
(gdb) n
13684        m_prebuilt->trx->op_info = "";
(gdb) p estimate
$22 = 1057
(gdb) p dict_index_calc_min_rec_len(index)
$23 = 31

image.png

也就是说

ut_a(stat_n_leaf_pages > 0);

local_data_file_length =
    ((ulonglong) stat_n_leaf_pages) * UNIV_PAGE_SIZE;

/* Calculate a minimum length for a clustered index record and from
that an upper bound for the number of rows. Since we only calculate
new statistics in row0mysql.cc when a table has grown by a threshold
factor, we must add a safety factor 2 in front of the formula below. */

estimate = 2 * local_data_file_length
    / dict_index_calc_min_rec_len(index);

(2*16*1024)/31  = 1057
,那么为什么
dict_index_calc_min_rec_len
是31呢?

继续查看源码发现 31 是这么计算出来的,就算知道了31,但是我也还是木有弄懂,为什么扫描行数是

(2*页内存大小)/索引最小行记录长度

12233.001.jpeg

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于数据库的相关知识,也可关注golang学习网公众号。

版本声明
本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
windows下重装xampp并做mysql数据迁移的步骤windows下重装xampp并做mysql数据迁移的步骤
上一篇
windows下重装xampp并做mysql数据迁移的步骤
个人博客一|抓取崔庆才个人博客网站前端源码
下一篇
个人博客一|抓取崔庆才个人博客网站前端源码
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    543次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    516次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    500次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    485次学习
查看更多
AI推荐
  • H2O EvalGPT:开源LLM大模型评估与排行榜工具
    H2O EvalGPT
    H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
    49次使用
  • SuperCLUE中文大模型评测基准:功能、能力维度与应用指南
    SuperCLUE
    SuperCLUE是权威的中文大语言模型综合评测基准,涵盖语言理解、知识应用、AI Agent智能体及安全性等12项核心能力。通过多轮对话与客观测试,定期发布榜单与技术报告,为模型研发、优化及行业选型提供科学依据。
    201次使用
  • C-Eval中文评测基准:大语言模型多学科能力评估指南
    C-Eval
    深入了解C-Eval中文评估套件,涵盖52个学科与4级难度。本文详解其功能特点、Zero-shot/Few-shot使用方法及代码示例,助您全面评测LLM中文理解与泛化能力。
    137次使用
  • AI Prompt Library:免费AI提示词库,助力ChatGPT高效创作与营销
    AI Prompt Library
    探索AI Prompt Library免费资源库,涵盖营销、写作及多场景AI提示词。兼容ChatGPT、Claude等工具,一键复制优化输出,提升工作效率。
    68次使用
  • Generrated:DALL·E 2/3 AI绘画提示词灵感库与图像对比平台
    Generrated
    Generrated汇集9300+张DALL·E生成图像及对应提示词,支持查看完整图集、对比DALL·E 2与3版本差异,是AI绘图新手学习Prompt设计与获取创作灵感的实用工具。
    48次使用