I'm wondering why some php based projects are sorting data by php rather than using sql's order by clause. Sorry, I'm feeling thick. Please, enlighten me.
-
custom user-defined sorts? – mister martin Nov 23 '16 at 13:50
-
check this http://www.w3schools.com/php/php_arrays_sort.asp. – Raunak Gupta Nov 23 '16 at 14:17
1 Answers
Following few comments should shed some light on this issue:
Mysql is data manipulation software, designed to do ordering, filtering, and other data related task. While PHP is not data manipulation software but hypertext preprocessor. Hence performance wise MySQL has an edge over PHP.
Sorting large data in PHP will lead to memory_limit quickly.
MySQL is quite good at caching if the table isn't being updated much you will get the order list again in a very quick time, while this task may consume much time in PHP.
Scenario where you may use PHP sort:
You need a custom sorting which mysql does not support. In that case you may prefer php sort as it allows more flexibility than MySQL.
When you deal with relatively very small data - Performance will not be a issue in that case as the difference between two would be negligable.
Reference: Sorting a MySQL query with ORDER BY or with PHP sort functions

- 1
- 1

- 152
- 7
-
Is there any resource that would compare memory(behaviour)/disk usage when performing sorting either way? – Janne Mikkonen Nov 23 '16 at 14:54
-
MySQL memory requirements are essentially fixed. If the data to sort is too big, it will use disk instead of memory. For PHP, memory is allocated; if it is not enough, it will crash. – Rick James Nov 28 '16 at 05:55