If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Is result sorted when index-only access?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-08-09, 23:48
rdba rdba is offline
Registered User
 
Join Date: Aug 2009
Posts: 30
Is result sorted when index-only access?

When a query is processed using index-only access, will the result set be sorted based on the index sequence or do I still need to specify an "order by"?

table:
empid
last
first
salary


unique index:
empid asc
last asc


query:
select empid, last from table where empid between 1000 and 2000


Will result be sorted by empid+last?
Reply With Quote
  #2 (permalink)  
Old 09-08-09, 23:59
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Per relational semantics, you always need to specify an ORDER BY if that is what you want otherwise there is no guarantee that the rows will always be returned in the desired order. While an ORDER BY may not be necessary in some cases, that could change in future, including if the number of rows in the table changes and DB2 chooses a different access plan.

If DB2 can avoid a sort by using index only access, then specifying ORDER BY will not cause an additional sort to happen because of the ORDER BY. DB2 is smart enough to know when a sort is not needed even when an ORDER BY is specified, so there is no reason to omit the ORDER BY.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On