MySQL的联合与多个查询 [英] mysql union vs multiple queries

查看:83
本文介绍了MySQL的联合与多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道一个联合是否比运行多个查询更好.

I'm interested to know if a union is better than running multiple queries.

如果是这样,是否有时会因为其他原因而使多个查询更快或更受欢迎?

If so, are there times that multiple queries would be faster or preferred for other reasons?

推荐答案

如果您使用UNION ALL并且不对输出进行排序,那么UNION的性能应与多个单独的性能大致相同假设查询与您要一起合并的查询相同.如果对数据进行排序,显然会增加一些开销(尽管可能比对应用程序进行排序的开销要少).如果您省略ALL关键字,MySQL将完成DISTINCT-ing您的结果的额外工作.同样,这会带来额外的开销,尽管可能比您自己做的要少.

If you use UNION ALL and don't sort the output, then the performance of UNION should be more-or-less the same as the performance of multiple separate queries assuming the queries are identical to the ones you're UNIONing together. If you sort the data, obviously you're imposing some overhead (although probably less than if you sorted in it your application). If you leave out the ALL keyword, MySQL will do the extra work of DISTINCT-ing your results. Again, this imposes extra overhead although probably less than doing it yourself.

这篇关于MySQL的联合与多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