性能优化|索引优化最佳实战(二)

简介: 性能优化|索引优化最佳实战(二)

表初始化sql见 性能优化|索引优化最佳实(一)

https://my-wechat.mdnice.com/fullstack-1.png); margin-bottom: -22px;">深入索引优化


https://my-wechat.mdnice.com/fullstack-2.png); background-position: center; background-size: 30px; margin: auto; opacity: 1; background-repeat: no-repeat; margin-bottom: -8px;">我们很好奇,mysql是如何选择合适的索引的?


其实在mysql底层,使用了一个trace的工具,帮助你分析每条SQL语句,它会按照索引去查询语句,预估消耗的时间,然后再按照全表扫描,预估消耗的时间,最后比较两者消耗时间,选择是否走索引还是全表扫描,执行如下sql,查看结果2,分析trace字段的结果:


https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">
SET SESSION optimizer_trace = "enabled=on",
end_markers_in_json = ON;

SELECT
*
FROM
student
WHERE
stu_age > 10
ORDER BY
stu_age;
SELECT
*
FROM
information_schema.OPTIMIZER_TRACE;

预估表的访问成本
在这里插入图片描述


使用索引预估的访问成本


在这里插入图片描述 https://img.alicdn.com/tfs/TB1Yycwyrj1gK0jSZFuXXcrHpXa-32-32.png); display: inline-block; width: 18px; height: 18px; background-size: 18px; background-repeat: no-repeat; background-position: center; margin-right: 5px; margin-bottom: -5px;">在这里插入图片描述

比较成本大小,则决定使用索引查询。


https://my-wechat.mdnice.com/fullstack-2.png); background-position: center; background-size: 30px; margin: auto; opacity: 1; background-repeat: no-repeat; margin-bottom: -8px;">order by优化



  • SQL1

https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">explain 
SELECT
*
FROM
student
WHERE
stu_name = '刘备'
ORDER BY
stu_addr;

在这里插入图片描述 https://img.alicdn.com/tfs/TB1Yycwyrj1gK0jSZFuXXcrHpXa-32-32.png); display: inline-block; width: 18px; height: 18px; background-size: 18px; background-repeat: no-repeat; background-position: center; margin-right: 5px; margin-bottom: -5px;">在这里插入图片描述

执行计划发现使用了文件排序 using filesort,原因是由于where 后面的语句过滤字段和排序字段中间跳过了age字段,索引排序是在文件中排序。



  • SQL2

https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">explain 
SELECT
*
FROM
student
WHERE
stu_name = '刘备' and stu_age = 16
ORDER BY
stu_addr;

在这里插入图片描述 https://img.alicdn.com/tfs/TB1Yycwyrj1gK0jSZFuXXcrHpXa-32-32.png); display: inline-block; width: 18px; height: 18px; background-size: 18px; background-repeat: no-repeat; background-position: center; margin-right: 5px; margin-bottom: -5px;">在这里插入图片描述

分析执行计划,发现using filesort没有了,因为满足了最左前缀原则了。加上了stu_age字段,直接可以在索引中完成排序。



  • SQL3

https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">explain 
SELECT
*
FROM
student
WHERE
stu_name = '刘备'
ORDER BY
stu_addr,stu_age

在这里插入图片描述 https://img.alicdn.com/tfs/TB1Yycwyrj1gK0jSZFuXXcrHpXa-32-32.png); display: inline-block; width: 18px; height: 18px; background-size: 18px; background-repeat: no-repeat; background-position: center; margin-right: 5px; margin-bottom: -5px;">在这里插入图片描述

执行计划中出现了filesort,原因是排序字段与索引建立的顺序不一致,不满足最左前缀法则,mysql没有使用索引进行排序。



  • SQL4

https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">explain 
SELECT
*
FROM
student
WHERE
stu_name = '刘备'
ORDER BY
stu_age, stu_addr desc

分析执行计划,发现又出现了using filesort,使用了文件排序,按照排序确实按照最左前缀了啊,不应该啊。仔细观察发现。两个字段的排序方向不一致,这会导致索引无法进行排序,在索引树中:只能使用排序方向一致的索引,否则就需要使用文件排序了。



  • SQL5

