Re: SQL Server 2000 UDF Intermittent Slow Execution
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Fri, 30 May 2008 21:17:11 +0000 (UTC)
Plamen Ratchev (Plamen@xxxxxxxxxxxxx) writes:
To solve this problem you can assign the parameter to a local variable
and then use the local variable in the query. The value of the local
variable is not known at compilation time because it is not set until
the UDF is executed. This in effect disables parameter sniffing.
This may or may not work. There are two ways why this could backfire:
1) The query is compiled for an unknown value, and the distribution is
such that for an unknown value a scan may be best. Say for instance
that of the rows in tblPaymentTemplate, most are for schedules
that are no longer used, but there are many rows for these schedules.
Whereas active schedules are more selective.
2) When recompilation occurs because of changed statistics, this could
happen on statement level, in which case the variable may be sniffed.
Copying a parameter to a local variable mainly makes sense, in my
opinion, if the most common parameter value is actually never used in
the query. The typical case is a date parameter where NULL means "today".
Since the NULL value is not used in the query, it's better to copy to
local variable.
SQL Server 2008 adds the FORCESEEK hint.
It also adds the hint OPTIMIZE FOR @var UNKNOWN, so that you don't
need that extra variable.
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.
- Follow-Ups:
- Re: SQL Server 2000 UDF Intermittent Slow Execution
- From: Plamen Ratchev
- Re: SQL Server 2000 UDF Intermittent Slow Execution
- References:
- SQL Server 2000 UDF Intermittent Slow Execution
- From: Peter Nurse
- Re: SQL Server 2000 UDF Intermittent Slow Execution
- From: Plamen Ratchev
- SQL Server 2000 UDF Intermittent Slow Execution
- Prev by Date: Re: Strange select
- Next by Date: Re: strange Problem
- Previous by thread: Re: SQL Server 2000 UDF Intermittent Slow Execution
- Next by thread: Re: SQL Server 2000 UDF Intermittent Slow Execution
- Index(es):
Relevant Pages
|