Mysql 使用相同的表优化从子选择中选择
您将如何优化当前需要10秒的查询?我需要选择具有特定条件的子查询中过滤的所有实体的IDMysql 使用相同的表优化从子选择中选择,mysql,sql,Mysql,Sql,您将如何优化当前需要10秒的查询?我需要选择具有特定条件的子查询中过滤的所有实体的ID select temp.ip, vas_updates.setting, vas_updates.version from (select count(id_update) as counter, ip from vas_updates where date(date)="2016-05-09" and time(date) between("14:00:00"
select temp.ip, vas_updates.setting, vas_updates.version
from (select count(id_update) as counter, ip
from vas_updates
where date(date)="2016-05-09" and
time(date) between("14:00:00") and ("23:00:00")
group by ip
having counter =2
) temp, vas_updates
where vas_updates.ip = temp.ip
order by ip;
我敢打赌这会花费很多时间,因为查询已经从temp、vas\u更新中获得,是否有办法隔离“最后一次查询”我将这样写:
select i.ip, u.setting, u.version
from (select count(id_update) as counter, ip
from vas_updates
where date between '2016-05-09 14:00:00' and '2016-05-09 23:00:00'
group by ip
having counter = 2
) i join
vas_updates u
on u.ip = i.ip
order by ip;
然后在vas\u更新(日期,ip)
和vas\u更新(ip)
上建立索引应该会有所帮助。在ip上建立索引基本上解决了这个问题,也是一个很好的方法。