Solved

T-SQL: Order By ColA, ColB --means--> Create Index ... (ColA, ColB), right? [SQL Server 2005]

Posted on 2010-11-16
5
444 Views
Last Modified: 2012-05-10
Using the table below if I wish to have the most efficient two-level Order By as such:
select * from Simple order by ColA, ColB
The composite index should be ColA, ColB, right?  Or is it ColB, ColA?
CREATE INDEX SimpleIndex ON Simple (ColA,ColB);
create table Simple (
  PK int,
  ColA int,
  ColB int
);

Open in new window

0
Comment
Question by:ZuZuPetals
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 250 total points
ID: 34144898
Yes you are correct. You will effectivly be running through the index from begining to end and pulling the relevant information.
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 34144904
Simply depends upon the usage and Cardinality of the columns.
If ColB is used most in WHERE clauses to uniquely identify records, then order should be ColB, ColA else you can have it as ColA, ColB
0
 
LVL 2

Author Comment

by:ZuZuPetals
ID: 34144931
@rrjegan17: So, for Order By ONLY, is the index ordering irrelevant? Let's say I never use the columns in a WHERE clause... is the index ColA,ColB the exact same as ColB, ColA?  Or, does index order matter for the Order By?
0
 
LVL 16

Assisted Solution

by:EvilPostIt
EvilPostIt earned 250 total points
ID: 34144991
No if you were to test this you would see in the query plan that an index scan is used on both clustered and nonclustered indexes.

If you were to use a where clause it would be a seek.
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 34145008
>> So, for Order By ONLY, is the index ordering irrelevant?

Yes, it would be used in ORDER BY along with columns involved in WHERE clause.
Say if you have WHERE ColB = ? referenced in your query, then Index with order ColB, ColA would be used whereas ColA, ColB would not be used at all.

>> Let's say I never use the columns in a WHERE clause... is the index ColA,ColB the exact same as ColB, ColA?  

Yes, if it is not used in both ORDER BY and WHERE conditions, then it is exactly the same.
Hope that clarifies.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the best way to calculate hours worked 5 75
Problem with SqlConnection 4 178
MS SQLK Server multi-part identifier cannot be bound 5 41
Unable to save view in SSMS 21 73
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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