Order by taking less time than max() in oracle I am using oracle db and I am running 2 queries to find the maximum id from an indexed column.

1st query - 

select max(id) 
from tableName


Explain Plan:  

Plan hash value: 3791258597



&lt;table style="width:100%"&gt;
&lt;tr&gt;
&lt;td&gt;ID&lt;/td&gt;
&lt;td&gt;Operation&lt;/td&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;Rows&lt;/td&gt;
&lt;td&gt;Bytes&lt;/td&gt;
&lt;td&gt;Cost(%CPU)&lt;/td&gt;
&lt;td&gt;Time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt; SELECT STATEMENT&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;1(0)&lt;/td&gt;
&lt;td&gt;00:00:01&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt; SORT AGGREGATE &lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;  INDEX FULL SCAN (MIN/MAX)&lt;/td&gt;
&lt;td&gt;Index Name&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;1(0)&lt;/td&gt;
&lt;td&gt;00:00:01&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;




It takes 2.839 seconds to execute on a table having nearly 80000000 rows.

2nd query - 

select id 
from (
   select id 
   from tableName 
   order by id desc) 
where rownum &lt; 2


Explain Plan:  

Plan hash value: 1514191988



&lt;table style="width:100%"&gt;
&lt;tr&gt;
&lt;td&gt;ID&lt;/td&gt;
&lt;td&gt;Operation&lt;/td&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;Rows&lt;/td&gt;
&lt;td&gt;Bytes&lt;/td&gt;
&lt;td&gt;Cost(%CPU)&lt;/td&gt;
&lt;td&gt;Time&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt; SELECT STATEMENT&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;13&lt;/td&gt;
&lt;td&gt;27876 (1)&lt;/td&gt;
&lt;td&gt;00:05:35 &lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;* 1&lt;/td&gt;
&lt;td&gt; COUNT STOPKEY &lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;VIEW&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;84M&lt;/td&gt;
&lt;td&gt;1046M&lt;/td&gt;
&lt;td&gt;27876 (1)&lt;/td&gt;
&lt;td&gt;00:05:35 &lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt; INDEX FULL SCAN&lt;/td&gt;
&lt;td&gt;INDEX Name&lt;/td&gt;
&lt;td&gt;84M&lt;/td&gt;
&lt;td&gt;482M&lt;/td&gt;
&lt;td&gt;27876 (1)&lt;/td&gt;
&lt;td&gt;00:05:35 &lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;




It takes 2.6 seconds to execute on the same table.

When I see the explain plan of the 1st query, I see that it is using SORT(AGGREGATE). Now I don't understand is that since the column is indexed, it should directly return the max value instead of using SORT(AGGREGATE) function. 

Why max() is taking more time than order by?
Both execution plans are fine

select    max(id) 
from tableName a;

---------------------------------------------------------------------------------------
| Id  | Operation                  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT           |          |     1 |     6 |     3   (0)| 00:00:01 |
|   1 |  SORT AGGREGATE            |          |     1 |     6 |            |          |
|   2 |   INDEX FULL SCAN (MIN/MAX)| TAB_IDX1 |     1 |     6 |     3   (0)| 00:00:01 |
---------------------------------------------------------------------------------------


Here the index is accessed to get the MAX value (should require only few index block access). The SORT AGGREGATE is performed, but process only one value.

The second query ...

select id 
from (
   select id 
   from tableName 
   order by id desc) 
where rownum &lt; 2;

-----------------------------------------------------------------------------------------
| Id  | Operation                    | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |          |     1 |    13 |     3   (0)| 00:00:01 |
|*  1 |  COUNT STOPKEY               |          |       |       |            |          |
|   2 |   VIEW                       |          |     1 |    13 |     3   (0)| 00:00:01 |
|   3 |    INDEX FULL SCAN DESCENDING| TAB_IDX1 |  3000K|    17M|     3   (0)| 00:00:01 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter(ROWNUM&lt;2)


... access the index descendent and limits the number of rows to one. (again few index block access only).

Both plans are IMO comparable and my question is not why is one is slower, but why both take more than few milliseconds.

Please check the size and BLEVEL of the index. 

I suppose the ID is NUMBER. My test give for both queries on an index with 96M response &lt; 100ms.