SQL Sort: Server and Client

In most cases, when it comes to sorting some data, we have two options:

  • Sort on SQL Server - Use ORDER BY Clause
  • Sort by customer, we get data from the database

When will you use one after the other and why?

+7
performance sorting sql
source share
3 answers

Always sort where possible on the SQL server.

SQL abstracts the steps of database queries, sorting, etc. Use as much abstraction as possible - why would you choose a group of rows and then write a sorting algorithm when you can do this in SQL using ORDER BY .

See also Bill Carvin's answer .

+23
source share

I would use sorting on the server when the data set is large, and I know there is an index in the database that will help in sorting. If the index is missing, I would create it.

I would use sorting on the client only if the data set is small enough to fit comfortably into the applicationโ€™s memory, and the application system has an effective sorting function and there is no index in the database. But if the data set is so small, the cost of sorting on a server even without an index is probably easy to carry.

+22
source share

In general , which you want to sort in the database is easier, less code and more efficient.

However, there are some boundary conditions where client-based sorting makes sense.

One dataset, several varieties, in-memory dataset

Let's say you have a javascript gui that looks at a table with 500 rows, if you want to give your users the flexibility to sort by any column that they want, it might make sense to do sorting in javascript so that you reduce the trip to the server.

The main advantage for sorting on the client side is the reduction in network traffic and the use of db (when using several varieties). You must be very careful with your sorting characteristics and memory limitations. Large datasets, the client side, are often virtualized, so not all data actually arrives at the client.

+6
source share

All Articles