Reputation: 667
Wondering, fetching all the data from Mysql (without order by and sort in PHP arrays) would be faster or Mysql order by?
For example, there is a table called "emp_info"
id
--------- name
--------- city
1 ------------ Eric --------- New york
2 ---------- Patrick -------- San Francisco
3 ---------- Joey ---------- London
So lets assume there are 100-200 records there in database.
If I want to city wise alphabetically show records then what would be faster??:
SELECT * FROM emp_info ORDER BY city;
OR
SELECT * FROM emp_info;
And then sort PHP array city column wise.
Even if there is order by on the city column, then also which approach would be faster?
Thanks.
Upvotes: 1
Views: 59
Reputation: 1281
MySQL will win. 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 Database CAN do it, the Database SHOULD do it, almost always.
Upvotes: 4