sqldatabaseperformancesorting

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?


Solution

  • 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.