DISTINCT May Remove Sorting From Pre-Sorted Data
Oracle 2010. 9. 10. 11:39Distinct는 원래 Sort가 발생되도록 되어 있지만, Query 결과가 Sorted되지 않은 상태로 나오는 경우가 있습니다.
그 원인과 해결 방법에 대해서 아래와 같이 정리합니다.
이 문제는 Oracle DB Version : 10.2.0.1 ro 11.1.0.7 까지 모든 Platform에서 발생합니다.
Symptoms
Including a DISTINCT clause in a query may re-sort data into an un-sorted order.
select distinct username
from ( select username
from all_users
order by username )
USERNAME
------------------------------
MDSYS
TSMSYS
SYSMAN
ORDPLUGINS
ORDSYS
SYS
Cause
Distinct does not guarantee that data will be returned in sorted order since methods that do not feature sort algorithms (such as hashing) can be used to evaluate this operation.All that distinct guarantees is that no duplicates will be included. If the method used to remove duplicates happens to be a sort operation then the data may be sorted, but if not the data may not be sorted. Indeed, if the data was sorted by a previous step, it may be un-sorted by a distinct operation.
Since there is no order by clause on the query block featuring the distinct, there is no requirement that the data must be returned in a sorted order.
Solution
If a sorted order is required, an order by must be included in the relevant query block.select distinct username
from ( select username
from all_users
order by username )
order by username;
개발할 때, 간과할 수 있는 소지가 다분한 Issue이므로 꼭 기억해야만 원하는 Data를 얻게 됩니다.
Reference : Oracle Support
'Oracle' 카테고리의 다른 글
인증되지 않은 TOOLS 의 ACCESS 제한하기 (1) | 2010.09.16 |
---|---|
Oracle PL/SQL의 User Function에서 Boolean Return (0) | 2010.09.10 |
Group By와 Distinct의 차이가 뭘까? (0) | 2010.09.10 |
Silent Installation을 통한 Oracle Embedding (0) | 2010.08.16 |
Oracle Session별 Trace 생성 방법 (0) | 2010.08.13 |