IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

performance of Order by Clause


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Baby Priya

New User


Joined: 06 Aug 2008
Posts: 3
Location: Bangalore

PostPosted: Thu May 21, 2009 12:14 pm
Reply with quote

Hi,

I have an SQL query with 'Order By' clause on the indexed columns.I want to understand how does this affect the performance of the query.

Thanks,
Priya
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Thu May 21, 2009 2:08 pm
Reply with quote

You actually do not require this order by as it is on the indexed cols and if the order is same as that of the index definition ...
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu May 21, 2009 2:28 pm
Reply with quote

the order by is an application concern ( data being requested in a certain sequence )
the index is a mix of application requirement ( unique indexes, for example )
and performance concern

so the order by should always be specified
the developer should not worry ( as a general point ) about indexes

seen a few times developers and testers chasing strange results because of indexes added to tables for performance reasons

so ( always as a general point ) I would consider not specifying an order by as an error
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Thu May 21, 2009 7:51 pm
Reply with quote

Hello,

Said slightly differently - if the sequence of the returned rows matters to the application ORDER BY must be specified. Without an ORDER BY, the results are not predictable.

A process that has run as intended for years may suddenly present results "out of sequence" causing lots of grief. . .
Back to top
View user's profile Send private message
Raghu navaikulam

Active User


Joined: 27 Sep 2008
Posts: 193
Location: chennai

PostPosted: Thu May 21, 2009 11:11 pm
Reply with quote

Hi Baby Priya

If the application requires an ORDER BY, you should provide it no matter whether it is degrading the performance or not. But definitly ORDER BY will reduce the performance even though the column used is an indexed column in the ORDER BY clause. Indexed column help to read the rows from the table quickly. DB2 will not give any guarantee that the rows are placed in the table based on the value of the indexed column.
Since sort (ORDER BY) is the last process of an SQL statement, the indexed column will not have any influence in the performance of the program. It will consider the column as an ordinary column in ORDER BY clause.

Hope this will help you.

Regards
Raghu
Back to top
View user's profile Send private message
ksk

Active User


Joined: 08 Jun 2006
Posts: 355
Location: New York

PostPosted: Fri May 22, 2009 10:36 am
Reply with quote

Priya,

We have also faced similar type of situation where due to ORDER BY performance of job was affected severly.

For our requirement, we have removed ORDER BY from the SQL query and added one more step to sort the file on reuquired field using SORT utility.

If you have SORT in your shop, I can sugest you to use sort instead of ORDER BY for performance purpose.
Back to top
View user's profile Send private message
ashimer

Active Member


Joined: 13 Feb 2004
Posts: 551
Location: Bangalore

PostPosted: Fri May 22, 2009 2:04 pm
Reply with quote

www.ibm.com/developerworks/db2/library/techarticle/0303kuznetsov/0303kuznetsov.html
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Rotate partition-logical & physic... DB2 0
No new posts exploiting Z16 performance PL/I & Assembler 2
No new posts To search DB2 table based on Conditio... DB2 1
No new posts JOIN STATEMENT PERFORMANCE. DFSORT/ICETOOL 12
No new posts DB2 Load - Sort Or order BY DB2 1
Search our Forums:

Back to Top