当前位置:网站首页>Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
Don't ask me again why MySQL hasn't left the index? For these reasons, I'll tell you all
2022-07-01 15:25:00 【One lamp architecture】
In the work , I often have problems like this , I am clearly in MySQL The table is indexed , Why execute SQL The index is not used when querying ?
Same article SQL Sometimes queries use indexes , Sometimes the index is not used , What's the matter ?
The reason may be that the index is invalid , There are several reasons for failure , See if you have stepped on a similar pit ?
1. Data preparation :
There is such a user table , stay name Build an index on the field :
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`name` varchar(255) DEFAULT NULL COMMENT ' full name ',
`age` int DEFAULT NULL COMMENT ' Age ',
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB COMMENT=' User table ';
2. Explain Detailed explanation :
Want to view a SQL Whether index is used ? What type of index is used ?
have access to explain keyword , see SQL Implementation plan . for example :
explain select * from user where id=1;
You can see type=const, Indicates that the primary key index is used .
explain All of the type The types are as follows :
3. Cause of failure
1. Data type implicit conversion
name The fields are varchar type , If we use data type query , Data type conversion will occur , Although not wrong , But the index cannot be used .
explain select * from user where name=' A lamp ';
explain select * from user where name=18;
2. Fuzzy query like With % start
explain select * from user where name like ' Zhang %';
explain select * from user where name like '% Zhang ';
3. or Index not used before and after
although name The field is indexed , however age Field has no index , Use or The whole table will be scanned when .
# or Index not used before and after , Causes a full table scan
explain select * from user where name=' A lamp ' or age=18;
4. Joint index , The first column index is not used
If we were (name,age) On , Set up a joint index , But only age Field , Indexes are also unavailable .
Use union index , The leftmost matching principle must be followed , First use the first column of fields , Then use the second column field .
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`name` varchar(255) DEFAULT NULL COMMENT ' full name ',
`age` int DEFAULT NULL COMMENT ' Age ',
PRIMARY KEY (`id`),
KEY `idx_name_age` (`name`,`age`)
) ENGINE=InnoDB COMMENT=' User table ';
5. Calculate in the index field
If we calculate in the index column , Indexes are also unavailable .
# Calculate on the primary key index , Causes a full table scan
explain select * from user where id+1=2;
6. Use functions on index fields
If we use functions in index columns , Indexes are also unavailable .
7. The index optimizer is wrong
Same article SQL Sometimes queries use indexes , Sometimes the index is not used , What's the matter ?
This may be the result of optimizer selection , You will choose whether to use the index according to the amount of data in the table .
When most of the table name It's all a light , Use at this time name=' A lamp ' Make a query , Will indexes be used ?
The index optimizer will think , Using an index is not as fast as a full table scan , Simply do not index .
Of course, we think the optimizer is not optimized properly , You can also use force index Force index .
Summary of knowledge points :
边栏推荐
- S32K1xx 微控制器的硬件设计指南
- MySQL审计插件介绍
- Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching
- How to realize clock signal frequency division?
- 微信网页订阅消息实现
- Flink 系例 之 TableAPI & SQL 与 MYSQL 分组统计
- An intrusion detection model
- Create employee data in SAP s/4hana by importing CSV
- "Qt+pcl Chapter 6" point cloud registration ICP Series 6
- opencv学习笔记五--文件扫描+OCR文字识别
猜你喜欢

An intrusion detection model

"Qt+pcl Chapter 6" point cloud registration ICP Series 6

idea中新建的XML文件变成普通文件的解决方法.

Returning to the top of the list, the ID is still weak

Fix the failure of idea global search shortcut (ctrl+shift+f)

微信小程序03-文字一左一右显示,行内块元素居中

leetcode:329. 矩阵中的最长递增路径

opencv学习笔记六--图像拼接

做空蔚来的灰熊,以“碰瓷”中概股为生?

张驰咨询:锂电池导入六西格玛咨询降低电池容量衰减
随机推荐
TS reports an error don't use 'object' as a type The `object` type is currently hard to use
将ABAP On-Premises系统连接到中央检查系统以进行自定义代码迁移
Create employee data in SAP s/4hana by importing CSV
Skywalking 6.4 distributed link tracking usage notes
Hardware design guide for s32k1xx microcontroller
MySQL service is starting. MySQL service cannot be started. Solution
微信网页订阅消息实现
swiper 轮播图,最后一张图与第一张图无缝衔接
DirectX repair tool v4.1 public beta! [easy to understand]
【一天学awk】条件与循环
Can I choose to open an account on Great Wall Securities? Is it safe?
The difference between arrow function and ordinary function in JS
【锁】Redis锁 处理并发 原子性
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
idea中新建的XML文件变成普通文件的解决方法.
Opencv learning notes 5 -- document scanning +ocr character recognition
What are the test items of juicer ul982
Intelligent operation and maintenance practice: banking business process and single transaction tracking
phpcms后台上传图片按钮无法点击
采集数据工具推荐,以及采集数据列表详细图解流程