https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">explain 
SELECT
*
FROM
student
WHERE
stu_name in('刘备','张飞')
ORDER BY
stu_age

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P8CAbS5j-1603062000913)(media/16030236994724/16030326775297.jpg)]
执行计划发现明明按照最左前缀的顺序查询和排序的,为什么又出现了文件排序呢?
因为in查询属于范围查询,会导致右侧所有索引失效。



  • order by 总结:

    • 排序字段尽量是索引字段
    • 尽量使用索引覆盖
    • where字段和排序字段遵循最左前缀
    • 出现filesort ,尽量优化成 using index,在索引中排序肯定比使用文件排序要快得多。
    • group by 和order by 优化点是一样的,默认会按照字段排序的,如果不需要排序 则可以在group by 之后加上order by null,省去排序的消耗。


https://my-wechat.mdnice.com/fullstack-2.png); background-position: center; background-size: 30px; margin: auto; opacity: 1; background-repeat: no-repeat; margin-bottom: -8px;">文件排序 using filesort


在上面优化中,发现在mysql中排序分为文件排序和索引排序,在无法使用索引排序的情况下,我们就得考虑如何优化文件排序了。
文件排序分为两种:



  • 单路排序
    单路排序是一次性取出所有满足条件的数据的所有字段,保存在sort buffer中,然后在sort buffer排序完成之后,直接返回结果。
  • 双路排序
    双路排序是取出排序的字段和能够定位数据的行标识,保存在sort_buffer中,完成排序后,再按照行标识按顺序从文件中取出需要查询的字段,这个比单路排序多了一步操作。

https://my-wechat.mdnice.com/fullstack-3.png); display: inline-block; width: 16px; height: 16px; background-size: 100%; background-position: left bottom; background-repeat: no-repeat; width: 16px; height: 15px; line-height: 15px; margin-right: 6px; margin-bottom: -2px;">单路排序和双路排序分别在什么情况下使用呢?



  • 如果查询的字段比sort buffer 空间要大(大小由系统变量max_length_for_sort_data来决定的,默认为1024字节),则使用双路排序,因为一次性无法从文件中取出所有字段放在sort buffer中。
  • 相反,如果查询的字段总长度比sort buffer 空间小,则使用单路排序。

https://my-wechat.mdnice.com/fullstack-3.png); display: inline-block; width: 16px; height: 16px; background-size: 100%; background-position: left bottom; background-repeat: no-repeat; width: 16px; height: 15px; line-height: 15px; margin-right: 6px; margin-bottom: -2px;">如何区分执行器使用的是单路排序还是双路排序呢?


我们可以使用上面提到的trace工具来分析:


https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">
SET SESSION optimizer_trace = "enabled=on",
end_markers_in_json = ON;

SELECT
*
FROM
student
WHERE
stu_name ='a'
ORDER BY
stu_addr,stu_age;
SELECT
*
FROM
information_schema.OPTIMIZER_TRACE;

https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">   {
"join_execution": {
"select#": 1,
"steps": [
{
"filesort_information": [
{
"direction": "asc",
"table": "student",
"field": "stu_addr"
},
{
"direction": "asc",
"table": "student",
"field": "stu_age"
}
] / filesort_information /,
"filesort_priority_queue_optimization": {
"usable": false,
"cause": "not applicable (no LIMIT)"
} / filesort_priority_queue_optimization /,
"filesort_execution": [
] / filesort_execution /,
"filesort_summary": {
"rows": 0,
"examined_rows": 0,
"number_of_tmp_files": 0,
"sort_buffer_size": 204800 // sort buffer大小
"sort_mode": "<sort_key, packed_additional_fields>" // 单路排序
} / filesort_summary /
}
] / steps /
} / join_execution /
}

分析trace结果中主要的字段:
sort_buffer_size:204800 mysql设置的默认值为1m
"sort_mode": "<sort_key, packed_additional_fields>" 单路排序
因为我们查询的字段长度肯定是小于1m的,我们现在设置下sort_buffer_size大小,然后再trace下结果:


https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">// 修改默认值,记得测试完之后修改回默认值(1m)
set max_length_for_sort_data = 10;

再执行上面的语句,查看结果为:


https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;">       "filesort_summary": {
"rows": 0,
"examined_rows": 0,
"number_of_tmp_files": 0,
"sort_buffer_size": 57344,
"sort_mode": "<sort_key, rowid>" // 双路排序 因为包含了rowid,需要根据rowid再去提取查询的字段。
} / filesort_summary /

sort_mode 字段中包含了rowid,所以排序模式已经更改为双路排序了。
// 设置回原来的值
set max_length_for_sort_data = 1024;


