存储过程中的光标性能问题
我发现下面的 SQL 和动态 SQL 中使用了游标。 Profile 带来了相当多的执行计划,我认为它必须处理这个游标。这是一个错误的 SQL 选择吗?
SET @SelectStmtSubHeader = 'SELECT DISTINCT
dbo.dsb_testID(sh.GPCustomerID) AScursor -- RIGHT HERE
PONumber,
sh.GPCustomerID,
.....
I found a cursor being used in the below SQL and dynamic SQL. Profile brings up quite a bit of execution plans and I think it has to deal with this cursor. Is this a bad choice of SQL?
SET @SelectStmtSubHeader = 'SELECT DISTINCT
dbo.dsb_testID(sh.GPCustomerID) AScursor -- RIGHT HERE
PONumber,
sh.GPCustomerID,
.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果集合逻辑中存在替代方案,则游标几乎总是一个需要避免的糟糕选择。
SQL 基于集合逻辑。它们不应该像集合一样被迭代。
SQL 优化器通常非常善于找到巧妙的方法来检索数据。光标是一种相对简单的工具。不过 ANSI SQL 确实需要它,所以它通常存在。
这是 Sybase
光标性能示例
Cursors are nearly always a bad choice to be avoided if alternatives exist in set logic.
SQL is based around set logic. They aren't meant to be iterated through like a collection.
The SQL Optimizers are usually pretty good at finding clever ways to retrieve your data. A cursor is a relatively unsophisticated tool. ANSI SQL does require it though, so it's usually present.
Here is a good example from Sybase
Cursor Performance Example
这不是光标的示例。
光标需要...
您显示的片段代码似乎使用标量函数来派生一个值,该值是单词
cursor
的别名。但是,拥有一个名为cursor
的字段并不意味着它就是游标。That's not an example of a cursor.
A cursor needs to be...
The snipped code you've shown appears to use a scalar function to derive a value, which it aliases to the word
cursor
. But having a field calledcursor
doesn't make it a cursor.