Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting on the server or on the client?

I had a discussion with a colleague at work, it was about SQL queries and sorting. He has the opinion that you should let the server do any sorting before returning the rows to the client. I on the other hand thinks that the server is probably busy enough as it is, and it must be better for performance to let the client handle the sorting after it has fetched the rows.

Anyone which strategy is best for the overall performance of a multi-user system?

like image 973
Mikael Sundberg Avatar asked Nov 28 '08 20:11

Mikael Sundberg


People also ask

Should you sort on frontend or backend?

The backend should return array of objects that can be sorted by different properties such as created_at , order , title etc... The front end sorts the objects via these properties.

Should filtering be done client-side?

If you don't paginate your search result, but returning every row to the client, you should filter client side since you know have all data clientside. If your search result is paginated, just showing first e.g.10 search results, a filter need to be done server side to include all paginated rows not yet in client.

Which is better server-side or client-side?

Between the two options, server-side rendering is better for SEO than client-side rendering. This is because server-side rendering can speed up page load times, which not only improves the user experience, but can help your site rank better in Google search results.

What is server-side sorting?

When server-side filtering or sorting is performed, the application sends a query that is a modification of the starting query to the server. The shortcoming of this way is that server resources are taken, but the benefit is that a user will get a complete result of sorting or filtering.


1 Answers

In general, you should let the database do the sorting; if it doesn't have the resources to handle this effectively, you need to upgrade your database server.

First off, the database may already have indexes on the fields you want so it may be trivial for it to retrieve data in sorted order. Secondly, the client can't sort the results until it has all of them; if the server sorts the results, you can process them one row at a time, already sorted. Lastly, the database is probably more powerful than the client machine and can probably perform the sorting more efficiently.

like image 82
Robert Gamble Avatar answered Sep 18 '22 22:09

Robert Gamble