简体   繁体   English

SQL 服务器:UNION 的数量有什么限制?

[英]SQL Server: What's the limit on number of UNIONs?

I was wondering if there's a limit on a number of UNION'ed SELECT statements I can send to an SQL Server?我想知道我可以发送到 SQL 服务器的 UNION 的 SELECT 语句的数量是否有限制? I may have up to 36 done in one SQL statement, so it becomes quite long, character wise.我可能在一个 SQL 语句中完成了多达 36 个,所以它变得相当长,字符明智。 (Also each of those SELECT statements is a somewhat complex by itself with CASE WHEN statements in it.) (此外,每个 SELECT 语句本身都有些复杂,其中包含 CASE WHEN 语句。)

From SQL 2008 BOLSQL 2008 BOL

"Any number of UNION operators can appear in a Transact-SQL statement" “在 Transact-SQL 语句中可以出现任意数量的 UNION 运算符”

Also from MSDN "Maximum Capacity Specifications for SQL Server" (2008):同样来自MSDN “SQL 服务器的最大容量规格”(2008 年):

Batch size (1) 65,536 * Network Packet Size批量大小 (1) 65,536 * 网络数据包大小

Length of a string containing SQL statements (batch size) (1) 65,536 * Network packet size包含 SQL 语句的字符串的长度(批量大小) (1) 65,536 * 网络数据包大小

(1) Network Packet Size is the size of the tabular data stream (TDS) packets used to communicate between applications and the relational Database Engine. (1) Network Packet Size 是表格数据 stream (TDS) 数据包的大小,用于在应用程序和关系数据库引擎之间进行通信。 The default packet size is 4 KB, and is controlled by the network packet size configuration option.默认数据包大小为 4 KB,由网络数据包大小配置选项控制。

To my mind, that means 268,435,456 bytes when using defaults.在我看来,这意味着使用默认值时需要 268,435,456 个字节。 Please say your query is shorter than that.请说您的查询比这更短。

Necromancing, because I now know the real-life answer.死灵法,因为我现在知道现实生活中的答案。

Technically, like Neil Moss said, > 268 million bytes (ASCII characters), or > 134 million UTF-16 characters.从技术上讲,就像 Neil Moss 所说,> 2.68 亿字节(ASCII 字符),或 > 1.34 亿 UTF-16 字符。

However, there is a limit of " 256 tables per SELECT statement ".但是,有“每个 SELECT 语句 256 个表”的限制。

And while you can use more than 256 tables in a query WITH UNION STATEMENTS , such a query cannot be used as a view, table-valued function or as a subquery for a SELECT statement.虽然您可以在WITH UNION STATEMENTS查询中使用超过 256 个表,但这样的查询不能用作视图、表值 function 或 SELECT 语句的子查询。

Also, if you use such a query directly, you may hit the stack space limit of the query optimizer at around 1'300-1'500 SELECTs.此外,如果您直接使用这样的查询,您可能会在大约 1'300-1'500 SELECTs 处达到查询优化器的堆栈空间限制。

I know, because thanks to a great (warning - sarcasm) OCP (original code "programmer"), we have such af*ed up schema, and I actually ran into both, the 256 and the 1'300-1'500 limit.我知道,因为感谢一个伟大的(警告 - 讽刺)OCP(原始代码“程序员”),我们有这样一个 f*ed 模式,我实际上遇到了 256 和 1'300-1'500 限制.

(one table per object type, in case anybody wonders; with exactly the same column-schema (but not names) per table - instead of a reference table with object-type...) (每个 object 类型一个表,以防有人想知道;每个表具有完全相同的列模式(但不是名称)-而不是具有对象类型的引用表......)

According to this post somwhere between 8000 and 16000, for his particular query.根据这篇帖子,介于 8000 到 16000 之间,用于他的特定查询。 Is probably mostly goverened by the resources available to your SQL Server.可能主要受 SQL 服务器可用资源的支配。

It might be worth asking another question about the particular case you have for an alternative method that doesn't need so many unions.对于不需要这么多联合的替代方法,您可能需要针对特定情况提出另一个问题。

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

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