我剛纔的問題: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中完成的。所有品種都可以討論,但請說出你的答案適用於什麼。
感謝您的鏈接! –