指尖上的码农 2021-01-31 12:15 采纳率: 100%
浏览 38
已结题

mysql 相关子查询 索引失效

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
 

依旧是没有起作用。。

想请教为什么子查询没有使用索引?

  • 写回答

2条回答 默认 最新

  • 指尖上的码农 2021-06-08 10:08
    关注

    移除子查询中的 order by 然后将索引建立成按照add_time 排序 可以使用索引,并且不会排序,原因未知

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月18日
  • 已采纳回答 11月10日

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line