stored procedure optimized execution plan?
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.
精彩评论