开发者

Sorting small list (10 items) in PHP vs. MySQL and then returning?

I'm curious what the runtime cost of sorting a small set of element开发者_JS百科s returned by a MySQL query is in PHP in contrast to sorting them in MySQL.

Reason I can't sort them in MySQL is because of the groups of data being returned, (actually 2 queries and then bringing data together PHP side).

Thanks!


There will be no difference for ten elements. But generally you should always sort in MySQL, not PHP. MySQL is optimized to make sorting fast and has the appropriate data structures and information.

PS: Depending on the nature of the data you want to combine: Look at JOINs, UNIONs and subqueries. They'll probably do it.


My opinion:

The overhead is negligible in either case. Optimize your productivity and do whichever is easier for you. The computer is there to help you, not vice versa.

If you really want to optimize the host, have you considered letting the client do it in javascript?

I'd probably do it in SQL too, but mainly to keep the logic in one place (increase cohesion, reduce coupling).


you could combine the queries with a union and then sort it with mysql

Q1
UNION
Q2
ORDER BY X
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