I am using .OrderByDescending(x => x.Id) to return the most recent data in paged sets of 20 from a large dataset to a web application. I have been attempting to optimize the speed of my query and noticed that when I remove the OrderByDescending() call it improves the query time from around 8 or 9 seconds down to 2 seconds. I assume the query with OrderByDescending() is starting at the first entry (the lowest ID Number) and looping through all of the data before returning the last 20 or so results. If this is the case is there anyway to make my query start at the last entry (the highest ID Number) and work it's way to the lowest?
In regards to some of the questions I am using MS SQL Server and the LINQ query below is the one I am attempting to optimize
TransactionDao.GetAll()
.Where(x => x.Order.Location.Id == locationId && x.Status == Status.Complete)
.OrderByDescending(x => x.Id)
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize).ToList();