mysql嵌套查询如何利用索引?

SELECT foreach_id FROM zyp_作业计划 where jtPlanKey = 2000000507162      --jtPlanKey 为主键 ,int64类型,foreach_id 为varchar
SELECT OnLineVidio FROM jobbill where foreach_id =‘731221717606469’          --foreach_id 为主键,varchar类型, foreach_id为前一条查询的结果

以上两条都秒出结果,但如果用组合查询的歇菜了,怎么都无法利用jobbill 表的主键索引:

explain SELECT OnLineVidio FROM jobbill where id = (SELECT foreach_id as id FROM zyp_作业计划 where jtPlanKey = 2000000507162)

explain SELECT OnLineVidio FROM jobbill t1 join zyp_作业计划 t2 on t1.id=t2.foreach_id where t2.jtPlanKey = 2000000507162

explain SELECT OnLineVidio FROM jobbill t1 , zyp_作业计划 t2 where t1.id=t2.foreach_id and t2.jtPlanKey = 2000000507162

explain SELECT OnLineVidio FROM jobbill t1 join (select foreach_id from zyp_作业计划 where jtPlanKey = 2000000507162 ) t2 on t1.id=t2.foreach_id

 以上各种组合都不行,explain时显示无法利用jobbill 的索引。

explain SELECT * FROM zyp_作业计划 where foreach_id = 731221717606469     --测试下来这一行也无法利用索引,因为foreach_id 为varchar,类型不区域

 

上述几个嵌套查询的foreach_id都为varchar,为何不能利用索引呢?

mysql太差了

posted @ 2025-10-19 16:07  81  阅读(0)  评论(1)    收藏  举报