Sort in SQL Server or .NET?

In terms of memory and processor usage, which one is better, sorting a result set in SQL Server or .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 it on SQL Server using the ORDER BY or is it better to do it in .NET after the data has been received ?

+4
source share
5 answers

If you can do this in SQL Server with an ORDER BY , then do it. SQL Server is designed to receive and process data and will be faster.

At the same time, depending on the type of data returned and the number of rows, there can be no noticeable difference. 100 rows really don't have much data to worry about performance.

+4
source

I would go to the SQL server as it can use indexes (if there are any features it can use)

+4
source

I will add that if you want to know if (a) or (b) is better, check (a) and (b) for each other using your data, your equipment and your usage patterns. Although I agree in this case, you are unlikely to find a big difference, there are very few hard and fast rules - there are always some β€œdepends” on factors that can change the answer from (a) to (b) or vice versa. For example, if the column you want to sort is not indexed, there are 80 billion rows, the 100 rows you want are not identified by this order, and your .NET computer has 10 times more RAM than SQL Server, I probably sort by customer.

+4
source

it depends on the situation, but ideally you want to sort by database and limit the amount of data returned only to the specified query. Do not return more than you need and return it in a format that gives you the least amount of work to process it.

+2
source

As others say, it’s good practice to sort the data in the database itself, and you can group by different criteria and for each group, which you can also sort. It also uses indexes, which can be considered one of the performance benefits provided that you index the database well.

Having said that, you cannot return all rows and use custom paging along with the necessary filter criteria, which does not return any rows that you selected for paging, and the total number of results found. This will definitely increase productivity.

0
source

All Articles