Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用相同的表优化从子选择中选择_Mysql_Sql - Fatal编程技术网

Mysql 使用相同的表优化从子选择中选择

Mysql 使用相同的表优化从子选择中选择,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"

您将如何优化当前需要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")  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上建立索引基本上解决了这个问题,也是一个很好的方法。