I am using MS SQL server 2005
I have a table with 3 columns where I store user-message mapping like:
msg_for msg_from msg_id
bob bob 1
bob john 1
bob steve 1
bob bob 2
bob john 2
bob bob 3
bob john 3
bob steve 3
The PK is on 3 columns and msg_id is FK to messages table that stores the messages
The above is the physical storage I see according to the PK on 3 columns
Now my query MUST return the messages for a given user having latest msg at top (order by msg_id DESC)
bob john 3
bob steve 3
bob john 2
bob steve 2
bob john 1
bob steve 1
This mapping table has millions of rows. I see 95% of the cost is to SORT the result.
Is it possible to have the PK or some other way store data physically like this (avoid SORT)?
msg_for msg_from msg_id
bob bob 3
bob john 3
bob steve 3
bob bob 2
bob john 2
bob bob 1
bob john 1
bob steve 1
Thanks