当前位置:网站首页>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 :
边栏推荐
猜你喜欢

Skywalking 6.4 distributed link tracking usage notes

An intrusion detection model

IDEA全局搜索快捷键(ctrl+shift+F)失效修复

STM32F4-TFT-SPI时序逻辑分析仪调试记录

《QT+PCL第六章》点云配准icp系列6

Summary of point cloud reconstruction methods I (pcl-cgal)

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

Opencv learning notes 5 -- document scanning +ocr character recognition

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

Filter & (login interception)
随机推荐
Raytheon technology rushes to the Beijing stock exchange and plans to raise 540million yuan
Can I choose to open an account on Great Wall Securities? Is it safe?
Implementation of deploying redis sentry in k8s
An intrusion detection model
k8s部署redis哨兵的实现
Apk signature principle
Skywalking 6.4 distributed link tracking usage notes
Shopping mall 6.27 to be completed
Demand prioritization method based on value quantification
【ROS进阶篇】第五讲 ROS中的TF坐标变换
Wechat applet 01 bottom navigation bar settings
【LeetCode】16、最接近的三数之和
Qt+pcl Chapter 6 point cloud registration ICP Series 2
Qt+pcl Chapter 6 point cloud registration ICP series 4
Configuration of ZABBIX API and PHP
What data capabilities do data product managers need to master?
Flink 系例 之 TableAPI & SQL 与 MYSQL 分组统计
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
Tableapi & SQL and MySQL insert data of Flink
微信小程序02-轮播图实现与图片点击跳转