当前位置:网站首页>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:

边栏推荐
猜你喜欢

STM8L_库函数-模板搭建

都说FPGA高端,它到底能干啥?

【网络攻防】常见的网络攻防技术——黑客攻防(通俗易懂版)

Integral Topic Notes - Path Independent Conditions

Activating data potential Amazon cloud technology reshapes cloud storage "family bucket"

Leetcode - 990: equations of satisfiability

一文理解分布式开发中的服务治理
![[Mini Program Column] Summarize the development specifications of uniapp to develop small programs](/img/7b/110d324eba00652e4987bc623a5bc6.png)
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs

Kotlin 值类 - value class

电源完整性的去耦和层间耦合电容
随机推荐
HCIP --- MPLS VPN实验
SEER数据库中“手术变量(RX SUMM-SURG OTH REG/DIS )”下的字段解释
[Mini Program Column] Summarize the development specifications of uniapp to develop small programs
20220728使用电脑上的蓝牙和汇承科技的蓝牙模块HC-05配对蓝牙串口传输
BaseQuickAdapter方法getBindingAdapterPosition
Explain the problem of change exchange in simple terms - the shell of the backpack problem
Integral Special Notes-Three Formulas for Curve Area Integral
树莓派_烧写Raspberry官方镜像系统
新手必备!最全电路基础知识讲解
积分专题笔记-与路径无关条件
集合相关Collection
激活数据潜力 亚马逊云科技重塑云上存储“全家桶”
仿牛客网项目第二章:开发社区登录模块(详细步骤和思路)
HashSet and LinkedHashSet
负电压电路(原理分析)
反射技巧让你的性能提升 N 倍
qsort 函数的使用及其模拟实现
ACL 2022 | Introduce angular margin to construct comparative learning objectives and enhance text semantic discrimination ability
HashSet和LinkedHashSet
How to use Jmeter to carry out high concurrency in scenarios such as panic buying and seckill?