Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-03-05
4
Medium Priority
?
854 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
4 Comments
 
LVL 10

Accepted Solution

by:
bret earned 520 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

916 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