当前位置:网站首页>MySQL -- SQL optimization case -- implicit character encoding conversion
MySQL -- SQL optimization case -- implicit character encoding conversion
2022-07-19 09:25:00 【51CTO】
Simply fail premise
MySQL We know there are
1、 If you do a function operation on the index field , May break the order of index values , So the optimizer decided to give up the tree search function .
2、 Implicit type conversion will also lead to the same abandonment of tree walking search .
Because type conversion is equivalent to using functions on condition fields, such as :
A real case
Let's take a look at a slow problem caused by implicit character encoding conversion sql
Original before optimization sql analysis
There is a business sql Execution requires 1.31 second

Look at the implementation plan

From the analysis of the implementation plan, we can see that the problem lies in r Watch is h_merge_result_new_indicator Table full table scan , Check that the table structure of this table has a joint index . However, the union index range will be invalid , So I plan to create a new joint index .
Optimize preliminary treatment

View the fields of the pre created union index

Combined with selectivity
create index idx_hmrni on h_merge_result_new_indicator(keyName,module,BATCH_NO);
Preliminary optimization invalidation analysis
After creation , Viewing the execution plan again is still invalid

View table structure

in addition 3 A table structure with 2 individual utf8mb4,1 individual utf8



Character set utf8mb4 yes utf8 Superset , So when these two types of strings are compared ,MySQL The internal operation is , The first utf8 String to utf8mb4 Character set , To compare .
therefore

This part will be converted and then connected with h_merge_result_new_indicator relation
The second optimization
Optimization only needs to convert the character set encoding to utf8 And again h_merge_result_new_indicator The association can use the index

If you look at the query again, you only need 0.02 The second

Third optimization
But there's a problem , The above implementation plan key_len yes 606 =(100*3+3)+(100*3+3)
in other words , It doesn't work BATCH_NO Index on field , We know that the index is missing one field , Occupancy will be reduced , Not too bloated , therefore , The federated index only needs to contain 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);
Conclusion
Function operation on index field , May break the order of index values , So the optimizer decided to give up the tree search function . This example is an implicit character encoding conversion , They are the same as functions used on other conditional indexes , Full index scanning is caused by requiring function operation on index field .
MySQL Our optimizer does have “ lazy ” The suspicion of , Even if you simply put where id+1=1000 Rewrite into where id=1000-1 You can use the index to quickly find , I won't take the initiative to rewrite this statement .
Ensure that the order of index values is not destroyed in the conditional index , Is a powerful tool for optimizing indexes .
边栏推荐
- Line Flow Based Simultaneous Localization and Mapping
- 多租户 SaaS 的数据库设计模式,你学废了吗?
- codeforces每日5题(均1500)-第十七天
- 组件化高级--插槽
- SSM implementation of one-to-one query detailed tutorial (1)
- Fundamentals of C language -- 2-1 pointer and wild pointer
- How to synchronize historical data when MySQL is upgraded to primary and standby?
- ETCD数据库源码分析——etcdserver bootstrap从快照中恢复store
- After working hard, I found that there were so many messes around
- 如何在监控主机上单独部署agent——WGCLOUD
猜你喜欢

第一部分—C语言基础篇_5. 数组和字符串

开发第一个Flink应用

How is MySQL data stored on disk?

un7.16:如何在码云上部署项目并邀请组员?

Markdown (5): anchor link

Solve interface cross domain problems and node operation MySQL

Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)

将视频格式转换为gif图片格式

Es conceptual model and basic faults

Simple third-party component log desensitization
随机推荐
AcWing 257. 关押罪犯 题解(二分图)
C语言基础篇 —— 2-2 const关键字与指针
un7.16:如何在码云上部署项目并邀请组员?
Pytorch框架之优化器 Optimizer
MySQL--SQL优化案例--隐式字符编码转换
第一部分—C语言基础篇_6. 函数
07---布儒斯特角
Static routing!! Static routing!! Static routing!!
OLED displays how to understand the character sizes of 12*6, 16*8, 24*12, etc
C语言基础篇 —— 2-3 指针与数组
两个结构体 ifconf 和 ifreq
Solve interface cross domain problems and node operation MySQL
Fundamentals of C language -- 2-2 const keywords and pointers
C——指针
Codeworks 5 questions per day (average 1500) - day 17
Leetcode 197 Rising temperature (July 16, 2022)
易贝按关键字搜索EBAY商品 API 返回值说明
Could NOT find CUDA (missing: CUDA_INCLUDE_DIRS) (found suitable exact version “11.4“)
【Flink】Flink 设置检查点失败一次就报错 setTolerableCheckpointFailureNumber 不起作用
代码庆端午--粽你心意