❝

微信搜一搜【乐哉开讲】关注帅气的我,回复【干货领取】,将会有大量面试资料和架构师必看书籍等你挑选,包括java基础、java并发、微服务、中间件等更多资料等你来取哦。


❞

本文使用 mdnice 排版

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
弹性计算 Java 大数据
揭秘企业数据智能创新奥秘 | 2023云栖大会倚天专场
【倚天专场】邀请了弹性计算、操作系统、平头哥、ARM中国等专家为大家解读阿里云倚天ECS实例最新进展,包括云原生处理器最新技术、龙蜥+倚天软硬件结合、倚天ECS实例最佳实践等话题,为客户上云提供一个更具“性价比”的选择,加速企业数据智能创新。
|
网络安全
编译原理复习二:Top-Down分析LL(1)文法的判断与LL(1)分析表的构造(附题目与答案 超详细)
编译原理复习二:Top-Down分析LL(1)文法的判断与LL(1)分析表的构造(附题目与答案 超详细)
1550 1
|
SQL 关系型数据库 MySQL
MySQL锁机制深度剖析:从底层原理到实战避坑,一篇吃透所有锁!
本文深入解析MySQL锁机制,涵盖全局锁、表锁、行锁、间隙锁等核心概念,结合实战案例剖析死锁、锁等待等问题根源,并提供乐观锁、索引优化等避坑方案,助你全面提升高并发场景下的数据库性能与一致性控制能力。
881 2
|
存储 人工智能 自然语言处理
RAG 实战|用 StarRocks + DeepSeek 构建智能问答与企业知识库
本文由镜舟科技解决方案架构师石强与StarRocks TSC Member赵恒联合撰写,围绕RAG(检索增强生成)技术展开,结合DeepSeek和StarRocks构建智能问答系统。RAG通过外部知识检索与AI生成相结合,解决大模型知识静态、易编造信息的问题。文章详细介绍了系统组成、操作流程及优化方法,包括DeepSeek部署、StarRocks向量索引配置、知识存储与提取等环节,并通过代码示例演示了从文本向量化到生成回答的完整过程。最后,加入RAG机制后,系统性能显著提升,支持企业级知识库与智能客服场景。文中还提供了Web可视化界面实现方案,助力开发者快速上手。
|
消息中间件 Java 测试技术
Goim框架的源码学习笔记
Goim框架的源码学习笔记
|
API Android开发
Android P 性能优化:创建APP进程白名单,杀死白名单之外的进程
本文介绍了在Android P系统中通过创建应用进程白名单并杀死白名单之外的进程来优化性能的方法,包括设置权限、获取运行中的APP列表、配置白名单以及在应用启动时杀死非白名单进程的代码实现。
767 1
|
存储 传感器 边缘计算
构建成功的边缘计算基础设施:硬件选择与关键接口
构建成功的边缘计算基础设施:硬件选择与关键接口
1006 8
|
SQL 存储 Oracle
TDengine 3.3.2.0 发布:新增 UDT 及 Oracle、SQL Server 数据接入
**TDengine 3.3.2.0 发布摘要** - 开源与企业版均强化性能,提升WebSocket、stmt模式写入与查询效率,解决死锁,增强列显示。 - taos-explorer支持geometry和varbinary类型。 - 企业版引入UDT,允许自定义数据转换。 - 新增Oracle和SQL Server数据接入。 - 数据同步优化,支持压缩,提升元数据同步速度,错误信息细化,支持表名修改。 - 扩展跨平台支持,包括麒麟、Euler、Anolis OS等。
760 0
|
Java 编译器 Apache
Java语言中的import语句:深入解析与应用
Java语言中的import语句:深入解析与应用
1835 0
|
算法 调度 C++
NR HARQ(三) semi-static HARQ-ACK codebook
UE在一个PUCCH(或PUSCH)上发送HARQ-ACK信息时,信息bit很可能是多个bits位数,这个多bits位数的HARQ-ACK信息,也称为HARQ-ACK codebook码本 UE物理层在以下几种情况下,需要产生对应的HARQ-ACK 信息bit:UE收到PDCCH调度的PDSCH,动态调度;UE接收的PDSCH没有对应的PDCCH,即DL SPS PDSCH调度; UE接收到PDCCH指示的SPS release,并没有PDSCH。

热门文章

最新文章