Cursors Be Gone!

本文介绍了一种避免使用SQL Server中游标的方法,并通过实例演示了如何将游标转换为传统循环,以此提高性能。通过对比游标和常规循环的执行计划,展示了游标的效率较低,尤其是在处理大量数据时。

Cursors Be Gone!

By Gaby Abed, 2008/12/24

 

I decided to make an attempt at a first article to help folks wean themselves off using cursors. It is quite easy to alter your code to use traditional loops and avoid the overhead of using cursors. For this example, the performance difference would be minimal but on cursors that work on massive tables, the overhead of a cursor definitely gets in the way and a conversion to more conventional while loops would be better.

Here is a simple example of a cursor that does a dbcc checkdb on each database (I know sp_msforeachdb would work as well, but this is mainly proof of concept).

declare @query varchar(100), @dbname sysname

declare BadCursor Cursor for
  select name from sys.databases
  where name not in ('master', 'model', 'msdb', 'tempdb')

open BadCursor
fetch next from BadCursor into @dbname
while @@fetch_status = 0
begin
select @query = 'dbcc checkdb(' + quotename(@dbname) + ')'
exec(@query)
fetch next from BadCursor into @dbname
end
close BadCursor
deallocate BadCursor go

To somehow get past the fetch next feature of a cursor, you will need to make a table. As this is an example with a small amount of rows, I will use the table variable @dblist, but for larger conversions, #temp tables would work just as well:

declare @query varchar(100),
@dbname sysname
declare @dblist table (dbname sysname)

insert into @dblist(dbname) select name from sys.databases
where name not in ('master', 'model', 'msdb', 'tempdb')

while (select count(*) from @dblist) > 0
begin
select top 1 @dbname = dbname from @dblist
select @query = 'dbcc checkdb(' + quotename(@dbname) + ')'
exec(@query)
delete from @dblist where dbname = @dbname
end go

When compared side by side when displaying the estimated execution plan, the Cursor based query was 69% compared to the 31% of the more conventional loop. So at least for this example, the cursor takes at least twice as long as the reguler while loop.

As you can see in the image below, while the actual allocation/deallocation of the cursor doesn't have much of an impact, the inefficient way the query is executed is what causes this to be slow. Imagine scaling the cursor to tables with millions of rows.

 

Zoom in   |   Open in new window
Zoom out   |   Open in new window

I hope this helps everyone realize the cost of doing business with cursors. While I readily admit the fetch next feature of cursors is attractive, don't be distracted by the dazzle and realize there is always a better alternative to using them.

Good luck folks.

内容概要:本文围绕“栅格内牛耕”策略与A星(A*)算法相结合的全覆盖路径规划方法展开研究,提出了一种适用于栅格化环境的高效路径规划方案。通过引入系统性的“牛耕式”扫描策略,确保对区域内所有有效栅格的无遗漏覆盖,并融合A*算法进行路径优化,提升路径的合理性与执行效率。该方法特别适用于需完成全域遍历任务的智能设备,如清洁机器人、农业自动化机械和巡检无人机等。文中详细阐述了算法的设计思路、关键实现步骤及启发式函数的改进机制,并借助Matlab平台进行了仿真实验,验证了该方法在复杂障碍环境下的有效性与鲁棒性。; 适合人群:具备一定Matlab编程基础,从事路径规划、智能机器人、自动化控制等相关领域的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于扫地机器人、无人农场农机、巡检机器人等需实现区域全覆盖作业的设备路径规划;②帮助研究人员深入理解A*算法在全覆盖场景中的改进策略,掌握覆盖优先级、方向约束与回溯机制的设计方法;③作为教学与科研案例,辅助学习启发式搜索算法与系统性覆盖策略的融合应用。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点分析A*算法在覆盖完整性与路径最优化之间的平衡机制,通过调整环境地图、障碍物分布及起始点位置开展多组仿真实验,深入探究算法性能影响因素与优化方向。
内容概要:本文深入研究了LLC谐振变换器的变频移相混合控制模型,并基于Simulink平台完成了系统的建模仿真与性能验证。该控制策略融合变频控制与移相控制的优点,旨在提升LLC变换器在宽输入电压和宽负载工况下的转换效率与运行稳定性。文章系统阐述了LLC谐振变换器的工作原理、小信号建模方法、混合控制策略的设计思路及其实现方式,重点分析了其在实现零电压开关(ZVS)、抑制环流、降低开关损耗和提高整体效率方面的优势。通过详尽的仿真结果,验证了所提出混合控制模型在动态响应、稳态精度和系统鲁棒性方面的优越性能。; 适合人群:具备电力电子变换器基础知识、掌握Simulink/Matlab仿真技能,从事高频高效电源系统、新能源变换技术或相关领域研究的研究生、高校教师及工程技术人员。; 使用场景及目标:① 深入理解LLC谐振变换器的核心工作机理与数学模型;② 掌握并实现变频与移相结合的先进控制策略;③ 利用Simulink搭建完整的控制系统模型,进行仿真分析与参数优化,为实际硬件开发提供理论支撑和技术储备。; 阅读建议:建议读者结合提供的Simulink模型进行同步操作与参数调试,重点关注控制逻辑的实现细节与关键波形的分析,有条件者可进一步开展硬件实验,实现从仿真到实物的闭环验证,深化理论与工程实践的融合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值