inner join的时候加上force,从26秒提升到3秒,why ?

本文通过对比使用与未使用FORCE INDEX指令的SQL执行效率,揭示了MySQL中索引选择的实际影响。尽管EXPLAIN显示选择了主键索引,但实际执行速度仍有显著差异,提示我们深入理解索引工作原理的重要性。
cdp.listed_cpny_fin_rpt_prd表总记录数:1467246
test.qa_cpny表总记录数:103


explain SELECT dat.*
FROM test.qa_cpny es force index(PRIMARY)
inner join cdp.listed_cpny_fin_rpt_prd dat
on (es.excel_id = dat.excel_id)
;

explainSELECT dat.*
FROM test.qa_cpny es
inner join cdp.listed_cpny_fin_rpt_prd dat
on (es.excel_id = dat.excel_id)
;
explain结果都是一样的如下:
'1', 'SIMPLE', 'listed_cpny_fin_rpt_prd', 'ALL', 'PRIMARY', NULL, NULL, NULL, '1056426', ''
'1', 'SIMPLE', 'es', 'eq_ref', 'PRIMARY', 'PRIMARY', '4', 'func', '1', 'Using where; Using index'

看explain结果,走的都是primary主键索引,可是接下来执行建表语句:
drop table if exists test_cpny.listed_cpny_fin_rpt_prd;
create table test_cpny.listed_cpny_fin_rpt_prd
SELECT dat.*
FROM test.qa_cpny es
inner join cdp.listed_cpny_fin_rpt_prd dat
on (es.excel_id = dat.excel_id)
;
60960 row(s) affected
Records: 60960Duplicates: 0Warnings: 0
25.797sec
不加force动作,是25.797sec

drop table if exists test_cpny.listed_cpny_fin_rpt_prd;
create table test_cpny.listed_cpny_fin_rpt_prd
SELECT dat.*
FROM test.qa_cpny es force index(PRIMARY)
inner join cdp.listed_cpny_fin_rpt_prd dat
on (es.excel_id = dat.excel_id)
;
60960 row(s) affected
Records: 60960Duplicates: 0Warnings: 0
2.765sec
加了force动作,是2.765sec

看来有的时候mysql的explain也不是尽善尽美的啊,大家讨论下这个现象的深层次原因吧!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值