返回信息流```
select equipcode from table1 where logicport in (select tp_id_a from table2 union select tp_id_z from table2)
```
Mysql,版本是5.7.15。
Sql语句如上,table1中logicport这个字段是主键,但是Explain后发现主键索引失效,百度了以下对于in是否会造成索引失效也没找到确切的答案。因为里面的select会查到几千条数据,我猜想是否是因为和这点有关。
这是一条镜像帖。来源:北邮人论坛 / java / #56774同步于 2017/7/17
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
一个sql语句索引失效的问题
letingoo
2017/7/17镜像同步8 回复
订阅后,新回复会通过你的通知中心匿名送达。
8 条回复
首先,你这个表设计感觉有点问题
其次,没有任何where条件即便用到索引也不会执行特别快
再次,把语句改造成join试试看吧
select equipcode from table1 t1 inner join table2 t2 on (t1.logicport = t2.tp_id_a) or (t1.logicport = t2. tp_id_z)
【 在 lovemaker 的大作中提到: 】
: 首先,你这个表设计感觉有点问题
: 其次,没有任何where条件即便用到索引也不会执行特别快
: 再次,把语句改造成join试试看吧
: ...................
谢谢
select table1.equipcode
(select tp_id_a as tid from table2 union select tp_id_z as tid from table29) as a
left join table1
on a.tid=table1.logicport
这样会用到索引..效率会提高.
要去重的话..就加多一个distinct..
select equipcode from table1 where exists (select 1 from table2 where logicport = tp_id_a or logicport = tp_id_z)