开发者

stored procedure optimized execution plan?

开发者 https://www.devze.com 2023-03-09 08:58 出处:网络
If you have multiple if conditions in a stored procedure, I\'m guessing the execution plan is going to be less optimized so is the latter below better?

If you have multiple if conditions in a stored procedure, I'm guessing the execution plan is going to be less optimized so is the latter below better?

if not exists (select * from accounts)
 begi开发者_开发问答n
  raiseerror('error', 16, 1);
 end

begin try
select 1/0 from accounts
end try
begin catch
  raiseerror('error', 16,1)
end catch


SQL Server will optimize the Not Exists better because you're telling it what you want. It can skip the whole "retrieve a (lot of) rows" and just pass back a boolean true/false if any rows exist


Optimization is best determined on the live system (or as close as you can get in test) since "your mileage may vary". However, if you are looking to see if there are any records in the accounts table, just do SELECT COUNT(*) from accounts.

Using a not exists is usually bad for optimization, so this case is a bit easier.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号