Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 optimization issue

Author  Topic 

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-02-01 : 03:11:31
This is a known "problem"/"solution" in T-SQL (regardless of version 2000,2005,2008). but i don't know what is the of this particular case.

i have two procedures (using AdventureWorks):
1.

create procedure test1
@date_to smalldatetime
,@date_from smalldatetime
as


select
d.id
,d.Q
,d.cash
,d.price

from

(
select
count(1) as id
,sum(OrderQty) as Q
,sum(UnitPrice)/100 as cash
,p_d.UnitPrice as price
from
Purchasing.PurchaseOrderDetail as p_d
join Purchasing.PurchaseOrderHeader as p
on p_d.PurchaseOrderID = p.PurchaseOrderID
where
p_d.DueDate between @date_from and @date_to

group by
p_d.PurchaseOrderID
,p_d.DueDate
,p_d.UnitPrice
) as d

group by d.Q,d.id,d.price,d.cash

go




2.

create procedure test2
@date_to smalldatetime
,@date_from smalldatetime
as

declare @date1 smalldatetime
declare @date2 smalldatetime

set @date1=@date_to
set @date2=@date_from

select
d.id
,d.Q
,d.cash
,d.price

from

(
select
count(1) as id
,sum(OrderQty) as Q
,sum(UnitPrice)/100 as cash
,p_d.UnitPrice as price
from
Purchasing.PurchaseOrderDetail as p_d
join Purchasing.PurchaseOrderHeader as p
on p_d.PurchaseOrderID = p.PurchaseOrderID
where
p_d.DueDate between @date1 and @date2

group by
p_d.PurchaseOrderID
,p_d.DueDate
,p_d.UnitPrice
) as d

group by d.Q,d.id,d.price,d.cash

go





First procedure uses procedure_parameters in where condition of select statement, where are the second procedure declares local variables (read from procedure_parameters) in where condition of select statement.

the second procedure executes much faster than the first one (especially visible if you have huge database - which adventureworks is not).

Can anyone explain, what is (how to address) the name of this common/known problem/issue? :)

thank you

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2009-02-01 : 07:26:32
quote:
Originally posted by slimt_slimt
the second procedure executes much faster than the first one (especially visible if you have huge database - which adventureworks is not).



Not always. What you're encountering is parameter sniffing. The optimiser when it compiles a procedure, uses the current value of the parameters to estimate the rows affected by the query. This is so that it can use the most optimal operators for the query, as well as calculating the memory required. Providing the statistics are up to date and it can get a good estimate this is not a problem.

The problem comes when the procedure's exec plan is reused and the estimates are no longer correct. This is typically when there's data-skew (not all values are equally likely in the table). If that happens, the plan is no longer optimal, but will still be reused.

When you use variables, the optimiser can't see the values of them, and guesses the number of rows that will be affected. The guess is based on the average density of the columns involved and the number of rows. It's often far less accurate than using the parameters to get a row estimate.

I wrote 3 posts on parameter sniffing on my blog. They're in the performance category.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-02-01 : 07:56:38
Thank you very much for your extensive explanation and buzz-word revelation :) parameter sniffing. will goggle it up.

greets to ZA :)
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2009-02-01 : 15:04:20
http://sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing/
http://sqlinthewild.co.za/index.php/2008/02/25/parameter-sniffing-pt-2/
http://sqlinthewild.co.za/index.php/2008/02/25/parameter-sniffing-pt-3/

--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -