Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 ORDER BY blahID (primary key) versus datetime

Author  Topic 

sql777
Constraint Violating Yak Guru

314 Posts

Posted - 2005-09-22 : 10:58:48
TableUsers

USERID (INT, PRIMARY KEY) USERNAME (VARCHAR 50) DATECREATED (datetime)


Say the table has 2 million rows.

Obviously doing selects with the USERID in the ORDER BY clause will be the fastest query. BUT if I create a index on 'DATECREATED'...will it be just as fast? I'm guessing internally the index will be a integer value that sqlserver will use to sort the rows instead of comparing the datetime values...so in essense it is exactly like the PK column 'userid'.

am i wrong? (i think i may be that is why i'm asking hehe)

SamC
White Water Yakist

3467 Posts

Posted - 2005-09-22 : 11:48:00
DATETIME is a fast sort. No worries.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2005-09-22 : 12:26:00
They key is having the column indexed and sorting on the column itself and not on an expression (i.e., ORDER BY Convert(varchar(20), DateColumn,101) or something like that).
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-09-22 : 12:35:29
The PK is probably clustered and will be faster.
But you will be ok.
Go to Top of Page
   

- Advertisement -