【发布时间】:2010-01-22 05:37:29
【问题描述】:
我之前的问题:
Date of max id: sql/oracle optimization
在我之前的问题中,我正在寻找不同的方法来查找具有最高 ID 号的记录的日期。以下是一些提供的解决方案,以及由解释计划计算得出的“成本”。
select date from table where id in (
select max(id) from table)
成本为 8
select date from table where rownum < 2 order by id desc;
成本为 5
select date from (select date from table order by id desc) where rownum < 2;
也有成本 5
with ranked_table as (select rownum as rn, date from table order by id desc)
select date from ranked_table where rn = 1;
成本为 906665
SELECT t1.date
FROM table t1
LEFT OUTER JOIN table t2
ON t1.id < t2.id
WHERE t2.id IS NULL;
成本为 1438619
显然 id 上的索引正在发挥作用。但我想知道,在什么情况下,最后两个表现至少一样好,如果不是更好?我想了解这样做的好处。
这是在 Oracle 中完成的。所有品种都可以讨论,但请说出你的答案适用于什么。
【问题讨论】:
标签: sql optimization