当前位置:网站首页>MySQL composite index with or without ID
MySQL composite index with or without ID
2022-07-02 07:17:00 【Software development heart】
One 、 Preface
A few days ago, there was a slow sql problem , The scene is import and Export ,, This watch has about 2 More than 100 million data , Need to add index optimization , Tables and fields are desensitized ., primary sql Something like that :
select * from object_range force index(idx_obj_no_id) where type = 1 and biz_type = 1 and obj_no = 123 order by id limit 1000,500
You can see that there are three query fields type,biz_typ,objec_no, The original query force index go obj_no and id Joint index of , But because of a single object_no The number of lines will still go to hundreds of thousands , So it's still slow to query .
Two 、 First processing .
According to my previous understanding , The composite index is automatically added id Of , Therefore, changes were made directly in production, and these three query fields were linked together to build a joint index idx_type_biz_type_obj_no. And then modify sql Removed force_index I went for a run explain
-- The modified sql no need force_index Time 0.015s
select * from object_range where type = 1 and biz_type = 1 and obj_no = 123 order by id limit 1000,500
-- Go to the new index idx_type_biz_type_obj_no
explain select * from object_range where type = 1 and biz_type = 1 and obj_no = 123 order by id limit 1000,500
Very satisfied with , Then publish directly .
Unfortunately, production is still the same slow sql, hold sql Pull it down and find it under specific parameters explain, This article sql Can go to the primary key index , Why did you leave primary Index? .
3、 ... and 、 solve
First , One sql stay mysql After the analyzer , Optimizer , actuator , Goodbye to the storage engine etc.
After consulting colleagues, I found , Composite indexes do add id, I really remember this correctly , But it turns out that this is in the storage layer . about mysql The optimizer does not know that this index is added id.
So in the sql When analyzing and optimizing , Especially when it comes to sorting , Probably mysql The optimizer thinks there will be filesort So use the primary key id The index will be better , Because the optimizer does not know that there is id, Therefore, the analysis will not consider ...
Then on sql Once again, the forced index publishing , slow sql period .
explain select * from object_range index(idx_type_biz_type_obj_no) where type = 1 and biz_type = 1 and obj_no = 123 order by id limit 1000,500
Back to the beginning , I understand why I started this sql To add idx_obj_no_id Indexes . So the next time you add a joint index, you'd better be honest and finally id add , There is no loss , There will be unexpected gains .
边栏推荐
- Basic knowledge of software testing
- 2021-07-05c /cad secondary development create arc (4)
- Oracle apex 21.2 installation and one click deployment
- ORACLE EBS接口开发-json格式数据快捷生成
- Principle analysis of spark
- php中计算两个日期之前相差多少天、月、年
- php中生成随机的6位邀请码
- Laravel8中的find_in_set、upsert的使用方法
- UEditor . Net version arbitrary file upload vulnerability recurrence
- view的绘制机制(三)
猜你喜欢
随机推荐
在php的开发环境中如何调取WebService?
SSM学生成绩信息管理系统
【信息检索导论】第六章 词项权重及向量空间模型
RMAN增量恢复示例(1)-不带未备份的归档日志
SQLI-LABS通关(less1)
Oracle EBS interface development - quick generation of JSON format data
Illustration of etcd access in kubernetes
Check log4j problems using stain analysis
view的绘制机制(三)
Sqli-labs customs clearance (less1)
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
Use of interrupt()
第一个快应用(quickapp)demo
view的绘制机制(二)
Oracle general ledger balance table GL for foreign currency bookkeeping_ Balance change (Part 1)
Oracle rman半自动恢复脚本-restore阶段
Oracle EBS ADI development steps
Only the background of famous universities and factories can programmers have a way out? Netizen: two, big factory background is OK
php中删除指定文件夹下的内容
Changes in foreign currency bookkeeping and revaluation general ledger balance table (Part 2)