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 JOIN
s, UNION
s 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
精彩评论