Janne Mikkonen
Janne Mikkonen

Reputation: 11

Is there any reason to sort data by php rather than sql order by

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.

Upvotes: 1

Views: 1272

Answers (1)

Ashish Kumar
Ashish Kumar

Reputation: 162

Following few comments should shed some light on this issue:

  1. 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.

  2. Sorting large data in PHP will lead to memory_limit quickly.

  3. 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:

  1. 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.

  2. 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

Sorting MySQL results in PHP...?

Upvotes: 1

Related Questions