2012-01-05 87 views
0

我有一个包含主机与报告ID组合的最后一次扫描的日期的(MySQL的)表:优化嵌套查询单查询

+--------------+---------------------+--------+ 
| host   | last_scan   | report | 
+--------------+---------------------+--------+ 
| 112.86.115.0 | 2012-01-03 01:39:30 |  4 | 
| 112.86.115.1 | 2012-01-03 01:39:30 |  4 | 
| 112.86.115.2 | 2012-01-03 02:03:40 |  4 | 
| 112.86.115.2 | 2012-01-03 04:33:47 |  5 | 
| 112.86.115.1 | 2012-01-03 04:20:23 |  5 | 
| 112.86.115.6 | 2012-01-03 04:20:23 |  5 | 
| 112.86.115.2 | 2012-01-05 04:29:46 |  8 | 
| 112.86.115.6 | 2012-01-05 04:17:35 |  8 | 
| 112.86.115.5 | 2012-01-05 04:29:48 |  8 | 
| 112.86.115.4 | 2012-01-05 04:17:37 |  8 | 
+--------------+---------------------+--------+ 

我想选择与日期的所有主机的列表最后一次扫描和相应的报告ID。我已经建立了下面的嵌套查询,但我相信它可以在一个单一的查询来实现:

SELECT rh.host, rh.report, rh.last_scan 
FROM report_hosts rh 
WHERE rh.report = (
    SELECT rh2.report 
    FROM report_hosts rh2 
    WHERE rh2.host = rh.host 
    ORDER BY rh2.last_scan DESC 
    LIMIT 1 
) 
GROUP BY rh.host 

是否有可能与一个单一的,非嵌套查询做到这一点?

回答

3

没有,但你可以在你的查询做一个JOIN

SELECT x.* 
FROM report_hosts x 
INNER JOIN (
    SELECT host,MAX(last_scan) AS last_scan FROM report_hosts GROUP BY host 
) y ON x.host=y.host AND x.last_scan=y.last_scan 

您的查询做文件排序,这是非常低效的。我的解决方案没有。在此表上创建索引是非常明智的建议

ALTER TABLE `report_hosts` ADD INDEX (`host` , `last_scan`) ; 

否则您的查询将执行两次文件。

+0

这似乎是合理的。感谢索引! – 2012-01-05 10:18:03

0

如果你只想从report_hosts表中选择一次,那么你可以使用一种'RANK OVER PARTITION'方法(在Oracle中可用,但不幸的是,在MySQL中)。像这样的东西应该工作:

select h.host,h.last_scan as most_recent_scan,h.report 
from 
(
select rh.*, 
case when @curHost != rh.host then @rank := 1 else @rank := @rank+1 end as rank, 
case when @curHost != rh.host then @curHost := rh.host end 
from report_hosts rh 
cross join (select @rank := null,@curHost = null) t 
order by host asc,last_scan desc 
) h 
where h.rank = 1; 

授予它仍然嵌套,但它确实避免了'双选'问题。不知道它是否会更有效率 - 还是取决于您拥有的索引和数据量。