Solved

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

Posted on 2002-03-05
4
801 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

8 Experts available now in Live!

Get 1:1 Help Now