In my application I use a simple table that contains the number of times a file was accessed and the date at which it was last accessed.
I'm trying to sort the saved entries so that the files with the highest number of accesses are always on top in the database. Also I would like to sort only the files which have been accessed in the last 7 days.
What method should I use(with execution speed in mind)? Should I maybe write a stored procedure that takes care of the sorting and just call that every time I add or modify an entry? Should I query the entries newer than 7 days in my application, sort them, and add them at the top of the database? Any other methods are most welcome.
Thanks, Catalin
If you add a clustered index on the columns that define ordering, SQL Server will use that to physically organize rows in the table.
However, you should always use an ORDER BY clause in your queries, as the database is not required to return rows in any particular order. The benefit of using a clustered index, however, is that can can reduce the overall cost of sorting rows when they are retrieved and it improves the performing range queries on the indexed columns.