Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting in Array vs Sorting in SQL

Tags:

c#

sql

sql-server

I have around 1000 rows of data.On the ASPX page, whenever the user clicks the sort button, it will basically sort the result according to a specific column.

I propose to sort the result in the SQL query which is much more easier with just an Order by clause.

However, my manager insisted me to store the result in an array, then sort the data within an array because he thinks that it will affect the performance to call the database everytime the user clicks the sort button.

Just out of curiosity - Does it really matter?

Also, if we disregard the number of rows, performance wise, which of these methods is actually more efficient?

like image 941
C.J. Avatar asked Aug 19 '14 20:08

C.J.


1 Answers

Well, there are three options:

  1. Sort in the SQL
  2. Sort server-side, in your ASP code
  3. Sort client-side, in your Javascript

There's little reason to go with (2), I'd say. It's meat and drink to a database to sort as it returns data: that's what a database is designed to do.

But there's a strong case for (3) if you want to have a button that the user can click. This means it's all done client-side, so you have no need to send anything to the web server. If you have only a few rows (and 1000 is really very few these days), it'll feel much faster, because you won't have to wait for sending the request and getting a response.

Realistically, if you've got so many things that Javascript is too slow as a sorting mechanism, you've got too many things to display them all anyway.

In short, if this is a one-off thing for displaying the initial page, and you don't want the user to have to interact with the page and sort on different columns etc., then go with (1). But if the user is going to want to sort things after the page has loaded, then (3) is your friend.

like image 121
chiastic-security Avatar answered Oct 05 '22 19:10

chiastic-security