Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a list of records that contain a column of arbitrary strings. I would like the application to display them sorted in some order (preferably alphabetically, since humans have to read the data).

I can choose to have the database sort the data before returning it to me, or I can choose to have the database give me the raw search results and let the application deal with it.

Currently the database is sorting it cause I don't think I can write a very efficient sort anyways, but I'm wondering whether there are any situations where I want a sorted list, but it would be better to have the application do it?

share|improve this question
    
I`d say when you have many calculations since MySql is not that good at math,many frontends>1 backend. –  Mihai Jan 16 at 21:07
    
If you're going to sort it on the app end, there's no need for you to figure out your own sorting algorithm. You can easily google a sorting algorithm in virtually any language you need. Many languages even have built in sorts. –  nhgrif Jan 17 at 1:24

1 Answer 1

up vote 3 down vote accepted

This is a way to broad question to give a detailed answer. However, there is one general rule that holds true in development:

Don't reinvent the wheel

That is, unless you have a good reason. In the case of sorting, the developers of the database engine spend a lot more time writing an efficient sorting algorithm than you will be able to. So unless you need to sort by something like a field that is encrypted and cannot be decrypted on the database server, I would always sort on the database side.

However, if you have changing sorting requirements, like a display were the user selects which column to sort by, you are better of getting the data once and then sorting it in memory. The alternative of transferring the data again and again every time the sort order changes might be just too chatty on the network. Also a fully flexible query tends to be hard to optimize by the rdbms.

But then, if you are returning only the top few rows, sorting on the database site becomes a lot faster again, as the amount of data going over the network is greatly reduced.

That really brings us to the second general rule in database development:

It depends.

It hope this was at least a little helpful. For a more detailed answer please give us more detail on your actual problem.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.