问题描述
在 SQL Server 存储过程中何时使用临时表以及何时使用游标.哪个是最好的选择性能明智的?
如果可能的话,避免游标就像瘟疫一样.SQL Server 基于集合 - 您需要以 RBAR(逐行痛苦的方式)方式执行的任何操作都将缓慢、缓慢,并且违背 SQL 工作原理的基本原则.>
您的问题非常模糊 - 根据这些信息,我们无法真正判断您要做什么.但主要建议仍然是:只要有可能(并且在绝大多数情况下都是可能的),使用基于集合的操作 - SELECT、UPDATE、INSERT
和连接 - 不要将您的过程思维强加于 SQL服务器 - 这不是最好的方法.
因此,如果您可以使用基于集合的操作来填充和使用您的临时表,那么我每次都更喜欢这种方法而不是游标.
In SQL Server stored procedures when to use temporary tables and when to use cursors. which is the best option performance wise?
If ever possible avoid cursors like the plague. SQL Server is set-based - anything you need to do in an RBAR (row-by-agonizing-row) fashion will be slow, sluggish and goes against the basic principles of how SQL works.
Your question is very vague - based on that information, we cannot really tell what you're trying to do. But the main recommendation remains: whenever possible (and it's possible in the vast majority of cases), use set-based operations - SELECT, UPDATE, INSERT
and joins - don't force your procedural thinking onto SQL Server - that's not the best way to go.
So if you can use set-based operations to fill and use your temporary tables, I would prefer that method over cursors every time.
这篇关于SQL Server 临时表与游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!