sql - 单个 Oracle 语句中 OR 子句的最大数量是多少?

标签 sql oracle

假设我必须检查单个更新语句的 100,000 个 uid - 我的代码当前会将其分成 1000 个 uid block :

[...] WHERE UID IN (..., '998', '999', '1000') 
OR UID IN ('1001', '1002', ...) 
OR (..., etc., ...)

OR 子句的数量是否有最大限制?即,在上面的示例中,它将生成 100 个 OR 子句,每个子句有 1000 个 IN 子句。

最佳答案

22。

嗯,不完全是。这是 1000 项 IN 列表中将在我的系统上运行的 OR 子句的数量,但每个人的这个数字可能都不同。有 否database limit这正是涵盖了这种情况。它可能属于注释:

The limit on how long a SQL statement can be depends on many factors, including database configuration, disk space, and memory

当我尝试 23 时,我在 SQL*Plus 中收到此错误:

ERROR at line 1:
ORA-03113: end-of-file on communication channel
Process ID: 2452
Session ID: 135 Serial number: 165

这不是真正的错误,这只是意味着服务器崩溃并且 SQL*Plus 失去了连接。奇怪的是,当我查看警报日志时,没有任何错误。有跟踪文件,但仍然没有 ORA 错误消息。我看到的只是数百行这样的:

*** 2013-11-04 21:59:48.667
minact-scn master-status: grec-scn:0x0000.00821c54 gmin-scn:0x0000.0081d656 gcalc-scn:0x0000.00821c54
minact-scn master-status: grec-scn:0x0000.00823b45 gmin-scn:0x0000.0081d656 gcalc-scn:0x0000.00823b46

这里的教训是避免大得离谱的 SQL 语句。您必须以另一种方式执行此操作,例如将数据加载到表中。并且不要尝试构建只是足够小的东西。今天可能可以工作,但明天在不同的环境中就会失败。


--Find the maximum number of IN conditions with 1000 items.
--Change the first number until it throws an error.
--This code uses dynamic SQL, but I found that static SQL has the same limit.
declare
    c_number_of_ors number := 22;

    v_in_sql varchar2(4000);
    v_sql clob;
    v_count number;
begin
    --Comma-separate list of 1000 numbers.
    select listagg(level, ',') within group (order by 1)
    into v_in_sql
    from dual connect by level <= 1000;

    --Start the statement.
    v_sql := 'select count(*) from dual ';
    v_sql := v_sql || 'where 1 in ('||v_in_sql||')';

    --Append more ORs to it.
    for i in 1 .. c_number_of_ors loop
        v_sql := v_sql || ' or '||to_char(i)||' in ('||v_in_sql||')';
    end loop;

    --Execute it.
    execute immediate v_sql into v_count;
end;
/

关于sql - 单个 Oracle 语句中 OR 子句的最大数量是多少?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19777853/

相关文章:

sql - 从表中选择在第二个表中没有相关条目的行

sql - 如何避免 PostgreSQL 中的嵌套查询

sql - 在字符串 Oracle SQL 之间插入字符

sql - PL/SQL 函数中的 OUT/IN OUT 参数

string - Oracle SQL 中的高级字符串比较

sql - 在存储过程结束时显式删除本地临时表有什么好处?

sql - 如何在Sql Developer中连接到本地数据库

mysql - 用其他字段唯一值的序号更新表中的字段

oracle - dbms_job/Oracle 作业调度程序

oracle - 单元测试 Oracle PL/SQL