MySQL:使用 UNION 与多个查询 [英] MySQL: using UNION vs multiple queries
本文介绍了MySQL:使用 UNION 与多个查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很想知道 UNION
是否比运行多个查询更好.
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 的性能应该或多或少与假设查询是多个单独查询的性能与您UNION
结合在一起的那些相同. - 如果你使用
UNION ALL
和 sort 数据,显然你会施加一些开销(虽然可能比您在其中对您的应用程序进行排序要少). - 如果你省略了
ALL
关键字,MySQL 会做额外的工作DISTINCT
- 你的结果.同样,这会带来额外的开销虽然可能比自己做的要少. - 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'reUNION
-ing together. - If you use
UNION ALL
and 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 ofDISTINCT
-ing your results. Again, this imposes extra overhead although probably less than doing it yourself.
这篇关于MySQL:使用 UNION 与多个查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文