当前位置:网站首页>MySQL--SQL优化案例--隐式字符编码转换
MySQL--SQL优化案例--隐式字符编码转换
2022-07-17 10:17:00 【51CTO】
索性失效前提
MySQL中我们知道有
1、如果对索引字段做函数操作,可能会破坏索引值的有序性,因此优化器就决定放弃走树搜索功能。
2、隐式类型转换也会导致同样的放弃走树搜索。
因为类型转换等价于在条件字段上使用了函数比如:
一个真实的案例
下面来看看隐式字符编码转换导致的一个慢sql
优化前原始sql分析
业务上有个sql执行需要1.31秒

看看执行计划

从执行计划分析看出问题出在r表也就是 h_merge_result_new_indicator 表全表扫描,查看该表的表结构有联合索引。但是联合索引范围后会失效,于是打算新建一个联合索引。
优化初步处理

查看预新建联合索引的字段选择性

结合选择性来看
create index idx_hmrni on h_merge_result_new_indicator(keyName,module,BATCH_NO);
初步优化无效分析
创建后,再次查看执行计划依然无效

查看表结构

另外3个表结构其中有2个utf8mb4,1个utf8



字符集 utf8mb4 是 utf8 的超集,所以当这两个类型的字符串在做比较的时候,MySQL 内部的操作是,先把 utf8 字符串转成 utf8mb4 字符集,再做比较。
因此

这部分会转换后再与h_merge_result_new_indicator关联
第二次优化处理
优化就只需要将字符集编码转为utf8再和h_merge_result_new_indicator关联就能用上索引

再看查询只需要0.02秒了

第三次优化
但是还有个问题,如上执行计划key_len是606 =(100*3+3)+(100*3+3)
也就是说,没有用上BATCH_NO字段上的索引,我们知道索引少一个字段,占用会减少,不会太臃肿,因此,联合索引只需要包含r(keyName,module)
drop index idx_hmrni on h_merge_result_new_indicator;
create index idx_hmrni on h_merge_result_new_indicator(keyName,module);
结论
对索引字段做函数操作,可能会破坏索引值的有序性,因此优化器就决定放弃走树搜索功能。该例子是隐式字符编码转换,它们都跟其他条件索引上使用函数一样,因为要求在索引字段上做函数操作而导致了全索引扫描。
MySQL 的优化器确实有“偷懒”的嫌疑,即使简单地把 where id+1=1000 改写成 where id=1000-1 就能够用上索引快速查找,也不会主动做这个语句重写。
保证在条件索引上不做破坏索引值的有序性,是优化索引的利器。
边栏推荐
- mysql进阶(六)模糊查询的四种常见用法介绍
- 易贝按关键字搜索EBAY商品 API 返回值说明
- Towhee 每日模型周报
- 电脑拨号上网
- 最新水果FLStudio20.9低版本版升级高版本教程
- 认真工作后,发现周围混事的真多
- Target detection model size calculation, model complexity (parameter conversion formula)
- Use < pre > and json Stringify handles the format of web page presentation JSON
- Simple case of wechat applet calling API
- Encapsulation API, request interception, response interception, authentication timeliness
猜你喜欢

Scope and lifecycle of beans

es概念模型与基本故障

2、 Pinda general permission system__ Project construction

日志脱敏-参考

银河麒麟v10-arm版离线安装Portainer

多租户 SaaS 的数据库设计模式,你学废了吗?
![[Hongke] Introduction to genicam protocol](/img/a5/91a1d24a47ea2e3265530778d6fc1f.png)
[Hongke] Introduction to genicam protocol

解决ApplicationEventMulticaster not initialized - call ‘refresh‘ before multicasting events异常

MySQL 用户管理

【论文笔记】融合多传感器数据的抓取机械臂末端定位研究
随机推荐
Programming in the novel [serial 11] the moon bends in the yuan universe
LDA分类器
Google Play应用商店可能会删除应用权限概述 转而使用新的数据安全信息组合
SSM implementation of one-to-one query detailed tutorial (1)
DEDECMS织梦保存当前栏目更改时失败的解决方法
Latest fruit flstudio20.9 low version upgrade high version tutorial
C语言基础篇 —— 2-1 指针与野指针
Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi
银河麒麟v10-arm版离线安装Portainer
Day 7 Training
使用 Golang 正确处理五大互联网注册机构的 IP 数据
L2-029 特立独行的幸福
R语言data.table导入数据实战:data.table使用dcast.data.table函数实现透视表(pivot table)
sql server建表时设置ID字段自增 (navicat 演示)
Markdown (5): anchor link
Pyodide 中实现网络请求的 3 种方法
MySQL view
Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage
SAP Fiori 的附件处理(Attachment handling)
Programming in the novel [serial 15] the moon bends in the yuan universe