Where do I sort? Where do I sort? sql sql

Where do I sort?


Database. Using indexes and other information about the data, db's are very good at this.


You should do the sorting in the database if at all possible.

  • The database can use indexes. If there is a suitable index available then the results can be read from disk already in sorted order, resulting in a performance increase - no extra O(n log(n)) sorting step is required.
  • If you only need the first x results you also minimize data transfer (both reduced network transfer, and also reduced disk access if there is a suitable index).


Best is at the mySQL query.

a) It is easy to do

b) If you use an index the sort happens when the index is created or when new rows are inserted automatically (sometimes an index needs a reorganization but this is a db admins daily business. This applies if the table is very huge.).

e) If the index includes the columns used in the where clause the access in general is faster

d) You do not need to read the whole table each time to do the sort for yourself

e) Even if you have no index I believe the DB can do the sorting best

Hope it helps