select * from t0_index i left join t0_index_listed_company ilc on i.id = ilc.index_id where i.ts_code = 'BK0057' and
(select sd.pct_chg from t0_share_data sd where sd.ts_code = concat(right(ilc.ts_code, 6), '.', left(ilc.ts_code, 2)) order by sd.add_time desc limit 1) > 9.9;
explain结果是
1,PRIMARY,i,,ALL,,,,,42,10,Using where
1,PRIMARY,ilc,,ALL,,,,,4048,100,Using where; Using join buffer (Block Nested Loop)
2,DEPENDENT SUBQUERY,sd,,ALL,index_tscode_addtime,,,,3946875,10,Using where; Using filesort
t0_share_data表400万条数据,有一个索引是ndex_tscode_addtime,就是tscode以及addtime建立的
刚开始以为是使用了cocat,或者right,left所导致的索引失效,遂改成:
select * from t0_index i left join t0_index_listed_company ilc on i.id = ilc.index_id where i.ts_code = 'BK0057' and
(select sd.pct_chg from t0_share_data sd where sd.ts_code = ilc.ts_code order by sd.add_time desc limit 1) > 9.9;
子查询where去除了函数
当然改完后的sql肯定是不正确的,只是为了确认是不是因为使用函数的原因,但是explain结果:
1,PRIMARY,i,,ALL,,,,,42,10,Using where
1,PRIMARY,ilc,,ALL,,,,,4048,100,Using where; Using join buffer (Block Nested Loop)
2,DEPENDENT SUBQUERY,sd,,ALL,index_tscode_addtime,,,,3946875,10,Using where; Using filesort
依旧是没有起作用。。
想请教为什么子查询没有使用索引?