Solved

rowcount + order by  w/o the wait ?!?!

Posted on 2002-03-05
4
834 Views
Last Modified: 2012-06-21
Hello all,

Using Sybase 12. Is there a cleaver way to "order by" without retrieving a whole result set?

Please take a look at the simplified example below:

  set rowcount @my_count
  select my_value
  from   my_table
  order  by my_value desc


Now, is there a way to get Sybase to just return @my_count number of rows, using the order by, without having to retrieve the whole result set first, then ordering, then returning the @my_count rows?

Obviously, there is no problem if the data set is small, but given a large data set, then having to order it, can be taxing.

A range (where my_value between @low and @high) is not possible as the ranges aren't known at query time.

A non-unique, clustered index (my_value) exists on the table.

A loop or cursor to get the max(my_value) where less then the last max(my_value) @my_count number of times is not an option either. (Although, if someone can prove that it is the most efficient way for any @my_count and table size, then I'm all for it! :)


Any help would be appreciated.

Thanks,
  -Dave

0
Comment
Question by:Obfuscate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 10

Accepted Solution

by:
bret earned 130 total points
ID: 6844841
You might try forcing the use of the index in the select

set rowcount @my_count
select my_value
from   my_table (index index_name)
order  by my_value desc


Do you have the sp_configure option "allow backward scans" turned on?

Alternatively, might help if the index was created in descending order.

-bret
0
 

Author Comment

by:Obfuscate
ID: 6847305
Bret,

As far as indexing options go, we have two options:
1)     create a clustered index on the my_value column(s) with or without the desc clause.
2)     create a covered non-clustered index on my_value with or without desc

It has been suggested that the non-clustered index will theoretically provide better performance in this case. Assuming that the index is created in the same order as the order by (my_value > 1 column).

Was hoping for a more SQL based optimization. If indexing is the only option, so be it.

If anyone comes up with anything, it'd be much appreciated.

Thanks,
 -Dave
0
 
LVL 6

Expert Comment

by:ChrisKing
ID: 9947718
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: bret

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

ChrisKing
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying our service is a grudge match between customer benefits and customer pain. In one corner, rolling out fixes (yay!) and delivering new features (double yay!). In the other corner, training on new features (boo – sounds like work), and chan…
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question