ProC 中使用和不使用游标的 SQL 查询比较
从查询性能角度来看,哪个更有效?
考虑到 T 是一个表,PK 是表 T 中的主键。它们是不同的还是只是一个选择问题?
select col1, col2 into :var1, :var2
from T
where PK = a
...或:
- EXEC SQL DECLARE aCursor CURSOR FOR select col1, col2 into :var1, :var2 from T where PK = a;
- EXEC SQL OPEN aCursor
- EXEC SQL FETCH aCursor
我认为如果可以直接检索单行,则声明一个游标来根据主键从表中获取单行就没那么有意义了?
Which, query performance wise, is more effective?
Considering T is a table, and PK is the primary key in the table T. Are they different or they are just a matter of choice?
select col1, col2 into :var1, :var2
from T
where PK = a
...or:
- EXEC SQL DECLARE aCursor CURSOR FOR select col1, col2 into :var1, :var2 from T where PK = a;
- EXEC SQL OPEN aCursor
- EXEC SQL FETCH aCursor
I think declaring a cursor to fetch a single row from a table based on primary key make less sense if the single row could be retrieved directly instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你只影响一行,我不会使用游标,这看起来像是过度杀戮
If you are only fecthing a single row, i would NOT use a cursor, that seems like over kill
SELECT INTO
总是比使用光标更快。如果在光标打开时没有执行任何操作,则根本没有必要使用光标。除此之外,只要表中存在 PK 值,通过 PK 进行搜索就保证返回单行。
SELECT INTO
will always be faster than using a cursor.If you aren't performing anything while the cursor is open, there's no point to using the cursor at all. That's besides the fact that searching by the PK is guaranteed to return a single row, providing the PK value exists in the table.