我刚才的问题:sql优化如何在内部工作?
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中完成的。所有品种都可以讨论,但请说出你的答案适用于什么。
感谢您的链接! –