| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Mailing Lists -> Oracle-L -> Re: query for top 10 sql?
Interesting - I have never seen "SORT (ORDER BY STOPKEY)" - Was this introduced in Oracle 9i?
I have been thinking about this problem. My best guess (sorry, it's only a guess) is that the algorithm can become something along the lines of:
Therefore, every record must be inspected but there is no need to build a tree containing all results if the record isn't a candidate for the result set. I can see a saving in building the sorted list, and I can see a definite memory saving, but the cost of reading and comparing every record still exists.
Having said that, kudos to Oracle for realising this optimisation - Does anyone know when it was introduced?
Regards,
Mark.
Mladen Gogala
<mladen_at_wangtradin To: oracle-l_at_freelists.org
g.com> cc:
Sent by: Subject: Re: query for top 10 sql?
oracle-l-bounce_at_fr
eelists.org
12/03/2004 05:53
Please respond to
oracle-l
To prove my statement of one sort to sort them all, in the land of queries where the counting stops, I did the following thing:
SQL> set autotrace on explain
SQL> select ticker from
2 (
3 select ticker,tso from spxmember
4 order by tso)
5 where rownum<=10;
TICKER
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=5 Card=10 Bytes=70) 1 0 COUNT (STOPKEY)
2 1 VIEW (Cost=5 Card=1000 Bytes=7000)
3 2 SORT (ORDER BY STOPKEY) (Cost=5 Card=1000 Bytes=16000)
4 3 TABLE ACCESS (FULL) OF 'SPXMEMBER' (Cost=4 Card=1000
Bytes=16000)
SQL> select count(*) from spxmember;
COUNT(*)
1500
Execution Plan
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=2 Card=1) 1 0 SORT (AGGREGATE)
2 1 INDEX (FULL SCAN) OF 'SPXMEMBER_PK' (UNIQUE) (Cost=4 Car
d=1000)
In the meantime, V$SYSSTAT was counting sorted rows:
SQL> select name,value from v$sysstat where name like 'sort%rows%';
NAME VALUE ---------------------------------------------------------------- ---------- sorts (rows) 40967148
and
b) after the execution.
SQL> /
NAME VALUE ---------------------------------------------------------------- ---------- sorts (rows) 40968667
SQL> select 40968667-40967148 from dual;
40968667-40967148
1519
I was the only user of the database, nobody else was sorting anything. As
the SPXMEMBER table
has 1500 rows, I conclude that all rows were sorted, just as I predicted
that they'll have to
be. I would still like to read Tom's explanation of how to sort only 10
rows and leave the
other 1490 alone. I'm sure that the explanation is logical and, probably,
misunderstood.
On 03/11/2004 10:39:05 AM, Igor Neyman wrote:
> Obviously in this case (second out of 3) it doesn't make any difference.
> But, if asked for top 10 out of 1000:
> - Without optimization: all 1000 would have been sorted;
> - With optimization: find top 10 and stop, no need to sort the rest 990.
>
> > >
>
>
> >> Please see the official ORACLE-L FAQ: http://www.orafaq.com
> ----------------------------------------------------------------
-- Archives are at http://www.freelists.org/archives/oracle-l/ FAQ is at http://www.freelists.org/help/fom-serve/cache/1.html ----------------------------------------------------------------- <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Privileged/Confidential information may be contained in this message. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone. In such a case, you should destroy this message and kindly notify the sender by reply e-mail or by telephone on (03) 9612-6999 or (61) 3 9612-6999. Please advise immediately if you or your employer does not consent to Internet e-mail for messages of this kind. Opinions, conclusions and other information in this message that do not relate to the official business of Transurban Infrastructure Developments Limited and CityLink Melbourne Limited shall be understood as neither given nor endorsed by them. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ---------------------------------------------------------------- Please see the official ORACLE-L FAQ: http://www.orafaq.com ---------------------------------------------------------------- To unsubscribe send email to: oracle-l-request_at_freelists.org put 'unsubscribe' in the subject line. -- Archives are at http://www.freelists.org/archives/oracle-l/ FAQ is at http://www.freelists.org/help/fom-serve/cache/1.html -----------------------------------------------------------------Received on Thu Mar 11 2004 - 17:15:49 CST
![]() |
![]() |