Database Engine Tuning Advisor suggestion to replace syntax.
  Home FAQ Contact Sign in
microsoft.public.sqlserver.tools only
 
Advanced search
POPULAR GROUPS

more...

microsoft ... sqlserver.tools Profile…
 Up
Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Mike
Date: Aug 28, 2008 09:20

SQL Server 2005 Database Engine Tuning Advisor informs me that I should
replace query 1 below with the syntax in query 2 below. I’m just not seeing
the reason since both the “SELECT” statements optimization plans are stored
for reuse. Am I missing something?

Mike.

Query 1
EXEC sp_executesql N'SELECT FirstName, LastName, Company, StreetAddress,
City, State, ZIP5, ZIP4
FROM Customers
WHERE City = @P1
AND State = @P2',
N'@P1 char(32), @P2 varchar(32)', 'Poedunk', 'Iowa'
Show full article (0.72Kb)
8 Comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Tibor Karaszi
Date: Aug 29, 2008 00:53

Russell, Erland, Mike,

Wow, I've been reading this thread three times now and I'm still confused. Seems like something is
messing with my head and twist some things in the opposite direction...

Going back to Mike's original post:

Mike,
Are you saying that DTE suggest instead of sp_executesql version use static SQL with variables
instead? Just so I understand. There are important differences between the two.

For the sp_executesql alternative, the parameter can be sniffed and used to determine things like
selectivity and also plan can be re-used. This can be a good thing or a bad thing.

For the TSQL variable alternative, the optimizer has no knowledge of the contents of the variables
so selectivity can not be determined based on those values. This can be a good thing or a bad thing.

Which one is best? I don't know and most probably DTA doesn't know either. If you want to read more
about the technicalities and differences between the two alternatives, check out this blog I just
wrote:
http://sqlblog.com/blogs/tibor_karaszi/archive/2008/08/29/execution-plan-re-use-sp...

Russell,

I don't see a contradiction between that BOL quote and Erland's post. Erland's remark was about the
TSQL variable alternative, not the sp_executesql alternative. Perhaps that confused you?
Show full article (4.50Kb)
no comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Russell Fields
Date: Aug 29, 2008 10:09

Tibor, Quite right. I was confused and added to the confustion. Sorry
about that. - RLF

"Tibor Karaszi" hotmail.nomail.com> wrote in
message news:%%23JGNaxaCJHA.4724@TK2MSFTNGP05.phx.gbl...
> Russell, Erland, Mike,
>
> Wow, I've been reading this thread three...
Show full article (3.34Kb)
no comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Erland Sommarskog
Date: Aug 29, 2008 15:42

Russell Fields (RussellFields@NoMail.com) writes:
> If that is true, Mike's code would benefit from reuse, but only if he
> fully specified the table name. (Because the Books Online also comment
> "If object names in the statement string are not fully qualified, the
> execution plan is not reused.")

Which is not fully correct. The plan is reused, if the next guy has the
same default schema. But best practice is to use two-part notation with
sp_executesql.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
no comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Russell Fields
Date: Aug 30, 2008 05:22

Thanks, Erland - RLF

"Erland Sommarskog" wrote in message
news:Xns9B0A7496817BYazorman@127.0.0.1...
> Russell Fields (RussellFields@NoMail.com) writes:
>> If that is true, Mike's code would benefit...
Show full article (0.93Kb)
no comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Mike
Date: Sep 2, 2008 09:06

Tibor,

DTE is suggesting that I replace the logic in query 1 with that in Query 2.
Sorry for the delay, I been out of town.

Mike.

"Tibor Karaszi" wrote:
> Russell, Erland, Mike,
>
> Wow, I've been reading this thread three times now and I'm still confused. Seems like something is
> messing with my head and twist some things in the opposite direction...
Show full article (4.89Kb)
no comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Tibor Karaszi
Date: Sep 2, 2008 10:08

Mike,

Then in my opinion DTE is making some very bold assumptions about your situation. Check my blog post
for elaboration...

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://sqlblog.com/blogs/tibor_karaszi

"Mike" wrote in message
news:50B8B307-B78B-457D-B9AC-13BFD535086D@microsoft.com...
> Tibor,
>
> DTE is suggesting that I replace the logic in query 1 with that in Query 2.
> Sorry...
Show full article (5.37Kb)
no comments
Re: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Mike
Date: Sep 2, 2008 13:17

Tibor,

I was under the same impression when I first ran it with a small sample set
of trace data. I ran it again over the long weekend with a much larger set of
tract data and all, yes "ALL", of the recommendations are suggesting that I
replace all of my dynamic SQL running via sp_executesql with the same type
query in my query 2 example.

Mike.

"Tibor Karaszi" wrote:
> Mike,
>
> Then in my opinion DTE is making some very bold assumptions about your situation. Check my blog post
> for elaboration...
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default...
Show full article (6.04Kb)
no comments
RE: Database Engine Tuning Advisor suggestion to replace syntax.         


Author: Mike
Date: Sep 4, 2008 13:59

Can Microsoft comment on this please?

Mike.

"Mike" wrote:
Show full article (0.84Kb)
no comments