存储过程中的光标性能问题

发布于 2024-12-25 22:34:03 字数 274 浏览 4 评论 0原文

我发现下面的 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 技术交流群。

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

发布评论

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

评论(2

只等公子 2025-01-01 22:34:04

如果集合逻辑中存在替代方案,则游标几乎总是一个需要避免的糟糕选择。

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

请叫√我孤独 2025-01-01 22:34:03

这不是光标的示例。

光标需要...

DECLARE this_is_a_cursor CURSOR
FOR
  SELECT
    stuff
  FROM
    a_query

您显示的片段代码似乎使用标量函数来派生一个值,该值是单词 cursor 的别名。但是,拥有一个名为 cursor 的字段并不意味着它就是游标。

That's not an example of a cursor.

A cursor needs to be...

DECLARE this_is_a_cursor CURSOR
FOR
  SELECT
    stuff
  FROM
    a_query

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 called cursor doesn't make it a cursor.

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