2012-01-05 86 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; 

授予它仍然嵌套,但它確實避免了'雙選'問題。不知道它是否會更有效率 - 還是取決於您擁有的索引和數據量。