简体   繁体   English

SQL Server将多个查询作为一个查询运行

[英]SQL Server running multiple queries as one query

I've got two different SQL queries that I need to run at once. 我有两个不同的SQL查询,我需要一次运行。 But since I am using inner joins in both of them, I can't just join them. 但由于我在两者中使用内连接,我不能只加入它们。 Is there a way to do this? 有没有办法做到这一点? Also, do you think that much normalization could cause problems? 另外,你认为很多标准化会导致问题吗?

SELECT S.SchoolName,Sd.DepartmentName,E.GraduationDate 
FROM   Education E 
           INNER JOIN SchoolDepartment Sd ON Sd.DepartmentID = E.DepartmentID 
           JOIN School S ON S.SchoolID = Sd.SchoolID

SELECT c.CompanyName,dt.DepartmentName, pl.PositionLevelName 
FROM   Employee Ee 
           INNER JOIN Position P ON Ee.PositionID = P.PositionID  
           JOIN Company c ON c.CompanyID = P.CompanyID 
           JOIN Department De ON De.DepartmentID=P.DepartmentID 
           JOIN DepartmentType dt ON dt.DepartmentName = De.DepartmentTypeID  
           JOIN PositionLevel pl ON pl.PositionLevelID=P.PositionLevelID    

It seems like you're attempting to get two distinct sets of data, so it's not the worst thing in the world to run two queries. 看起来你正试图获得两组不同的数据,因此运行两个查询并不是世界上最糟糕的事情。

Per your comment: I'm sure Facebook queries several stores when they load your profile page, but they're making heavy use of data caching, so they may cache your profile object(s) to reduce database hits. 根据您的评论:我确定Facebook在加载您的个人资料页面时会查询多个商店,但是他们正在大量使用数据缓存,因此他们可能会缓存您的个人资料对象以减少数据库命中率。

You haven't indicated how they both are connected to a profile and whether each set is going to have many rows which are unrelated to each other. 您尚未指出它们如何连接到配置文件以及每个集合是否会有许多彼此无关的行。 If each just returns a single row and both are related to the profile (related in the sense of relational database - attributes "related" to a key - perhaps your profile), there there probably is a realistic way to combine them into a single result set (ie a relation, or table). 如果每个只返回一行,并且两者都与配置文件相关(在关系数据库的意义上相关 - 属性与键相关 - 也许是您的配置文件),那么可能有一种现实的方法将它们组合成一个结果设置(即关系或表)。

There are a few strategies you can use if the sets on not really related in that sense. 如果这些设置在这个意义上并不真正相关,那么您可以使用一些策略。

  1. Make a single stored procedure or batch both in the same command which returns both result sets in sequence and use ADO.NET's feature to get the next result set on your reader. 在同一个命令中创建一个存储过程或批处理,该命令按顺序返回两个结果集,并使用ADO.NET的功能在读取器上获取下一个结果集。

  2. Make two sequential calls to the database. 对数据库进行两次顺序调用。

  3. Use ASP and ADO's asynchronous features to make two simultaneous calls to the database and handle the completions - this can be useful for high-volume sites, since control is returned to the page once they are completed, freeing up threads. 使用ASP和ADO的异步功能可以同时调用数据库并处理完成 - 这对于高容量站点非常有用,因为一旦完成控制就会返回到页面,从而释放线程。

I am trying to do something similar. 我正在尝试做类似的事情。 I am actually trying to group lines by multiple criteria that are different into one data set. 我实际上是尝试按不同的标准将行分组到一个数据集中。 What I am doing is querying all of the data into a temp table then querytin that temp table into the different views then unioning it back. 我正在做的是将所有数据查询到临时表中,然后将临时表查询到不同的视图中,然后将其合并回来。 This way it shows the different views. 这样它就显示了不同的视图。 For example. 例如。 Line 1 includes cat1 cat2 and cat3. 第1行包括cat1 cat2和cat3。 Line 2 is just cat 2 and cat 3. And line 3 is Cat1 - cat8. 第2行只是cat 2和cat 3.而第3行是Cat1 - cat8。 The data is all the same just different slices of it. 数据完全相同,只是它的不同部分。 So multiple trips to the database to query it isn't worth the overhead. 因此,多次访问数据库以查询它是不值得的开销。 I can query the temp multiple times to get my slices then union them together into one view. 我可以多次查询temp以获取我的切片然后将它们合并到一个视图中。 Alternatively if you need these views and they change you can create a table with the groupings you want in a one to many. 或者,如果您需要这些视图并进行更改,则可以创建一个包含所需分组的表格。 With that three tiered approach you can join and get the sums in one step. 使用这三种方法,您可以加入并一步获得总和。

如果字段具有相同的数据类型(查询1中的列与查询2的相同数据类型匹配),则可以使用UNION组合两个查询,否则如果无法加入它们则不可能。

No, it is not possible on one connection. 不,在一个连接上是不可能的。 You need to open two connections and have a server powerfull enough to handle that (which means more than one core, which you mostl likely have). 您需要打开两个连接并拥有足够强大的服务器来处理它(这意味着您可能拥有多个核心)。

Note that I answer your question - most others dont seem to. 请注意,我回答你的问题 - 其他大多数人似乎都没有。

I've got two different SQL queries that I need to run at once 我有两个不同的SQL查询,我需要一次运行

That is not how it works in SQ LServer. 这不是它在SQ LServer中的工作原理。 If you writee thm into one batch, they still execute one after the other, not at the same time. 如果你把它写成一个批次,它们仍然一个接一个地执行,而不是同时执行。 Not sure it makes sense for them to run at the same time, but that is what you ask for. 不确定它们在同一时间运行是否合理,但这就是你要求的。

But I have software doing hundreds of SQL at the same time given a powerfull enough applkication server (24 cores), a powerfull enough Oracle RAC (48 cores on 2 machines with hyperthreading = 96 parallel threads). 但是我有软件可以同时执行数百个SQL,因为它有足够强大的applkication服务器(24个内核),足够强大的Oracle RAC(2台机器上48个内核,超线程= 96个并行线程)。

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

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