当前位置:网站首页>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 :
边栏推荐
- Detailed explanation of ArrayList expansion, expansion principle [easy to understand]
- Photoshop插件-HDR(二)-脚本开发-PS插件
- 雷神科技冲刺北交所,拟募集资金5.4亿元
- 《QT+PCL第六章》点云配准icp系列6
- Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching
- Create employee data in SAP s/4hana by importing CSV
- Build MySQL master-slave server under Ubuntu 14.04
- 【STM32学习】 基于STM32 USB存储设备的w25qxx自动判断容量检测
- 厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
- idea中新建的XML文件变成普通文件的解决方法.
猜你喜欢

S32K1xx 微控制器的硬件設計指南

Survey of intrusion detection systems:techniques, datasets and challenges
![[advanced ROS] lesson 5 TF coordinate transformation in ROS](/img/4d/ae7d477bf6928005e16f046d461dcb.png)
[advanced ROS] lesson 5 TF coordinate transformation in ROS

opencv学习笔记四--银行卡号识别

The data in the database table recursively forms a closed-loop data. How can we get these data

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

【ROS进阶篇】第五讲 ROS中的TF坐标变换

雷神科技冲刺北交所,拟募集资金5.4亿元

《性能之巅第2版》阅读笔记(五)--file-system监测

Qt+pcl Chapter 6 point cloud registration ICP Series 2
随机推荐
Introduction to MySQL audit plug-in
张驰课堂:六西格玛数据的几种类型与区别
《QT+PCL第九章》点云重建系列2
idea中新建的XML文件变成普通文件的解决方法.
TS报错 Don‘t use `object` as a type. The `object` type is currently hard to use
JS中箭头函数和普通函数的区别
opencv学习笔记六--图像拼接
SAP S/4HANA: 一条代码线,许多种选择
skywalking 6.4 分布式链路跟踪 使用笔记
TypeScript:var
How to realize clock signal frequency division?
Survey of intrusion detection systems:techniques, datasets and challenges
微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
opencv学习笔记六--图像特征[harris+SIFT]+特征匹配
Demand prioritization method based on value quantification
An intrusion detection model
Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
S32K1xx 微控制器的硬件設計指南
opencv学习笔记四--银行卡号识别
项目中字符串判空总结