这里有一个简单的例子。所以我在网上读到了rownum,我试着去看看
哪种方法调用它是最好的,因为基于SQL优化器的两种方法都显示了
没什么区别。
select count(distinct BCC || '~' || BN) BCCN
from LINK_TBL
where AN = 'abcdefg'
and BR = 1
and rownum <= 5;或
select count(distinct BCCN)
from (
select BCC||'~'|| BN BCCN
from LINK_TBL
where AN = 'abcdefg' and BR = '1'
)
where rownum <= 5;发布于 2011-11-08 18:14:19
优化器可以选择按照它认为合适的方式来解决/重组您的查询,只要它提供正确的结果。如果你检查解释计划,你可能会发现它们是一样的。
发布于 2011-11-08 22:52:24
我怀疑您遗漏的是在使用rownum时使用order by的意义。您发布的两个查询在功能上是等效的。但是,以下两个查询不是:
select count(distinct BCC || '~' || BN) BCCN
from LINK_TBL
where AN = 'abcdefg'
and BR = 1
and rownum <= 5
order by BCC || '~' || BN;
select count(distinct BCCN)
from (
select BCC||'~'|| BN BCCN
from LINK_TBL
where AN = 'abcdefg' and BR = '1'
order by BCC || '~' || BN
)
where rownum <= 5;不同之处在于,第一个查询以指定的顺序获取所有行,然后获取前5行,第二个查询any获取5行,然后仅对这5行进行排序。
https://stackoverflow.com/questions/8048799
复制相似问题