Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Finding the 10 smallest values in a column

Re: Finding the 10 smallest values in a column

From: Stef ten Bras <stbras_at_hiscom.nl>
Date: 1997/11/13
Message-ID: <346B28D4.2781@hiscom.nl>#1/1

Jeff Brown wrote:
>
> How about (I didn't test this, but I've used similar):
>
> Select VAL
> From TABLE
> Where ROWNUM < 11
> Order by VAL Asc
> ;
>
> If you want values only once, make that "Select Distinct VAL".
>
> Jeff Brown
> JF.Brown_at_pnl.gov

For smal tables the solution is fine but for bigger tables I think you get a performance hit.
To get a good performance this is not the way to go. On a similar subject Oracle gave the following answer.

cursor X
is select <whatever>
order by yourvalue decending
begin

	open X
	for i in 1..10
	loop
		fetch X into r
		cache or process the data
	end loop

end

In this manner the ten highest yourvalue's become available. Tuning of this query by the statistical optimizer (Oracle) on statistical information and indexes should give a good performance. An index over yourvalue is assumed. Only the ten highest yourvalue's are accessed. No sorting, no accessing of more than 10 indexes (1 or 2 blocks) and 10 rows is done.

-- 
E-mail: 
+-----------------------------------------------------------------------+
| stef ten bras, HISCOM afdeling Systeem Ontwikkeling         __   o  ~
|
| Schipholweg 97, Postbus 901, 2300 AX Leiden, Nederland      _ _/[._  
|
| Tel. +31 71 5256758,      Email stbras_at_hiscom.nl         --  (_)>(_) 
|
+-----------------------------------------------------------------------+
Received on Thu Nov 13 1997 - 00:00:00 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US