当前位置:网站首页>Using IN in MySQL will not go through index analysis and solutions
Using IN in MySQL will not go through index analysis and solutions
2022-07-30 09:33:00 【Li Bai loves tea】
One: Analyze why all MySQL In queries do not take effect
Conclusion: IN will definitely follow the index, but when the value range of IN is large, it will lead to Invalid index, go full table scan
navicatThe visualization tool uses the explain function to view sql execution information
1.1 Scenario 1: When the value in IN has only one primary key
We only need to pay attention to one of the most important types of information. It is obvious whether the withdrawal uses the index:
The type result value in order from good to bad is:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
all:full table scan
index: Another form of full table scan, but his scan method is in the order of the index
range: Index scan with a range. Compared with the full table scan of index, it has a range limit, so it is better than index
ref: The search condition column uses an index and is not the primary key and unique.In fact, it means that although the index is used, the value of the index column is not unique and there are repetitions.In this way, even if the index is used to quickly find the first piece of data, it still cannot be stopped, and a small range scan near the target value must be performed.But its advantage is that it does not need to scan the entire table, because the index is ordered, and even if there are duplicate values, it is scanned in a very small range.
const: Normally, if a primary key is placed behind where as a conditional query, the mysql optimizer can convert this query optimization into aconstant.How and when to convert depends on the optimizer
Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref. When index and all appear in the type, it means that the full table scan is performed without the index, which is inefficient. At this time, the SQL needs to be tuned..
When the Using filesor or Using temproary appears in the extra, it means that the index cannot be used and must be optimized as soon as possible.
possible_keys: the index used by sql
key: Displays the key (index) that MySQL actually decided to use.If no index is selected, the key is NULL
rows: Displays the number of rows that MySQL thinks it must examine when executing the query.
1.2 Scenario 2: Expand the value range in IN
At this time, the index is still gone, but the efficiency is reduced
1.3 Scenario 3: Continue to expand the value range of IN
Looking at the above figure, I found that there is no index at this time, but a full table scan.
I'm talking about the conclusion
Conclusion: IN will definitely go through the index, but when the value range of IN is large, the index will be invalid, and a full table scan will be performed.
By the way: If not in is used, the index is not taken.
Second: What should I do if MySQL needs IN query but it is very slow?
From the above, we know that our IN query index does not take effect and why it does not take effect.
2.1 This is a common IN query
SELECT id, order_index, data_order_start, update_time, create_time, gov_frame_idFROM gov_price_category_detailWHEREgov_frame_id IN (SELECT id FROM gov_price_frame WHERE deleted=1 AND is_spider=0 AND city IN ( 'Changsha City' ) GROUP BY id)AND deleted=1AND data_order_start < 51
2.2 We transform IN query into inner query
SELECT gcd.id, gcd.order_index, gcd.data_order_start, gcd.update_time, gcd.create_time, gcd.gov_frame_idFROM gov_price_category_detail gcd , ( SELECT gp.id FROM gov_price_frame gp WHERE gp.deleted=1 AND gp.is_spider=0 AND gp.city IN ( 'Changsha City' ) GROUP BY gp.id ) gpfWHEREgpf.id = gcd.gov_frame_idAND gcd.deleted=1AND gcd.data_order_start < 51
3.3 optimized speed comparison
Raw SQL speed information:
Optimized SQL speed information:
边栏推荐
猜你喜欢
How to avoid CMDB becoming a data island?
用示波器揭示以太网传输机制
Integral Special Notes-Three Formulas for Curve Area Integral
C language classic practice questions (3) - "Hanoi Tower (Hanoi)"
剖析SGI STL空间配置器(allocate内存分配函数)
02-课程发布
20220728使用电脑上的蓝牙和汇承科技的蓝牙模块HC-05配对蓝牙串口传输
MySQL [operator]
TreeSet解析
FPGA基础协议二:I2C读写E²PROM
随机推荐
一文读懂二十种开关电源拓扑结构
【无标题】
树莓派_烧写Raspberry官方镜像系统
电源完整性基础知识
STM8L_库函数-模板搭建
2022/07/29 学习笔记 (day19)异常处理
Scala
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
XP电源维修fleXPower电源X7-2J2J2P-120018系列详解
一个低级错误导致的诡异现象——走近科学能拍三集,(C语言)最简单的数组元素读取,不正确!?
【蓝桥杯选拔赛真题45】Scratch猫鼠游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
【零基础玩转BLDC系列】以GD32F30x为例定时器相关功能详解
如何避免CMDB沦为数据孤岛?
【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)
[Unity]UI切换环形滚动效果
ant-design form form verification upload component (with personal packaged upload component)
Golang DES 加解密如何实现?
Jetpack Compose 从入门到入门(八)
电路分析:运放和三极管组成的恒流源电路
DDR、GDDR、QDR的区别