当前位置:网站首页>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 :
边栏推荐
- [Cloudera][ImpalaJDBCDriver](500164)Error initialized or created transport for authentication
- 选择在长城证券上炒股开户可以吗?安全吗?
- 《性能之巅第2版》阅读笔记(五)--file-system监测
- What data capabilities do data product managers need to master?
- [advanced ROS] lesson 5 TF coordinate transformation in ROS
- Survey of intrusion detection systems:techniques, datasets and challenges
- 购物商城6.27待完成
- [cloud trend] new wind direction in June! Cloud store hot list announced
- What are the EN ISO 20957 certification standards for common fitness equipment
- Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
猜你喜欢

微信小程序02-轮播图实现与图片点击跳转

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

Filter & (login interception)
Sort out the four commonly used sorting functions in SQL

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

How to realize clock signal frequency division?

【一天学awk】函数与自定义函数

It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!

Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析

张驰咨询:家电企业用六西格玛项目减少客户非合理退货案例
随机推荐
Can I choose to open an account on Great Wall Securities? Is it safe?
《QT+PCL第六章》点云配准icp系列4
What are the EN ISO 20957 certification standards for common fitness equipment
Solid smart contract development - easy to get started
TypeScript: let
SAP CRM organization Model(组织架构模型)自动决定的逻辑分析
Hidden rules of the workplace that must be understood before 30
Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
精益六西格玛项目辅导咨询:集中辅导和点对点辅导两种方式
Demand prioritization method based on value quantification
使用swiper制作手机端轮播图
Opencv Learning Notes 6 -- image feature [harris+sift]+ feature matching
微信网页订阅消息实现
Using swiper to make mobile phone rotation map
cmake 基本使用过程
Detailed explanation of ArrayList expansion, expansion principle [easy to understand]
Survey of intrusion detection systems:techniques, datasets and challenges
《QT+PCL第六章》点云配准icp系列3
【目标跟踪】|模板更新 时间上下文信息(UpdateNet)《Learning the Model Update for Siamese Trackers》
微信小程序01-底部导航栏设置