将子查询组合在一起
我收到一个调用 4 个子查询的查询。每个子查询都有完全相同的 FROM 和 WHERE 条件,只是选择不同(即:一个子查询对一列进行求和,另一个对另一列的记录数进行求和,还有一些子查询也在不同的列上进行求和)。
我没有太多使用聚合函数,但我认为如果我只是将它合并到一个子查询中,结果没有真正的差异。事实上,我期望更好的性能,因为它只有一个查询而不是 4 个?这是一个有效的假设吗?
I've received a query that calls 4 subqueries. Each subquery has the exact same FROM and WHERE conditions, except the selection is different (ie: one sums up one column, another counts the number of records for another column, and some other ones also do sums on different columns).
I haven't worked with aggregate functions much but I'm thinking there's no real difference in results if I just combine it into one subquery. In fact I would expect better performance since it's only one query as opposed to 4? Is this a valid assumption?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要查看执行计划。优化器很可能一下子就完成了这一切。另一方面,也可能不是。
我的倾向是重构子查询,只是为了使查询更简单/更易于阅读。
You need to look at the execution plan. The optimizer may well be doing it all in one feel swoop. On the other hand, it might not be.
My inclination would be to refactor the subqueries, just to make the query simpler/easier to read.