PL/SQL - 在嵌套查询中使用变量
我想用 PL/SQL 做这样的事情:(
for ACCOUNT in account_cursor
loop
for related_data in (select something from table where some_column = ACCOUNT.column)
loop
endloop;
endloop;
大写强调)
我远离我的开发环境,所以我无法测试它,所以请忽略任何小的语法错误。我更关心的是我是否可以这样使用变量。
编辑:
解释我想要实现的目标。我有两个巨大的表:帐户和日志。我想获取特定几个帐户的日志数据。由于记录数量较多,直接选择/连接速度非常慢。我试图通过首先选择我感兴趣的帐户然后将它们加入到日志表中来加快该过程。任何建议将不胜感激。
I'd like to do something like this with PL/SQL:
for ACCOUNT in account_cursor
loop
for related_data in (select something from table where some_column = ACCOUNT.column)
loop
endloop;
endloop;
(caps for emphasis)
I'm away from my dev environment so I can't test this out, so please ignore any minor syntactical errors. I am more concerned about whether I can use the variables in this way.
EDIT:
An explination of what I'm trying to achieve. I have two huge tables: accounts and log. I want to get the log data for a specific few accounts. Because of the number of records a straight select/join is extremely slow. I am trying to speed up the proccess by first selecting the accounts I am interested in and then joining these to the log table. Any advice would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您似乎将显式游标 (
account_cursor
) 与隐式游标 (related_data
) 混合在一起...除了 ENDLOOP 需要是
END LOOP
,它在语法上是正确的 &有效的。您可以在此链接中查看两种类型游标的示例。但我想知道您实际上在做什么来查看光标方法是否确实有必要。You appear to be mixing an Explicit cursor (
account_cursor
) with an implicit cursor (related_data
)...Aside from ENDLOOP needing to be
END LOOP
, it's syntactically correct & valid. You can see examples of both types of cursors in this link. But I wonder what you're actually doing to see if the cursor approach is actually necessary.在使用此方法之前,请阅读上面的回复。从技术上讲,是的,您可以访问变量..类似这样。
总是非常慢,因为这是逐行处理。如果您可以为您的逻辑发布代表性代码,您将得到指示如何在集合中处理相同代码的响应。
Please read the response above before using this approach. Technically, yes, you can access the variables .. something like this.
Invariably, this is pretty slow as this is row-by-row processing. If you can post a representative code for your logic, you will get responses which indicate how do process the same in Sets.
正如 Rajesh 所观察到的,在游标中执行此操作会非常慢。
如果查询未使用适当的索引,则相对较少值的直接选择/连接只会在大型表上缓慢 - 检查帐户和日志表中是否存在帐号索引。如果存在,请检查您的查询是否在解释计划中使用它们。
As Rajesh observed, doing this in a cursor will be very slow.
A straight select/join of relatively few values should only be slow on huge tables if the query isn't using appropriate indexes - check that indexes on the account number exist in both accounts and log tables. If they do, check that your query is using them in the explain plan.