Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.server -> Why does select max(..) sort?
Hello
I am wondering why Oracle sorts a table in order to do a select max (..):
explain plan for select max(n) from test_for_max;
->
SELECT STATEMENT ()
SORT (AGGREGATE)
TABLE ACCESS (FULL) TEST_FOR_MAX
It seems to me that the sort is unnecessary because while a table
scan is performed on test_for_max, Oracle could compare an internal
variable max_so_far with the current value of n and set max_so_far equal
n if n is larger.
pseudo code
max_so_far = -INFINITE;
while (row* r=get_a_row_from_table(test_for_max)) {
if (r->n > max_so_far) max_so_far=n;
}
return max_so_far;
TIA Rene
-- Latest article on my HP (as of 14.07.2002): Transforming each character in a string with perl http://www.adp-gmbh.ch/perl/each_char.htmlReceived on Mon Jul 22 2002 - 14:02:39 CDT