views:

120

answers:

5

Hi. I was wondering if, in terms of performance and considering a mysql select on a table with very very very many (> 1.000.000) records, is better sorting results with sql "order by" or sorting results after the query with classical programming sort algorithm ... someone has any suggestion?

Tanks

+11  A: 

mySQL, hands down. It's optimized to do this, and can make use of indexes. This would be horrible to do in PHP (and you would reach the memory_limit quickly).

Pekka
+6  A: 

In the hypothetical case that you actually get the records in the memory of your application then mysql will still beat the pants of your app because if you configured your database right it won't HAVE to sort.

I fyou want to order by in a table of 1 Mio records, you would provide in index which would be typically implemented as a B-Tree where Mysql can walk through and get the sorted results.

Peter Tillemans
+4  A: 

MySQL will win. One more reason besides the others listed is that, assuming the records are already in the DB, you don't have to copy them out of the DB to sort them. And paging or subindexing them will be easy and optimized automatically.

In short, if the DB CAN do it, the DB SHOULD do it, almost always.

Scott Stafford
+1 - Tranferring the data to do the sort will be much more expensive than doing it on the database.
James Black
A: 

You're comparing a system with methods implemented in optimized C, meant to do exactly this task, with another that you are going to implement in an interpreted scripting language.

Basically, anything written in C is going to be a lot faster than an equivalent function written in PHP, by a factor of 10 to 100.

As already noted, there's no question at all that it is far more efficient to configure your DB properly and let it do the work.

Alex JL
Language doesn't THAT matter, pal. We use PHP with that factor everyday and happy with it. You are just wrong. That language difference is a least reason, and doesn't matter
Col. Shrapnel
@Col. Shrapnel What a joke! Are you serious? C is not in the same class of languages as PHP - THAT is the difference. I'm not comparing it to Python or Ruby - you clearly don't know what you're talking about. I don't use C at all because it's not suitable for web apps, but there is ABSOLUTELY NO QUESTION C is faster than PHP for the same task - however, it takes 20,000 time as many lines of code and 5 times as long to write. There IS a reason people don't write operating systems, database engine in PHP or Perl. I mean, PHP is IMPLEMENTED IN C. They are absolutely for different things.
Alex JL
Alex JL
we are not talking of operating systems. even not of reasons why mysql written not in PHP. You can see only one point, and do not understand others. It's a pity.
Col. Shrapnel
@Col. Shrapnel. Seriously, I don't know what you are talking about. Nor do you. The issue is native database code versus a program written in an interpreted scripting language. Are you saying sorting in PHP would be equivalent to having MySQL do it? Nobody else seems to think so.
Alex JL
That's easy. Even if mysql were written in PHP, it would be preferred anyway. Language doesn't that matter for this question. There are more deep reasons.
Col. Shrapnel
@Col. Shrapnel no, if MySQL was written in PHP it would have the absolute worst performance of any DB in the world, because that's ridiculous. I don't think you understand the difference between a compiled-to-native code language like C and an interpreted scripting language. The deep reason here is what I've said - MySQL has very optimized routines to do this, in native code, and writing a program in ANY scripting language would produce very poor performance. Do you disagree? I know you love PHP, I'm not bashing it in the slightest. I use it everyday and I'm absolutely not someone like that.
Alex JL
Lol you are too straightforward :)
Col. Shrapnel
Ha ha, sorry if I'm too passionate...
Alex JL
A: 

tanks everybody