Home » SQL & PL/SQL » SQL & PL/SQL » Order by some columns (11g)
Order by some columns [message #601534] Thu, 21 November 2013 23:14 Go to next message
yuvipoy
Messages: 5
Registered: November 2013
Junior Member
Hi i am new to this.
i am having a requirement like

I am having a table with 100 columns and with 1 million records.

out of 1 million records need to sort the record based on the client requirement and return back the same.

Client 1 might ask for some 100 records alone with column5 asc where rownum between 10000 and 10100 out of 1 million

Client 2 might ask for some 10k records with column9 asc, column24 desc where rownum between 20000 and 30000 out of 1 million

and so and so...
thess columns will not have any index.

how to sort the data much faster way

i have some thing like this

select a.*
from (
select col_name1.....col_name100
from mytable
order
by col_name5 desc
, col_name8
) a
where rownum <=10000
but it is time consuming.
Re: Order by some columns [message #601536 is a reply to message #601534] Thu, 21 November 2013 23:30 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>how to sort the data much faster way

you have no control over sort times.
Re: Order by some columns [message #601537 is a reply to message #601536] Thu, 21 November 2013 23:52 Go to previous messageGo to next message
yuvipoy
Messages: 5
Registered: November 2013
Junior Member
is there a best way to sort the data as soon as possible
Re: Order by some columns [message #601538 is a reply to message #601537] Thu, 21 November 2013 23:55 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
yuvipoy wrote on Thu, 21 November 2013 21:52
is there a best way to sort the data as soon as possible

no
Re: Order by some columns [message #602325 is a reply to message #601538] Tue, 03 December 2013 03:56 Go to previous messageGo to next message
yuvipoy
Messages: 5
Registered: November 2013
Junior Member
How do i order by a column with the minimum time which is having 1 million rows with some 100 columns

Here is the Execution Plan of my table.

Elapsed: 00:00:23.46

Execution Plan
----------------------------------------------------------
Plan hash value: 182415445

--------------------------------------------------------------------------------
---

| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time
|

--------------------------------------------------------------------------------
---

| 0 | SELECT STATEMENT | | 755K| 13M| | 18116 (1)| 00:03:3
8 |

| 1 | SORT ORDER BY | | 755K| 13M| 20M| 18116 (1)| 00:03:3
8 |

| 2 | TABLE ACCESS FULL| mytable | 755K| 13M| | 13671 (1)| 00:02:4
5 |

--------------------------------------------------------------------------------
---


Note
-----
- dynamic sampling used for this statement (level=2)

Statistics
----------------------------------------------------------
68 recursive calls
0 db block gets
50193 consistent gets
50341 physical reads
0 redo size
11743757 bytes sent via SQL*Net to client
588486 bytes received via SQL*Net from client
53463 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1001920 rows processed

[Updated on: Tue, 03 December 2013 04:02]

Report message to a moderator

Re: Order by some columns [message #602337 is a reply to message #602325] Tue, 03 December 2013 04:27 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Please read and follow How to use [code] tags and make your code easier to read?

If the columns in the order by are in an index then oracle can use that index to find the data and avoid the scan.
If the columns aren't in an index, or if you're getting too much data for oracle to consider using it then you are left with two options:
1) enable parallel processing - you'll need enterprise edition for this
2) buy better hardware.
Re: Order by some columns [message #602347 is a reply to message #602325] Tue, 03 December 2013 04:43 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
You might be able to improve the fetch time by increasing your array size to the maximum (5000 for SQL*Plus). And you should confirm that the session has enough PGA to run the statement optimally (see v$sql_workarea).
Re: Order by some columns [message #602354 is a reply to message #602337] Tue, 03 December 2013 06:05 Go to previous messageGo to next message
yuvipoy
Messages: 5
Registered: November 2013
Junior Member

Elapsed: 00:00:25.26

Execution Plan
----------------------------------------------------------
Plan hash value: 4154513149

--------------------------------------------------------------------------------
-----------------------------------------

| Id  | Operation               | Name     | Rows  | Bytes |TempSpc| Cost (%CPU)
| Time     |    TQ  |IN-OUT| PQ Distrib |

--------------------------------------------------------------------------------
-----------------------------------------

|   0 | SELECT STATEMENT        |          |   801K|  9397K|       |  3801   (1)
| 00:00:46 |        |      |            |

|   1 |  PX COORDINATOR         |          |       |       |       |
|          |        |      |            |

|   2 |   PX SEND QC (ORDER)    | :TQ10001 |   801K|  9397K|       |  3801   (1)
| 00:00:46 |  Q1,01 | P->S | QC (ORDER) |

|   3 |    SORT ORDER BY        |          |   801K|  9397K|    15M|  3801   (1)
| 00:00:46 |  Q1,01 | PCWP |            |

|   4 |     PX RECEIVE          |          |   801K|  9397K|       |  3794   (1)
| 00:00:46 |  Q1,01 | PCWP |            |

|   5 |      PX SEND RANGE      | :TQ10000 |   801K|  9397K|       |  3794   (1)
| 00:00:46 |  Q1,00 | P->P | RANGE      |

|   6 |       PX BLOCK ITERATOR |          |   801K|  9397K|       |  3794   (1)
| 00:00:46 |  Q1,00 | PCWC |            |

|   7 |        TABLE ACCESS FULL| TR22     |   801K|  9397K|       |  3794   (1)
| 00:00:46 |  Q1,00 | PCWP |            |

--------------------------------------------------------------------------------
-----------------------------------------



Statistics
----------------------------------------------------------
         24  recursive calls
          0  db block gets
      50320  consistent gets
      50107  physical reads
          0  redo size
   11743757  bytes sent via SQL*Net to client
     588486  bytes received via SQL*Net from client
      53463  SQL*Net roundtrips to/from client
          5  sorts (memory)
          0  sorts (disk)
     1001920  rows processed

Re: Order by some columns [message #602356 is a reply to message #602354] Tue, 03 December 2013 06:09 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
So there you go - parallel makes it faster.
Re: Order by some columns [message #602388 is a reply to message #602356] Tue, 03 December 2013 23:23 Go to previous messageGo to next message
yuvipoy
Messages: 5
Registered: November 2013
Junior Member
No where ?

Elapsed: 00:00:25.26 when parallel
and
Elapsed: 00:00:23.46 non parallel.

Re: Order by some columns [message #602405 is a reply to message #602388] Wed, 04 December 2013 02:42 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
I missed that, explain plan says it's faster.
If it's not then you probably need better hardware.
Re: Order by some columns [message #602410 is a reply to message #602405] Wed, 04 December 2013 04:01 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
I'd not be adding parallel to speed something up which it appears users can call on demand. That's a VERY bad idea. (I'm aware of statement queuing/automagic DOP but suspect they'll not be in play).

From your first post I also don't think you know what rownum is or how it works. Unless of course that is just how you've worded it and there is some deterministic record identifier they are using to limit it.
Re: Order by some columns [message #602454 is a reply to message #601534] Wed, 04 December 2013 07:13 Go to previous message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
yuvipoy wrote on Thu, 21 November 2013 23:14
Hi i am new to this.
i am having a requirement like

I am having a table with 100 columns and with 1 million records.

out of 1 million records need to sort the record based on the client requirement and return back the same.

Client 1 might ask for some 100 records alone with column5 asc where rownum between 10000 and 10100 out of 1 million

Client 2 might ask for some 10k records with column9 asc, column24 desc where rownum between 20000 and 30000 out of 1 million
<snip>


Sounds suspiciously like this : https://forums.oracle.com/thread/2609847
Previous Topic: Query Help
Next Topic: DBMS_CDC find only the columns that are changed along with the primary key in the table
Goto Forum:
  


Current Time: Fri Apr 26 12:23:24 CDT 2024