You are here: Re: Why optimizer is not smart? Is dynamic SQL my only option... « MsSQL Server « IT news, forums, messages
Re: Why optimizer is not smart? Is dynamic SQL my only option...

Posted by Erland Sommarskog on 02/08/07 08:51

serge (sergea@nospam.ehmail.com) writes:
> declare @ContactId as integer
> set @ContactId = 5
> select *
> from Person.Contact
> where ContactId = @ContactId
> OR @ContactId = -1
>
> If you run this in SQL 2005 on the AdventureWorks database,
> why the logical reads is 561
> Table 'Contact'. Scan count 1, logical reads 56
>
> and not 2 when you run without the second OR condition:

Because the optimizer computes the plan, it does not know what the value of
@ContactId will be when the statement is executed.

I have a whole article on my web site on this topic,
http://www.sommarskog.se/dyn-search.html.


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

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация