c# - Why is exec sp_executesql much slower than inline sql? -
i have test query in management studio , execute fast(less second)
declare @p_message_0 varchar(3) = 'whh' declare @p_createdtime_0 datetime = '2015-06-01' select count(1) (select * [logs](nolock) contains([message], @p_message_0) , [createdtime]<@p_createdtime_0) t select t2.* (select t.*,row_number() on (order id desc) rownum (select * [logs](nolock) t contains([message], @p_message_0) , [createdtime]<@p_createdtime_0) t) t2 rownum>0 , rownum<=20
execution plan this:
then move c# ado.net, run this
exec sp_executesql n'select count(1) (select * [logs](nolock) contains([message], @p_message_0) , [createdtime]<@p_createdtime_0) t select t2.* (select t.*,row_number() on (order id desc) rownum (select * [logs](nolock) t contains([message], @p_message_0) , [createdtime]<@p_createdtime_0) t) t2 rownum>0 , rownum<=20',n'@p_message_0 varchar(3),@p_createdtime_0 datetime',@p_message_0='whh',@p_createdtime_0='2015-06-01'
this 1 run slow(about 30s). execution plan like:
i don't know make these 2 plan different. sql server 2008 r2 sp2, , have tried parameter hint , option (recompile), both not work me.
try updating statistics. first 1 uses variable today's date. variables aren't sniffed guessed distribution. second 1 uses parameter. can sniffed.
if stats haven't been updated today sql server think no rows exist date give plan on basis. such nested loops plan estimated execute tvf once execute many times.
Comments
Post a Comment