Advertisement

03.05.2008 at 03:36AM PST, ID: 23215566
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.9

How to Select and make use of the indexes

Asked by kinton in SQL Server 2005

Hi all,

I'm trying to find if its possible to utilise the indexes so my query doesnt spend 2 hours sorting itself before rows are returned.

If I run this, rows are returned instantly;
SELECT  subs.ID, subs.customer, subs.contract,  subs.ref, subs.sequence FROM subs

if I add this it orders it first before returning the rows;
order by   subs.ref, subs.sequence

However, if I add this rows are returned instantly because there is an index for that order;
order by subs.customer, subs.contract,  subs.ref, subs.sequence

This is where i get a bit stuck.  I can't seem to link the table to itself and still have it return rows instantly.  It orders the linked table without using the indexes so goes back to taking 2 hours before returning any rows.
SELECT     subs.subscription_id, renewals.subscription_ID FROM subscription.subscription subs
LEFT OUTER JOIN subscription.subscription AS renewals ON subs.subscription_ref = renewals.subscription_ref AND subs.subscription_sequence + 1 = renewals.subscription_sequence
order by subs.subscription_customer, subs.subscription_magazine,  subs.subscription_ref, subs.subscription_sequence

To make matters worse I actually need to link it to itself twice.
I need to bring in fields from the rows in the table for the 1st in the sequence and the next in the sequence.  Example;
I'll use just the ID for what I need to include, but there are actually 4 fields so I can't use a sub query.
ID,      customer,      ref, sequence, acquisitionID, renewalID
25,     myCompany,  pro1,  1,               25,               340
340,   myCompany,  pro1,  2,               25,               700
700,   myCompany,  pro1,  3,               25,               1020
1020, mycompany,  pro1,  4,               25,               NULL

My current SQL for this is as follows;
SELECT      subs.subscription_id FROM         subs LEFT OUTER JOIN
   subs AS renewals ON subs.subscription_ref = renewals.subscription_ref AND
   subs.subscription_sequence = renewals.subscription_sequence LEFT OUTER JOIN
   subs AS acquisitions ON subs.subscription_ref = acquisitions.subscription_ref AND
   subs.subscription_acquisition_sequence = acquisitions.subscription_sequence AND acquisitions.subscription_term_type = 'ACQUISITION' ORDER BY subs.subscription_customer, subs.subscription_magazine, subs.subscription_ref, subs.subscription_sequence

Any help would be much appreciated.

Regards,
KintonStart Free Trial
[+][-]03.05.2008 at 04:07AM PST, ID: 21049585

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Server 2005
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.05.2008 at 05:38AM PST, ID: 21050124

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628