SQL查询可以有多少个字符?OracleDB和SQL Server

huangapple go评论58阅读模式
英文:

How many characters can a SQL Query have? OracleDB and SQL Server

问题

ORACLE和SQL Server对SQL查询的字符数量有限制吗?

在ORACLE和SQL Server中,SQL查询的字符数量通常是有限制的,但这个限制取决于具体的版本和配置。这些限制通常是为了确保性能和系统稳定性。

在ORACLE中,SQL查询的字符数量限制取决于SGA_MAX_SIZE、PGA_AGGREGATE_TARGET和其他配置参数。通常情况下,ORACLE允许非常大的查询,但你需要确保系统资源足够来执行这样大的查询。在一些版本中,SQL查询的字符数量限制可能在32K到64K字符之间。

在SQL Server中,查询的字符数量限制也取决于版本和配置。SQL Server允许较大的查询,但也有一些限制。在SQL Server中,查询文本通常可以达到2GB的大小。然而,这个限制可能会受到一些因素的影响,比如服务器的内存和磁盘空间。

需要注意的是,大型查询可能会对数据库性能产生影响,因此应该谨慎使用。如果你的查询接近或超过了限制,考虑优化查询或拆分查询成多个较小的部分以减轻数据库的负担。

总之,ORACLE和SQL Server通常有字符数量限制,但这些限制可以根据配置和版本的不同而有所不同。如果你的查询非常庞大,建议查阅相应数据库管理系统的文档以获取关于字符限制的具体信息。

英文:

I'm working on a feature in a Java application that builds a query based on a LOG table.

This LOG table basically has the history of created entities, storing all PK columns and their values.

This feature basically adds an intermediary filter where the user can tell me something like:

"Here are the entity X and date Y. Go to the log table, find all PKs of entity X since the date Y, then go on the Entity X's table and bring me all the data based on the PKs that you've get on the last step."

This is why I cannot paginate :(. I need to find all PKs since date Y.

I'm facing the following situation:

Imagine that my log table has 5,000 creation logs of the TB_WINDOWS table. The table TB_WINDOWS has a composite key with 3 columns ID(NUMBER), CREATED_BY(NUMBER), CREATED_AT(DATE).

So the query will have at least:

  • At least 10,000 AND clauses (AND.size() * 10,000 = 30,000 characters)

  • At least 5,000 OR clauses (OR.size() * 5,000 = 10,000 characters)

  • At least 30,000 () braces

  • At least 110,000 characters (( ID.size() + CREATED_BY.size() + CREATED_AT.size() ) * 5,000 = 110,000).

So, imagine the query looking like this:

-- In this example I'm using Oracle's TO_DATE.
-- But as I said, I need to know about Oracle and SQL Server
-- So, just take this as an example.
SELECT
	*
FROM
	TB_WINDOWS
WHERE
	(ID = 1
		AND CREATED_BY = 2
		AND CREATED_AT = TO_DATE('a date', 'a format') )
	OR (ID = 2
		AND CREATED_BY = 3
		AND CREATED_AT = TO_DATE('a date', 'a format') )
	OR (.....)
    OR (.....)
    OR (.....)
    -- And many other OR clauses

We can say that this query will have more than 180,000 characters.

My question is, does ORACLE and SQL Server have a limitation on the number of characters that a SQL Query can have?

答案1

得分: 2

你冒着严重风险将你的代码投入生产中运行。Oracle共享SQL语句及其执行计划。因此,每次SQL硬解析都需要在共享内存资源中获取独占锁。当你想要分配的内存块非常大时,这可能会成为一个问题。我曾看到数据库系统因为在共享区域尝试解析15MB的SQL语句而导致崩溃。

通常,这种方法(例如Hibernate使用的方法)是不鼓励的。
关于Oracle中的“动态WHERE子句问题”已经有很多文章写过。将数据存储在临时表中通常更安全。

英文:

You risk serious trouble to run your code in production. Oracle shares SQL statements and their exec plan. Therefore every every SQL hard-parsing requires exclusive lock in shared memory resources. This can be a problem when a chunk of memory you want to allocate is HUGE. I saw DB system crashes cased by situation when heavily loaded system tried to allocate memory in shared area, trying to parse 15MB SQL statement.

Generally this approach (used by Hibernate for example) is discouraged.
There were various articles written about "Dynamic where clause problem" in Oracle. It is much safer to store your data in temporary table - for example.

huangapple
  • 本文由 发表于 2023年2月14日 22:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定