Re: defined order of records for SQL query without explicit instruction (order by)?

From: Jonathan Lewis <>
Date: Fri, 14 Jan 2011 09:31:55 -0000
Message-ID: <>

"Frank Bergemann" <> wrote in message
> select a.x, b.z from table a, b
> where a.x = b,x
> order by b.x;

As others have pointed out, you HAVE to have the "order by" clause to guarantee the data will be in order. This does not mean, however, that Oracle has to sort the data if it can find another way of ordering it without sorting. In your example, for instance, it you had the correct indexes you could push the optimizer down an index-driven path:

select /*+ leading (a b) use_nl(b) index(a unique_index) index(b join_index) */

    a.x, b.z
from table a, b

        a.x is not null
and a.x = b,x
order by a.x;

The plan you got from this would probably show something like "sort order by (nosort)" as the ordering operation.

> But using 'oder by' seems to have the issue, to
> 1. fetch ALL records then
> 2. order record before
> 3. hand records over to recipient.
> And that again is a performance issue, if using parallel pipelined
> functions as consumers.
> Because it creates a a delay between SQL query and startup of worker
> threads for the parallel pipelined functions (using SQL query cursor
> as argument).

The plan that avoids the sorting may still not work for you with your 'parallel pipeline' because it can only run as a serial process to avoid sorting - perhaps you can parallelise by creating a mechanism that allows each parallel thread to change the "a.x is not null" to "a.x between M and N".


Jonathan Lewis
Received on Fri Jan 14 2011 - 03:31:55 CST

Original text of this message