Solved

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

Posted on 2002-03-05
4
814 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do we check sybase license in ASE 1 2,691
install ASE 16 side by side with ASE 15.7 15 832
surrogate key in database world 6 83
SQL Query Syntax 12 80
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now