[英]Oracle 10g performance between rownum or with clause
I am using Oracle Database 10g Enterprise Edition 10.2.0.4.0 64bit
我正在使用
Oracle Database 10g Enterprise Edition 10.2.0.4.0 64bit
and I would like to know, what is the best way to write the following query? 我想知道,编写以下查询的最佳方法是什么?
1. With rownum 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 a WITH
clause 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. With rank clause 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;
I think 2 and 3 are similar, but which is the fastest, 1, 2 or 3? 我认为2和3相似,但是最快的是1、2或3?
I don't think you can particularly generalise as to which query is the "best" in all situations. 我认为您无法在所有情况下特别概括哪个查询是“最佳”查询。 As with most IT questions, the answer is: "it depends"!
与大多数IT问题一样,答案是:“取决于”! You would need to investigate each query on its own merits.
您将需要根据其优点调查每个查询。
As an aside, you've missed off another alternative - assuming that you're only after a single column, rather than the whole row for the highest column you're interested in: 顺便说一句,您错过了另一种选择-假设您只关注单列,而不是感兴趣的最高列的整行:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.