I've got this simple table: RecordID (int PK), UserID (int FK), DTime (datetime), Length (int)
I need to run a simple query:
SELECT TOP(1) * FROM uTable WHERE UserID = @userid ORDER BY DTime DESC
The query is run using a Stored Procedure on SQL Server 2008 R2.
The objective is to get the last record for @userid in terms of DTime.
While checking the execution plans I've noticed the sorting (ORDER BY) is almost 80% of the cost to run this query (which is the most "expensive" query on my DB for now) - how could I change the query so I won't use ORDER BY and thus eliminate that heavy cost.
Thanks!