ProC 中使用和不使用游标的 SQL 查询比较

发布于 2024-08-13 10:35:06 字数 402 浏览 6 评论 0原文

从查询性能角度来看,哪个更有效?
考虑到 T 是一个表,PK 是表 T 中的主键。它们是不同的还是只是一个选择问题?

select col1, col2 into :var1, :var2 
  from T 
 where PK = a

...或:

  1. EXEC SQL DECLARE aCursor CURSOR FOR select col1, col2 into :var1, :var2 from T where PK = a;
  2. EXEC SQL OPEN aCursor
  3. 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:

  1. EXEC SQL DECLARE aCursor CURSOR FOR select col1, col2 into :var1, :var2 from T where PK = a;
  2. EXEC SQL OPEN aCursor
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖彼岸 2024-08-20 10:35:07

如果你只影响一行,我不会使用游标,这看起来像是过度杀戮

If you are only fecthing a single row, i would NOT use a cursor, that seems like over kill

旧时模样 2024-08-20 10:35:07

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文