代码区项目交易流程

Keep the SQL Server table sorted in the database


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.

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

点击收藏

LAST More PowerShell v6 thoughts Hackers Exploit Known Google Chromecast Vulnerability in Thousands of Devices NEXT