Oracle sqplus:在块中检索结果(更新循环中的ROWNUM)

Oracle sqplus:在块中检索结果(更新循环中的ROWNUM),oracle,select,subquery,sqlplus,rownum,Oracle,Select,Subquery,Sqlplus,Rownum,我对使用sqlplus真的很陌生,所以如果这是一个愚蠢的问题,我很抱歉 我有一个很长时间运行的表单查询: SELECT columnA from tableA where fieldA in ( (select unique columnB from tableB where fieldB in (select columnC from tableC where fieldC not in (select columnD fro

我对使用sqlplus真的很陌生,所以如果这是一个愚蠢的问题,我很抱歉

我有一个很长时间运行的表单查询:

SELECT columnA 
from tableA 
where fieldA in (
  (select unique columnB
  from tableB
  where fieldB in 
    (select columnC
    from tableC
    where fieldC not in
      (select columnD 
      from tableD 
      where x=y 
      and a=b 
      and columnX in 
        (select columnE 
        from tableE
        where p=q)))
  and columnInTableB = <some value> 
  and anotherColumnInTableB = <some other value> 
  and thirdColumnInTableB IN (<set of values>) 
  and fourthColumnInTableB like <some string>); 
第四列和第四列呈气泡状;在最坏的情况下,匹配表B中大约300万个条目

有人建议我“以较小的块运行查询”

我想到的一种方法是为长时间运行的子查询检索数据

(select unique columnB
  from tableB  ...

按建议使用ROWNUM以块形式显示。 我的问题是:

我不知道这个子查询有多少潜在的匹配项。我可以动态设置ROWNUM来检索块中的数据吗

如果是,您能给我一个while循环必须是什么样子的示例,以及如何确定结果集何时耗尽? 我找到的一个选项是在@@ROWCOUNT>0时检查或使用:

但是,我仍然不知道如何编写循环以及如何使用变量动态设置ROWNUM

基本上,我想知道我是否能做到:

SELECT columnA 
from tableA 
where fieldA in (
  while all results have not been fetched:
    select * 
    from
      (select a.*, rownum rnum
      from
        (select unique columnB
        from tableB
        where fieldB in 
          (select columnC
          from tableC
          where fieldC not in
            (select columnD 
            from tableD 
            where x=y 
            and a=b 
            and columnX in 
              (select columnE 
              from tableE
              where p=q)))
        and columnInTableB = <some value> 
        and anotherColumnInTableB = <some other value> 
        and thirdColumnInTableB IN (<set of values>) 
        and fourthColumnInTableB like <some string>) a
    where rownumm <= i) and rnum >= i);
    update value of i here (?) so that the loop can repeat
如何在某个循环中动态更新上面rownum/rnum的'I'值,以检索块(例如10000块)中的结果,直到结果集用尽为止

另外,while循环应该是什么样子

我也不知道如何使用连接重写这个,我对sql的了解非常有限,所以如果有人能帮助我使用连接或任何其他方法更有效地重写这个,那也行

我真的非常感谢你在这方面的帮助。我已经在这个问题上纠缠了几天了,我无法确定一个合适的解决方案


谢谢大家!

1尝试删除UNIQUE/DISTINCT子句。它应该位于内存排序和临时段中

2尝试应用“rownum
SELECT columnA
FROM tableA
WHERE fieldA IN (
  (SELECT **UNIQUE** columnB
  FROM tableB
  WHERE fieldB IN
    (SELECT columnC
    FROM tableC
    WHERE fieldC NOT IN
      (SELECT columnD
      FROM tableD
      WHERE x      =y
      AND a        =b
      AND columnX IN
        (SELECT columnE FROM tableE WHERE p=q
        )
      )
    )
  AND columnInTableB            = <SOME value>
  AND anotherColumnInTableB     = <SOME other value>
  AND thirdColumnInTableB      IN (<
    SET OF VALUES               >)
  AND fourthColumnInTableB LIKE <SOME string>
  )
  **ROWNUM < 50** ; 

如果你的问题是关于oracle的,为什么要标记mysql和sql server?如果没有表、数据量、查询及其执行计划的详细信息,我认为我们帮不了什么忙——这太笼统了。使用连接而不是子查询,并检查您的统计数据是否最新,这是唯一的通用建议;我们不知道为什么你那样做会有问题。可能您使用了旧语法,但缺少联接条件。您似乎还试图遵循另一个RDBMS的建议—将您的研究重点放在专门适用于Oracle的内容上。如果子查询是长期运行的,那么您可以尝试使用EXISTS而不是IN。“如果选择谓词在子查询中,则在中使用。如果选择谓词在父查询中,则在中使用。”我对其进行了编辑,以添加有关表和常规查询结构的更多详细信息。希望这对你有帮助。你能发送计划吗?设置线宽180设置自动跟踪跟踪<此处的SQL>
SELECT columnA 
from tableA 
where fieldA in (
  while all results have not been fetched:
    select * 
    from
      (select a.*, rownum rnum
      from
        (select unique columnB
        from tableB
        where fieldB in 
          (select columnC
          from tableC
          where fieldC not in
            (select columnD 
            from tableD 
            where x=y 
            and a=b 
            and columnX in 
              (select columnE 
              from tableE
              where p=q)))
        and columnInTableB = <some value> 
        and anotherColumnInTableB = <some other value> 
        and thirdColumnInTableB IN (<set of values>) 
        and fourthColumnInTableB like <some string>) a
    where rownumm <= i) and rnum >= i);
    update value of i here (?) so that the loop can repeat
SELECT columnA
FROM tableA
WHERE fieldA IN (
  (SELECT **UNIQUE** columnB
  FROM tableB
  WHERE fieldB IN
    (SELECT columnC
    FROM tableC
    WHERE fieldC NOT IN
      (SELECT columnD
      FROM tableD
      WHERE x      =y
      AND a        =b
      AND columnX IN
        (SELECT columnE FROM tableE WHERE p=q
        )
      )
    )
  AND columnInTableB            = <SOME value>
  AND anotherColumnInTableB     = <SOME other value>
  AND thirdColumnInTableB      IN (<
    SET OF VALUES               >)
  AND fourthColumnInTableB LIKE <SOME string>
  )
  **ROWNUM < 50** ;