SQL Server:多个查询或 UNION

2021-09-14 00:00:00 union sql-server

我需要执行 16 个查询.所有这些查询都具有相同的格式:

I've 16 queries that I need to execute. All these queries have the same format :

SELECT string, number, number 

将所有这些数据组合在一起对我来说很有意义,因为它可以创建一个包含所有结果的仪表板.

Make sense for me to group all these data together as it's to create a dashboard with all the results.

我的问题是:你认为 UNION ALL 会比一个一个执行所有查询更快吗?

My question is : do you think a UNION ALL will be faster then executing all the queries one by one?

推荐答案

UNION ALL 当然应该让你更快取回数据.

UNION ALL should certainly allow you to get the data back faster.

不过,您打算如何处理客户端上的数据?如果您必须将其拆分回 16 组独立的数据集,则整体执行时间可能会增加.

What are you going to do with the data on the client, though? If you have to split it back it into 16 separate sets of data the overall execution time will probably increase.

无论您做什么,您只需要对数据库进行一次调用,因此如果您将其保留为 16 个单独的查询,那么最好在一个 存储过程(或子存储过程)中调用它们程序).

Whatever you do, you only want one call to the database so if you leave it as 16 separate queries then it would be nice to have them all called in one stored procedure (or sub-stored procedures).

相关文章