开发者

Sorting in SQL Server or .NET?

开发者 https://www.devze.com 2023-04-03 03:44 出处:网络
In terms of the memory and CPU usage, which one is better, sorting a resultset in SQL Server or in .NET?

In terms of the memory and CPU usage, which one is better, sorting a resultset in SQL Server or in .NET?

For example, I have a stored procedure called csp_Products that returns 1000 rows - the result should be sorted by product name, is it better to sort this in SQL Server using an ORDER BY clause or is it better to do this in .NET after the data has been retriev开发者_如何学Goed?


If you can do it in SQL Server with an ORDER BY clause, then do it. SQL Server is made for retrieving and manipulating data and will be faster.

That being said, depending on the type of data being returned and the number of rows there may not be a noticeable difference. 100 rows really isn't that much data to have to worry about performance.


I would go for SQL server since it can use indexes (if there are any it can take advantage of)


I'll add that if you want to know whether (a) or (b) is better, test (a) and (b) against each other using your data, your hardware, and your usage patterns. While I agree in this case you're not likely to uncover much difference, there are very few hard and fast rules - always some "it depends" factors that can change the answer from (a) to (b) or vice-versa. For example, if the column you want to sort by is not indexed, there are 80 billion rows, the 100 rows you want are not identified by this ordering, and your .NET machine has 10x more RAM than SQL Server, I'd likely sort on the client.


situation dependant, but ideally you want to do the sorting on the database, and limit the amount of data returned to only the specified query. Don't return more than you need, and return it in the format that gives you the least amount of work to process it.


As said by others, it's good practice to sort the data in database itself and you can group by different criteria and for each group you can sort also. It uses indexes as well which could be consider one of the performance benefit provided you have well indexed database.

Having said that, one should not return all the rows and should use Custom Paging along with necessary filter criteria, which returns no of rows that you've chosen for paging and Total No of results found. It will definitely boost performance.

0

精彩评论

暂无评论...
验证码 换一张
取 消