当前位置:网站首页>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 :
边栏推荐
- SAP s/4hana: one code line, many choices
- MySQL 服务正在启动 MySQL 服务无法启动解决途径
- 《QT+PCL第六章》点云配准icp系列4
- Hardware design guide for s32k1xx microcontroller
- phpcms后台上传图片按钮无法点击
- SQL常用的四个排序函数梳理
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- 榨汁机UL982测试项目有哪些
- Implementation of deploying redis sentry in k8s
- 异常检测中的浅层模型与深度学习模型综述(A Unifying Review of Deep and Shallow Anomaly Detection)
猜你喜欢
Filter & (login interception)
Opencv Learning Notes 6 -- image mosaic
skywalking 6.4 分布式链路跟踪 使用笔记
It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
MySQL审计插件介绍
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Introduction to MySQL audit plug-in
opencv学习笔记四--银行卡号识别
Photoshop插件-HDR(二)-脚本开发-PS插件
opencv学习笔记五--文件扫描+OCR文字识别
随机推荐
采集数据工具推荐,以及采集数据列表详细图解流程
MySQL审计插件介绍
《QT+PCL第六章》点云配准icp系列4
The data in the database table recursively forms a closed-loop data. How can we get these data
Hardware design guide for s32k1xx microcontroller
Guide de conception matérielle du microcontrôleur s32k1xx
Implementation of wechat web page subscription message
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
常见健身器材EN ISO 20957认证标准有哪些
phpcms后台上传图片按钮无法点击
【STM32-USB-MSC问题求助】STM32F411CEU6 (WeAct)+w25q64+USB-MSC Flash用SPI2 读出容量只有520KB
[Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
The last picture is seamlessly connected with the first picture in the swiper rotation picture
Photoshop插件-HDR(二)-脚本开发-PS插件
What is the relationship between network speed, broadband, bandwidth and traffic?
【一天学awk】条件与循环
雷神科技冲刺北交所,拟募集资金5.4亿元
Qt+pcl Chapter 6 point cloud registration ICP Series 2
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(三)
竣达技术丨多台精密空调微信云监控方案