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
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Why is my parameter causing a different Execution?

Author  Topic 

Deadpool
Starting Member

2 Posts

Posted - 2012-08-18 : 23:58:01
All my performance tuning gurus out there I have 2 questions. (SQL 2005)

[BASE QUERY]
where v.newdate = ‘1/1/2012’


when I used this where clause the execution plan looked great using mostly Index seeks and nested loops. I believe it had 1 Bookmark
Lookup, but for the most part it was pretty efficient.

declare @begin datetime
set @begin = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)

[BASE QUERY]
where v.newdate = @begin

when I used the where clause with the autogenerated first of year parameter the execution plan looked totally different. It was using
parallelism and hash
join every where. It is definitely not as effiecient as version one.

Question 1
How can the execution plans be soooo different when the base query is exactly the same but only the way the date is generated in the
Where clause is different?

Question 2

Isn’t that a good practice to isolate sections of a parameterized stored procedure and tune as you go? I would think that providing
the date would be the same way the optimizer would handle the provided parameter

komkrit
Yak Posting Veteran

60 Posts

Posted - 2012-08-19 : 00:37:42
Yes you right. They should be the same plan.
I point to the v.newdate data-type, what is it?

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Komkrit Yensirikul
Welcome for all question. Let us know if our solution solved your problem.
Go to Top of Page

Deadpool
Starting Member

2 Posts

Posted - 2012-08-19 : 00:44:43
quote:
Originally posted by komkrit

Yes you right. They should be the same plan.
I point to the v.newdate data-type, what is it?


Data type is datetime.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Komkrit Yensirikul
Welcome for all question. Let us know if our solution solved your problem.

Go to Top of Page

jackv
Master Smack Fu Yak Hacker

2179 Posts

Posted - 2012-08-19 : 01:54:02
When you changed to the second format - did you issue a DBCC FREEPROCCACHE to clear down the procedure cache? This will force the first EXEC, after the cache clear out , to complete a fresh compile.
It is possible you are still using the older compiled plan

Jack Vamvas
--------------------
http://www.sqlserver-dba.com
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-19 : 06:56:07
As Jack pointed out this is most likely because somehow you are stuck with a bad execution plan.

Is this an adhoc query or is this in a stored procedure? I have seen this happen with stored procedures because of parameter sniffing. But, with plan reuse, it could happen in ad-hoc queries as well.

If this is a production server and you are concerned about doing DBCC FREEPROCCACHE, you can try these alternatives. (I am really shooting in the dark thinking that something will stick).

1. Change your query to
where v.newdate = @begin OPTION (RECOMPILE)
This forces a statement level recompilation every time the query is run. Whether this is good or not depends on how complex the statement is. If it is a simple select with a where clause the recompilation penalty may be small enough.

2. Change your query to
where v.newdate = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
This is based on a well-established technique called "shoot-in-the-dark".

3. Change your query to this:
declare @begin datetime
set @begin = DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)
declare @dummyBegin DATETIME;
set @dummyBegin = @Begin;
...
-- and then
where v.newdate = @dummyBegin;
This again is based on the technique used in #2

4. Change your query to this:
create table #tmpDate (dummyDate DATETIME);
insert into #tmpDate (dummyDate) VALUES (@begin);

-- and then
FROM
YourOtherTable v
cross join #tmpDate td
WHERE
v.newDate = td.dummyDate;


Even though I said "shoot-in-the-dark", all of the above are attempts to give the query optimizer a chance to find a better execution plan.
Go to Top of Page
   

- Advertisement -