在复制到表变量而不是实际表的数据上创建游标会更好吗?
我有过一些关于游标导致死锁的糟糕经历。我一直在尝试尽可能消除光标。当它们不可避免时,我会尝试最大限度地减少光标打开时完成的工作量。
将数据复制到表变量并在表变量上构建游标是否更好?
我知道答案可能取决于数据集的大小。此时,我关心的是一个非常小的数据集(一堆记录),并且很少更新。有时我仍然会在大型数据集上创建复杂的循环。
复制到表变量有什么好处,还是我让事情变得过于复杂?
I've had some bad experiences with cursors causing deadlocks. I have been trying to eliminate cursors wherever possible. When they are unavoidable, I try minimize the amount of work done while the cursor is open.
Is it ever better to copy the data to a table variable, and build a cursor over the table variable?
I know the answer will likely depend on the size of the data set. At them moment, I am concerned with a very small, data set (a handlefull of records), that is rarely updated. On occasion I will still have create complex loops over large data sets.
Is there any benefit to copying to table variables, or am I over-complicating things?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在声明游标时将其定义为
STATIC
,SQL Server 将自动为您创建数据的临时副本。来自文档:
If you define the cursor as
STATIC
when you declare it, SQL Server will make a temporary copy of the data for you automatically.From the documentation: