rownum 或 with 子句之间的 Oracle 10g 性能

Oracle 10g performance between rownum or with clause

我正在使用 Oracle Database 10g Enterprise Edition 10.2.0.4.0 64bit

我想知道编写以下查询的最佳方式是什么?

1.用 rownum

SELECT * FROM
  (
  SELECT ID_DONNEE_H, DATE_DONNEE
  FROM DONNEE_H d
  WHERE d.DATE_DONNEE > sysdate -50000
  AND d.ID_SC = 38648
  ORDER BY DATE_DONNEE DESC
  )
  WHERE rownum=1;

2。带有 WITH 子句

with req as (
  select d.ID_DONNEE_H, row_number() over (order by DATE_DONNEE desc) as seqnum
  from DONNEE_H d
  where d.DATE_DONNEE > sysdate -50000
  AND d.ID_SC = 38648 ) 
select * from req where seqnum = 1;

3。带等级子句

select * from (select d.ID_DONNEE_H, row_number() over (order by DATE_DONNEE desc) as seqnum
  from DONNEE_H d
  where d.DATE_DONNEE > sysdate -50000
  AND d.ID_SC = 38648) test
  where seqnum = 1;

我认为 2 和 3 相似,但哪个最快,1、2 还是 3?

我认为您不能特别概括哪个查询在所有情况下都是 "best"。与大多数 IT 问题一样,答案是:"it depends"!您需要根据自身的优点调查每个查询。

顺便说一句,您错过了另一种选择 - 假设您只是在单列之后,而不是您感兴趣的最高列的整行:

with sample_data as (select 10 col1, 3 col2 from dual union all
                     select 20 col1, 3 col2 from dual union all
                     select 30 col1, 1 col2 from dual union all
                     select 40 col1, 2 col2 from dual)
select max(col1) keep (dense_rank first order by col2 desc) col1_val_of_max_col2
from   sample_data;

COL1_VAL_OF_MAX_COL2
--------------------
                  20