Consider I have newly calculated one million (1,000,000) values.
I want to highest 10 values of those one million values.
I’m hesitating to choose whether to sort in PHP or using MongoDB (Indexed) to sort those.
I know that less using DB might increase overall performance.
But I do not know which one would be faster in this case, what if MongoDB is incredibly fast so that even using MongoDB just for sorting is faster than using PHP to sort.
If php is faster and better way to do, which sorting algorithm should be chosen?
Give me some suggestions.
MongoDB has a pretty nice set on indexes features in the other hand in PHP you can use different functions such as sort, (which uses an implementation of quicksort, btw) etc.
I wouldn’t only focus on speed unless your concurrency is minimal, consider if you are sorting the result set int PHP each time you want to display it and you are listening X number of requests then the memory footprint will be about
X * array size + extra overheaduntil the request/run finishes.MongoDB has the ability of allowing you to choose the index sorting when you are creating them so this can be a good idea, since the data is going to be added to a B-tree for indexing in the right order (while in the other hand it will slow down the inserts for the same reason)
So, bottom line, maybe if the set were smaller I would opt for PHP sorting, but in this case (and as usual this kind of questions ends) I would recommend you to benchmark and decide with real data.