简体   繁体   English

在PHP vs. MySQL中排序小清单(10个项目),然后返回?

[英]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 elements returned by a MySQL query is in PHP in contrast to sorting them in MySQL. 我很好奇在MySQL中对MySQL查询返回的一小部分元素进行排序所花费的运行时间成本与PHP相比如何。

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). 我之所以无法在MySQL中对它们进行排序,是因为返回了数据组(实际上是2个查询,然后将数据合并到PHP端)。

Thanks! 谢谢!

There will be no difference for ten elements. 十个元素没有区别。 But generally you should always sort in MySQL, not PHP. 但是通常,您应该始终使用MySQL而不是PHP进行排序。 MySQL is optimized to make sorting fast and has the appropriate data structures and information. MySQL经过优化可以快速排序,并具有适当的数据结构和信息。

PS: Depending on the nature of the data you want to combine: Look at JOIN s, UNION s and subqueries . PS:根据要合并的数据的性质:查看JOINUNION子查询 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? 如果您确实要优化主机,是否考虑过让客户端使用javascript?

I'd probably do it in SQL too, but mainly to keep the logic in one place (increase cohesion, reduce coupling). 我可能也可以在SQL中执行此操作,但主要是为了将逻辑保持在一个位置(增加内聚力,减少耦合)。

you could combine the queries with a union and then sort it with mysql 您可以将查询与联合结合,然后使用mysql对其进行排序

Q1
UNION
Q2
ORDER BY X

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM