You are here: Re: Performance issue using conditional WHERE clause « MsSQL Server « IT news, forums, messages
Re: Performance issue using conditional WHERE clause

Posted by Daniel Eyer on 09/29/35 12:01

SELECT * FROM Employees
WHERE [Name] = IsNull(@Name,[Name])

"Jared" <blacktoe.the.crippler@gmail.com> a ιcrit dans le message de news:
d5382a71-4306-4603-809f-9082269c9a85@s8g2000prg.googlegroups.com...
> Consider the following two functionally identical example queries:
>
> Query 1:
>
> DECLARE @Name VARCHAR(32)
> SET @Name = 'Bob'
> SELECT * FROM Employees
> WHERE [Name] = CASE WHEN @Name IS NULL THEN [Name] ELSE @Name END
>
> Query 2:
>
> SELECT * FROM Employees WHERE [Name] = 'Bob'
>
> I would expect SQL Server to construct an identical QEP under the hood
> for these two queries, and that they would require essentially the
> same amount of time to execute. However, Query 1 takes much longer to
> run on my indexed table of ~300,000 rows. By "longer", I mean that
> Query 1 takes about two seconds, while Query 2 returns almost
> instantly.
>
> Is there a way to implement a conditional WHERE clause without
> suffering this performance hit? I want to avoid using the IF...THEN
> method because I frequently require several optional parameters in the
> WHERE clause.
>
> Thanks!
> Jared

 

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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация